aboutsummaryrefslogtreecommitdiffhomepage
path: root/libslang/doc/tm/rtl/eval.tm
blob: 3c1e66c3f180933b3aa12b1754040526b096dc33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
\function{autoload}
\synopsis{Load a function from a file}
\usage{autoload (String_Type funct, String_Type file)}
\description
    The \var{autoload} function is used to declare \var{funct} to the
    interpreter and indicate that it should be loaded from \var{file} when
    it is actually used.
\example
    Suppose \var{bessel_j0} is a function defined in the file
    \var{bessel.sl}.  Then the statement
#v+
      autoload ("bessel_j0", "bessel.sl");
#v-
    will cause \var{bessel.sl} to be loaded prior to the execution of
    \var{bessel_j0}
\seealso{evalfile}
\done

\function{byte_compile_file}
\synopsis{Compile a file to byte-code for faster loading.}
\usage{byte_compile_file (String_Type file, Integer_Type method)}
\description
  The \var{byte_compile_file} function byte-compiles \var{file}
  producing a new file with the same name except a \var{'c'} is added
  to the output file name.  For example, \var{file} is
  \exmp{"site.sl"}, then the function produces a new file named
  \exmp{site.slc}.
\notes
  The \var{method} parameter is not used in the current
  implementation.  Its use is reserved for the future.  For now, set
  it to \exmp{0}.
\seealso{evalfile}
\done

\function{eval}
\synopsis{Interpret a string as \slang code}
\usage{eval (String_Type expression, [,String_Type namespace])}
\description
  The \var{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.

  This is a useful function in many contexts such as dynamically
  generating function definitions where there is no way to generate
  them otherwise.
\example
#v+
    if (0 == is_defined ("my_function"))
      eval ("define my_function () { message (\"my_function\"); }");
#v-
\seealso{is_defined, autoload, evalfile}
\done

\function{evalfile}
\synopsis{Interpret a file containing \slang code.}
\usage{Integer_Type evalfile (String_Type file, [,String_Type namespace])}
\description
  The \var{evalfile} function loads \var{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 no errors were encountered, \exmp{1} will
  be returned; otherwise, a \slang error will be generated and the
  function will return zero.
\example
#v+
    define load_file (file)
    {
       ERROR_BLOCK { _clear_error (); }
       () = evalfile (file);
    }
#v-
\notes
  For historical reasons, the return value of this function is not
  really useful.
  
  The file is searched along an application-defined load-path.  The
  \ifun{get_slang_load_path} and \ifun{set_slang_load_path} functions
  may be used to set and query the path.
\seealso{eval, autoload, set_slang_load_path, get_slang_load_path}
\done

\function{get_slang_load_path}
\synopsis{Get the value of the interpreter's load-path}
\usage{String_Type get_slang_load_path ()}
\description
  This function retrieves the value of the delimiter-separated search
  path used for loading files.
\notes
  Some applications may not support the built-in load-path searching
  facility provided by the underlying library.
\seealso{}
\done

\function{set_slang_load_path}
\synopsis{Set the value of the interpreter's load-path}
\usage{set_slang_load_path (String_Type path)}
\description
  This function may be used to set the value of the
  delimiter-separated search path used by the \ifun{evalfile} and
  \ifun{autoload} functions for locating files.
\example
#v+
    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;

       variable d = path_get_delimiter ();
       set_slang_load_path (strcat (p, d, get_slang_load_path ()));
    }
#v-
\notes
  Some applications may not support the built-in load-path searching
  facility provided by the underlying library.
\seealso{get_slang_load_path, path_get_delimiter, evalfile, autoload}
\done