Set automatic variable declaration mode
Integer_Type _auto_declare
The _auto_declare
variable may be used to have undefined
variable implicitly declared. If set to zero, any
variable must be declared with a variable
declaration before it
can be used. If set to one, then any undeclared variable will be
declared as a static
variable.
The _auto_declare
variable is local to each compilation unit and
setting its value in one unit has no effect upon its value in other
units. The value of this variable has no effect upon the variables
in a function.
The following code will not compile if X
not been
declared:
X = 1;
However,
_auto_declare = 1; % declare variables as static.
X = 1;
is equivalent to
static variable X = 1;
This variable should be used sparingly and is intended primarily for interactive applications where one types S-Lang commands at a prompt.
Return the class-id of a specified type
Int_Type __class_id (DataType_Type type)
This function returns the internal class-id of a specified data type.
Return the class-type of a specified type
Int_Type __class_type (DataType_Type type))
Internally S-Lang objects are classified according to four types:
scalar, vector, pointer, and memory managed types. For example, an
integer is implemented as a scalar, a complex number as a vector,
and a string is represented as a pointer. The __class_type
function returns an integer representing the class-type associated
with the specified data type. Specifically, it returns:
0 memory-managed
1 scalar
2 vector
3 pointer
Get the name of the current namespace
String_Type current_namespace ()
The current_namespace
function returns the name of the
static namespace associated with the compilation unit. If there is
no such namespace associated with the compilation unit, then the
empty string ""
will be returned.
Get the DataType_Type for a specified internal class-id
DataType_Type __datatype (Int_Type id)
This function is the inverse of __class_type in the sense that it
returns the DataType_Type
for the specified class-id. If no
such class exists, the function will return NULL
.
One should not expect distinct interpreter instances to always return
the same value for a dynamically assigned class-id such as one
defined by a module or one stemming from a typedef
statement.
Test for equality of two objects
Int_Type _eqs (a, b)
This function tests its two arguments for equality and returns 1 if they are equal or 0 otherwise. What it means to be equal depends upon the data types of the objects being compared. If the types are numeric, they are regarded as equal if their numerical values are equal. If they are arrays, then they are equal if they have the same shape with equal elements. If they are structures, then they are equal if they contain identical fields, and the corresponding values are equal.
_eqs (1, 1) ===> 1
_eqs (1, 1.0) ===> 1
_eqs ("a", 1) ===> 0
_eqs ([1,2], [1.0,2.0]) ===> 1
For testing sameness, use __is_same
.
Get all environment variables
String_Type[] = get_environ()
The get_environ
function returns an array of strings
representing the environmen variables defined for the current
process. Each element of the array will be of the form
NAME=VALUE
.
This function will return NULL
if the system does not support this
feature.
Get the value of an environment variable
String_Type getenv(String_Type var)
The getenv
function returns a string that represents the
value of an environment variable var
. It will return
NULL
if there is no environment variable whose name is given
by var
.
if (NULL != getenv ("USE_COLOR"))
{
set_color ("normal", "white", "blue");
set_color ("status", "black", "gray");
USE_ANSI_COLORS = 1;
}
Get a reference to a global object
Ref_Type __get_reference (String_Type nm)
This function returns a reference to a global variable or function
whose name is specified by nm
. If no such object exists, it
returns NULL
, otherwise it returns a reference.
Consider the function:
define runhooks (hook)
{
variable f;
f = __get_reference (hook);
if (f != NULL)
@f ();
}
This function could be called from another S-Lang function to allow
customization of that function, e.g., if the function represents a
jed editor mode, the hook could be called to setup keybindings for
the mode.
is_defined, typeof, eval, autoload, __is_initialized, __uninitialize
Create a new static namespace
implements (String_Type name)
The implements
function may be used to create a new static
namespace and have it associated with the current compilation unit.
If a namespace with the specified name already exists, a
NamespaceError
exception will be thrown.
In addition to creating a new static namespace and associating it with the compilation unit, the function will also create a new private namespace. As a result, any symbols in the previous private namespace will be no longer be accessible. For this reason, it is recommended that this function should be used before any private symbols have been created.
Suppose that some file t.sl
contains:
implements ("My");
define message (x)
{
Global->message ("My's message: $x"$);
}
message ("hello");
will produce "My's message: hello"
. This message
function may be accessed from outside the namespace via:
My->message ("hi");
Since message
is an intrinsic function, it is public and may
not be redefined in the public namespace.
The implements
function should rarely be used. It is
preferable to allow a static namespace to be associated with a
compilation unit using, e.g., evalfile
.
Determine whether or not an object is callable
Int_Type __is_callable (obj)
This function may be used to determine if an object is callable by dereferencing the object. It returns 1 if the argument is callable, or zero otherwise.
__is_callable (7) ==> 0
__is_callable (&sin) ==> 1
a = [&sin];
__is_callable (a[0]) ==> 1
__is_callable (&a[0]) ==> 0
Determine whether or not a type is a numeric type
Int_Type __is_datatype_numeric (DataType_Type type)
This function may be used to determine if the specified datatype
represents a numeric type. It returns 0
if the datatype does not
represents a numeric type; otherwise it returns 1 for an
integer type, 2 for a floating point type, and 3 for a complex type.
Determine whether or not an object is a numeric type
Int_Type __is_numeric (obj)
This function may be used to determine if an object represents a numeric type. It returns 0 if the argument is non-numeric, 1 if it is an integer, 2 if a floating point number, and 3 if it is complex. If the argument is an array, then the array type will be used for the test.
__is_numeric ("foo"); ==> 0
__is_numeric ("0"); ==> 0
__is_numeric (0); ==> 1
__is_numeric (PI); ==> 2
__is_numeric (2j); ==> 3
__is_numeric ([1,2]); ==> 1
__is_numeric ({1,2}); ==> 0
Test for sameness of two objects
Int_Type __is_same (a, b)
This function tests its two arguments for sameness and returns 1 if they are the same, or 0 otherwise. To be the same, the data type of the arguments must match and the values of the objects must reference the same underlying object.
__is_same (1, 1) ===> 1
__is_same (1, 1.0) ===> 0
__is_same ("a", 1) ===> 0
__is_same ([1,2], [1,2]) ===> 0
For testing equality, use _eqs
.
Add or change an environment variable
putenv (String_Type s)
This functions adds string s
to the environment. Typically,
s
should of the form "name=value"
. The function
throws an OSError
upon failure.
This function may not be available on all systems.
Set the argument list
__set_argc_argv (Array_Type a)
This function sets the __argc
and __argv
intrinsic variables.
S-Lang's installation prefix
String_Type _slang_install_prefix
The value of this variable is set at the S-Lang library's
compilation time. On Unix systems, the value corresponds to the
value of the prefix
variable in the Makefile. For normal
installations, the library itself will be located in the lib
subdirectory of the prefix
directory.
The value of this variable may or may not have anything to do with
where the slang library is located. As such, it should be regarded
as a hint. A standard installation will have the slsh
library files located in the share/slsh
subdirectory of the
installation prefix.
Test if the interpreter running in UTF-8 mode
Int_Type _slang_utf8_ok
If the value of this variable is non-zero, then the interpreter is running in UTF-8 mode. In this mode, characters in strings are interpreted as variable length byte sequences according to the semantics of the UTF-8 encoding.
When running in UTF-8 mode, one must be careful not to confuse a
character with a byte. For example, in this mode the strlen
function returns the number of characters in a string which may be
different than the number of bytes. The latter information may be
obtained by the strbytelen
function.
Returns the value of a variable and uninitialize the variable
__tmp (x)
The __tmp
function takes a single argument, a variable,
returns the value of the variable, and then undefines the variable.
The purpose of this pseudo-function is to free any memory
associated with a variable if that variable is going to be
re-assigned.
x = 3;
y = __tmp(x);
will result in `y' having a value of `3' and `x' will be undefined.
This function is a pseudo-function because a syntax error results if used like
__tmp(sin(x));
Uninitialize a variable
__uninitialize (Ref_Type x)
The __uninitialize
function may be used to uninitialize the
variable referenced by the parameter x
.
The following two lines are equivalent:
() = __tmp(z);
__uninitialize (&z);
Change to another namespace
use_namespace (String_Type name)
The use_namespace
function changes the current static namespace to
the one specified by the parameter. If the specified namespace
does not exist, a NamespaceError
exception will be generated.