Expand the dollar-escaped variables in a string
String_Type _$(String_Type s)
This function expands the dollar-escaped variables in a string and returns the resulting string.
Consider the following code fragment:
private variable Format = "/tmp/foo-$time.$pid";
define make_filename ()
{
variable pid = getpid ();
variable time = _time ();
return _$(Format);
}
Note that the variable Format
contains dollar-escaped
variables, but because the $
suffix was omitted from the
string literal, the variables are not expanded. Instead expansion is
deferred until execution of the make_filename
function through
the use of the _$
function.
Load a function from a file
autoload (String_Type funct, String_Type file)
The autoload
function is used to declare funct
to the
interpreter and indicate that it should be loaded from file
when it is actually used. If func
contains a namespace
prefix, then the file will be loaded into the corresponding
namespace. Otherwise, if the autoload
function is called
from an execution namespace that is not the Global namespace nor an
anonymous namespace, then the file will be loaded into the execution
namespace.
Suppose bessel_j0
is a function defined in the file
bessel.sl
. Then the statement
autoload ("bessel_j0", "bessel.sl");
will cause bessel.sl
to be loaded prior to the execution of
bessel_j0
.
Compile a file to byte-code for faster loading.
byte_compile_file (String_Type file, Int_Type method)
The byte_compile_file
function byte-compiles file
producing a new file with the same name except a 'c'
is added
to the output file name. For example, file
is
"site.sl"
, then this function produces a new file named
site.slc
.
The method
parameter is not used in the current
implementation, but may be in the future. For now, set
it to 0
.
Interpret a string as S-Lang code
eval (String_Type expression [,String_Type namespace])
The eval
function parses a string as S-Lang code and executes the
result. If called with the optional namespace argument, then the
string will be evaluated in the specified namespace. If that
namespace does not exist it will be created first.
This is a useful function in many contexts including those where it is necessary to dynamically generate function definitions.
if (0 == is_defined ("my_function"))
eval ("define my_function () { message (\"my_function\"); }");
Interpret a file containing S-Lang code
Int_Type evalfile (String_Type file [,String_Type namespace])
The evalfile
function loads file
into the interpreter
and executes it. If called with the optional namespace argument,
the file will be loaded into the specified namespace, which will be
created if necessary. If given no namespace argument and the file
has already been loaded, then it will be loaded again into an
anonymous namespace. A namespace argument given by the empty string
will also cause the file to be loaded into a new anonymous namespace.
If no errors were encountered, 1 will be returned; otherwise, a S-Lang exception will be thrown and the function will return zero.
define load_file (file)
{
try
{
() = evalfile (file);
}
catch AnyError;
}
For historical reasons, the return value of this function is not really useful.
The file is searched along an application-defined load-path. The
get_slang_load_path
and set_slang_load_path
functions
may be used to set and query the path.
Get the value of the interpreter's load-path
String_Type get_slang_load_path ()
This function retrieves the value of the delimiter-separated search
path used for loading files. The delimiter is OS-specific and may
be queried using the path_get_delimiter
function.
Some applications may not support the built-in load-path searching facility provided by the underlying library.
Set the value of the interpreter's load-path
set_slang_load_path (String_Type path)
This function may be used to set the value of the
delimiter-separated search path used by the evalfile
and
autoload
functions for locating files. The delimiter is
OS-specific and may be queried using the path_get_delimiter
function.
public define prepend_to_slang_load_path (p)
{
variable s = stat_file (p);
if (s == NULL) return;
if (0 == stat_is ("dir", s.st_mode))
return;
p = sprintf ("%s%c%s", p, path_get_delimiter (), get_slang_load_path ());
set_slang_load_path (p);
}
Some applications may not support the built-in load-path searching facility provided by the underlying library.