erl_eval
The Erlang Meta Interpreter
This module provides an interpreter for Erlang expressions. The
expressions are in the abstract syntax as returned by
erl_parse
,
the Erlang parser, or
io
.
A binding structure.
As returned by
erl_parse:parse_exprs/1
or
io:parse_erl_exprs/2
.
Further described below.
Further described below.
Functions
exprs/2
exprs/3
exprs/4
Evaluates
with the set of bindings
, where
is a sequence of
expressions (in abstract syntax) of a type which may be
returned by
io:parse_erl_exprs/2
. See below for an
explanation of how and when to use the arguments
and
.
Returns {value,
expr/2
expr/3
expr/4
expr/5
Evaluates
with the set of bindings
.
is an expression in
abstract syntax. See below for an explanation of
how and when to use the arguments
and
.
Returns {value,
by default. But if the
is value
only
the
is returned.
expr_list/2
expr_list/3
expr_list/4
Evaluates a list of expressions in parallel, using the same
initial bindings for each expression. Attempts are made to
merge the bindings returned from each evaluation. This
function is useful in the LocalFunctionHandler
. See below.
Returns {
.
new_bindings/0
Returns an empty binding structure.
bindings/1
Returns the list of bindings contained in the binding structure.
binding/2
Returns the binding of
in
.
add_binding/3
Adds the binding
to
.
Returns an updated binding structure.
del_binding/2
Removes the binding of
in
.
Returns an updated binding structure.
Local Function Handler
During evaluation of a function, no calls can be made to local
functions. An undefined function error would be
generated. However, the optional argument
LocalFunctionHandler
may be used to define a function
which is called when there is a call to a local function. The
argument can have the following formats:
{value,Func}
This defines a local function handler which is called with:
Func(Name, Arguments)
Name
is the name of the local function (an atom) and
Arguments
is a list of the evaluated
arguments. The function handler returns the value of the
local function. In this case, it is not possible to access
the current bindings. To signal an error, the function
handler just calls exit/1
with a suitable exit value.
{eval,Func}
This defines a local function handler which is called with:
Func(Name, Arguments, Bindings)
Name
is the name of the local function (an atom),
Arguments
is a list of the unevaluated
arguments, and Bindings
are the current variable
bindings. The function handler returns:
{value,Value,NewBindings}
Value
is the value of the local function and
NewBindings
are the updated variable bindings. In
this case, the function handler must itself evaluate all the
function arguments and manage the bindings. To signal an
error, the function handler just calls exit/1
with a
suitable exit value.
none
There is no local function handler.
Non-local Function Handler
The optional argument NonlocalFunctionHandler
may be
used to define a function which is called in the following
cases: a functional object (fun) is called; a built-in function
is called; a function is called using the M:F syntax, where M
and F are atoms or expressions; an operator Op/A is called
(this is handled as a call to the function erlang:Op/A
).
Exceptions are calls to erlang:apply/2,3
; neither of the
function handlers will be called for such calls.
The argument can have the following formats:
{value,Func}
This defines an nonlocal function handler which is called with:
Func(FuncSpec, Arguments)
FuncSpec
is the name of the function on the form
{Module,Function}
or a fun, and Arguments
is a
list of the evaluated arguments. The function
handler returns the value of the function. To
signal an error, the function handler just calls
exit/1
with a suitable exit value.
none
There is no nonlocal function handler.
Note!
For calls such as erlang:apply(Fun, Args)
or
erlang:apply(Module, Function, Args)
the call of the
non-local function handler corresponding to the call to
erlang:apply/2,3
itself--Func({erlang, apply}, [Fun, Args])
or Func({erlang, apply}, [Module, Function, Args])
--will never take place. The non-local function
handler will however be called with the evaluated
arguments of the call to erlang:apply/2,3
: Func(Fun, Args)
or Func({Module, Function}, Args)
(assuming
that {Module, Function}
is not {erlang, apply}
).
Calls to functions defined by evaluating fun expressions
"fun ... end"
are also hidden from non-local function
handlers.
The nonlocal function handler argument is probably not used as
frequently as the local function handler argument. A possible
use is to call exit/1
on calls to functions that for some
reason are not allowed to be called.
Bugs
Undocumented functions in erl_eval
should not be used.