File: erl_eval.html

package info (click to toggle)
erlang-doc-html 1%3A11.b.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 23,284 kB
  • ctags: 10,724
  • sloc: erlang: 505; ansic: 323; makefile: 62; perl: 61; sh: 45
file content (348 lines) | stat: -rw-r--r-- 9,955 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
  <TITLE>erl_eval</TITLE>
  <SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
  <STYLE TYPE="text/css">
<!--
    .REFBODY     { margin-left: 13mm }
    .REFTYPES    { margin-left: 8mm }
-->
  </STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
      ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
  <IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>erl_eval</H1>
</CENTER>

<H3>MODULE</H3>
<DIV CLASS=REFBODY>
erl_eval
</DIV>

<H3>MODULE SUMMARY</H3>
<DIV CLASS=REFBODY>
The Erlang Meta Interpreter
</DIV>

<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>

<P>This module provides an interpreter for Erlang expressions. The
expressions are in the abstract syntax as returned by
<CODE>erl_parse</CODE>, the Erlang parser, or a call to
<CODE>io:parse_erl_exprs/2</CODE>.

</DIV>

<H3>EXPORTS</H3>

<P><A NAME="exprs/2"><STRONG><CODE>exprs(Expressions, Bindings) -&#62; {value, Value, NewBindings}</CODE></STRONG></A><BR>
<A NAME="exprs/3"><STRONG><CODE>exprs(Expressions, Bindings, LocalFunctionHandler) -&#62; 
{value, Value, NewBindings}</CODE></STRONG></A><BR>
<A NAME="exprs/4"><STRONG><CODE>exprs(Expressions, Bindings, LocalFunctionHandler, 
NonlocalFunctionHandler) -&#62; {value, Value, NewBindings}</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Expressions = as returned by erl_parse or io:parse_erl_exprs/2</CODE></STRONG><BR>
<STRONG><CODE>Bindings = as returned by bindings/1</CODE></STRONG><BR>
<STRONG><CODE>LocalFunctionHandler = {value, Func} | {eval, Func} | none</CODE></STRONG><BR>
<STRONG><CODE>NonlocalFunctionHandler = {value, Func} | none</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Evaluates <CODE>Expressions</CODE> with the set of bindings
<CODE>Bindings</CODE>, where <CODE>Expressions</CODE> is a sequence of
expressions (in abstract syntax) of a type which may be
returned by <CODE>io:parse_erl_exprs/2</CODE>. See below for an
explanation of how and when to use the arguments
<CODE>LocalFunctionHandler</CODE> and <CODE>NonlocalFunctionHandler</CODE>.


<P>Returns <CODE>{value, Value, NewBindings}</CODE>

</DIV>

<P><A NAME="expr/2"><STRONG><CODE>expr(Expression, Bindings) -&#62; { value, Value, NewBindings }</CODE></STRONG></A><BR>
<A NAME="expr/3"><STRONG><CODE>expr(Expression, Bindings, LocalFunctionHandler) -&#62; 
{ value, Value, NewBindings }</CODE></STRONG></A><BR>
<A NAME="expr/4"><STRONG><CODE>expr(Expression, Bindings, LocalFunctionHandler, 
NonlocalFunctionHandler) -&#62; { value, Value, NewBindings }</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Expression = as returned by io:parse_erl_form/2, for example</CODE></STRONG><BR>
<STRONG><CODE>Bindings = as returned by bindings/1</CODE></STRONG><BR>
<STRONG><CODE>LocalFunctionHandler = {value, Func} | {eval, Func} | none</CODE></STRONG><BR>
<STRONG><CODE>NonlocalFunctionHandler = {value, Func} | none</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Evaluates <CODE>Expression</CODE> with the set of bindings
<CODE>Bindings</CODE>. <CODE>Expression</CODE> is an expression (in
abstract syntax) of a type which may be returned by
<CODE>io:parse_erl_form/2</CODE>. See below for an explanation of
how and when to use the arguments
<CODE>LocalFunctionHandler</CODE> and
<CODE>NonlocalFunctionHandler</CODE>.


<P>Returns <CODE>{value, Value, NewBindings}</CODE>.

</DIV>

<P><A NAME="expr_list/2"><STRONG><CODE>expr_list(ExpressionList, Bindings) -&#62; 
{ValueList, NewBindings}</CODE></STRONG></A><BR>
<A NAME="expr_list/3"><STRONG><CODE>expr_list(ExpressionList, Bindings, LocalFunctionHandler) -&#62; 
{ValueList, NewBindings}</CODE></STRONG></A><BR>
<A NAME="expr_list/4"><STRONG><CODE>expr_list(ExpressionList, Bindings, LocalFunctionHandler, 
NonlocalFunctionHandler) -&#62; {ValueList, NewBindings}</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY>

<P>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 <CODE>LocalFunctionHandler</CODE>. See below.


<P>Returns <CODE>{ValueList, NewBindings}</CODE>.

</DIV>

<P><A NAME="new_bindings/0"><STRONG><CODE>new_bindings() -&#62; BindingStruct</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY>

<P>Returns an empty binding structure.

</DIV>

<P><A NAME="bindings/1"><STRONG><CODE>bindings(BindingStruct) -&#62; Bindings</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY>

<P>Returns the list of bindings contained in the binding
structure.

</DIV>

<P><A NAME="binding/2"><STRONG><CODE>binding(Name, BindingStruct) -&#62; Binding</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY>

<P>Returns the binding of <CODE>Name</CODE> in <CODE>BindingStruct</CODE>.

</DIV>

<P><A NAME="add_binding/3"><STRONG><CODE>add_binding(Name, Value, Bindings) -&#62; BindingStruct</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY>

<P>Adds the binding <CODE>Name = Value</CODE> to <CODE>Bindings</CODE>.
Returns an updated binding structure.

</DIV>

<P><A NAME="del_binding/2"><STRONG><CODE>del_binding(Name, Bindings) -&#62; BindingStruct</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY>

<P>Removes the binding of <CODE>Name</CODE> in <CODE>Bindings</CODE>.
Returns an updated binding structure.

</DIV>

<H3>Local Function Handler</H3>
<DIV CLASS=REFBODY>

<P>During evaluation of a function, no calls can be made to local
functions. An undefined function error would be
generated. However, the optional argument
<CODE>LocalFunctionHandler</CODE> 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:

<P>
<DL>

<DT>
<CODE>{value,Func}</CODE>
</DT>

<DD>
 This defines a local function handler which is called with:<BR>

<PRE>
Func(Name, Arguments)
</PRE>

<CODE>Name</CODE> is the name of the local function (an atom) and
<CODE>Arguments</CODE> is a list of the <STRONG>evaluated</STRONG>
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 <CODE>exit/1</CODE> with a suitable exit value.
<BR>

</DD>

<DT>
<CODE>{eval,Func}</CODE>
</DT>

<DD>
 This defines a local function handler which is called with:<BR>

<PRE>
Func(Name, Arguments, Bindings)
</PRE>

<CODE>Name</CODE> is the name of the local function (an atom),
<CODE>Arguments</CODE> is a list of the <STRONG>unevaluated</STRONG>
arguments, and <CODE>Bindings</CODE> are the current variable
bindings. The function handler returns:<BR>

<PRE>
{value,Value,NewBindings}
</PRE>

<CODE>Value</CODE> is the value of the local function and
<CODE>NewBindings</CODE> 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 <CODE>exit/1</CODE> with a
suitable exit value.
<BR>

</DD>

<DT>
<CODE>none</CODE>
</DT>

<DD>
There is no local function handler.
<BR>

</DD>

</DL>

</DIV>

<H3>Non-local Function Handler</H3>
<DIV CLASS=REFBODY>

<P>The optional argument <CODE>NonlocalFunctionHandler</CODE> 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. Exceptions are function calls in
guard tests and calls to <CODE>erlang:apply/2,3</CODE>; neither of the
function handlers will be called for such calls. 
The argument can have the following formats:

<P>
<DL>

<DT>
<CODE>{value,Func}</CODE>
</DT>

<DD>
This defines an nonlocal function handler which is called with:<BR>

<PRE>
Func(FuncSpec, Arguments)
</PRE>

<CODE>FuncSpec</CODE> is the name of the function on the form
<CODE>{Module,Function}</CODE> or a fun, and <CODE>Arguments</CODE> is a
list of the <STRONG>evaluated</STRONG> arguments. The function
handler returns the value of the function. To
signal an error, the function handler just calls
<CODE>exit/1</CODE> with a suitable exit value.
<BR>

</DD>

<DT>
<CODE>none</CODE>
</DT>

<DD>
There is no nonlocal function handler.
<BR>

</DD>

</DL>

<P>
<TABLE CELLPADDING=4>
  <TR>
    <TD VALIGN=TOP><IMG ALT="Note!" SRC="note.gif"></TD>
    <TD>

<P>For calls such as <CODE>erlang:apply(Fun, Args)</CODE> or
<CODE>erlang:apply(Module, Function, Args)</CODE> the call of the
non-local function handler corresponding to the call to
<CODE>erlang:apply/2,3</CODE> itself--<CODE>Func({erlang, apply}, [Fun,
Args])</CODE> or <CODE>Func({erlang, apply}, [Module, Function,
Args])</CODE>--will never take place. The non-local function
handler <STRONG>will</STRONG> however be called with the evaluated
arguments of the call to <CODE>erlang:apply/2,3</CODE>: <CODE>Func(Fun,
Args)</CODE> or <CODE>Func({Module, Function}, Args)</CODE> (assuming
that <CODE>{Module, Function}</CODE> is not <CODE>{erlang,
apply}</CODE>).    </TD>
  </TR>
</TABLE>

<P>The nonlocal function handler argument is probably not used as
frequently as the local function handler argument. A possible
use is to call <CODE>exit/1</CODE> on calls to functions that for some
reason are not allowed to be called.
</DIV>

<H3>Bugs</H3>
<DIV CLASS=REFBODY>

<P>The evaluator is not complete. <CODE>receive</CODE> cannot be
handled properly.


<P>Any undocumented functions in <CODE>erl_eval</CODE> should not be used.

</DIV>

<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
Robert Virding - support@erlang.ericsson.se<BR>

</DIV>
<CENTER>
<HR>
<SMALL>stdlib 1.14.2<BR>
Copyright &copy; 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>