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
|
<html><head><title>XLISP evalhook</title>
<link rel="stylesheet" type="text/css" href="reference.css">
</head>
<body>
<a href="../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>
<hr>
<h1>evalhook</h1>
<hr>
<p><table cellpadding="0" cellspacing="0" style="margin-left:10px"><tbody>
<tr valign="top">
<td><nobr>Type:</nobr></td>
<td><nobr> - </nobr></td>
<td width="100%"><nobr>function (subr)</nobr></td>
</tr>
<tr valign="top">
<td><nobr>Source:</nobr></td>
<td><nobr> - </nobr></td>
<td width="100%"><nobr>xlbfun.c, xleval.c</nobr></td>
</tr>
</tbody></table></p>
<h2>Syntax</h2>
<dl>
<dt>(evalhook<i> expr eval-expr apply-expr</i> [<i>env</i>])</dt>
<dd><i>expr</i> - an expression to evaluate<br>
<i>eval-expr</i> - an expression for the evaluation routine<br>
<i>apply-expr</i> - an expression for
<a href="apply.htm">apply</a> [not used]<br>
<i>env</i> - an environment expression, default is
<a href="nil.htm">NIL</a><br>
returns - the result of evaluating the expression</dd>
</dl>
<h2>Description</h2>
<p>The 'evalhook' function is a function that performs evaluation.
The routine specified by 'eval-expr' is called with the 'expr' and 'env'
parameters. If 'eval-expr' is
<nobr><a href="nil.htm">NIL</a> ,</nobr> then the normal system
evaluator is called. The 'apply-hook' is a dummy parameter that is not used
in the current XLISP system. The 'expr' contains the expression to be
evaluated. If the 'env' argument to 'evalhook' is not specified,
<a href="nil.htm">NIL</a> is used, which specifies to use the
current global environment. The 'env', if specified, is a structure composed
of dotted pairs constructed of the symbol and its value which have the
form:</p>
<pre class="example">
(((<font color="#008844"><i>sym1</i></font> . <font color="#008844"><i>val1</i></font>) (<font color="#008844"><i>sym2</i></font> . <font color="#008844"><i>val2</i></font>) ... )))
</pre>
<h2>Examples</h2>
<pre class="example">
(setq a 100) <font color="#008844">; set up global values</font>
(setq b 200)
(evalhook '(+ a b) NIL NIL) <font color="#008844">; returns 300 - no 'env' was given</font>
(evalhook '(+ a b) NIL NIL <font color="#008844">; eval with a=1 and b=2</font>
'((((a . 1)(b . 2))))) <font color="#008844">; returns 3</font>
(defun myeval (exp env) <font color="#008844">; define MYEVAL routine</font>
(princ "exp: ") (print exp)
(princ "env: ") (print env)
(evalhook exp #'myeval NIL env))
(defun foo (a) (+ a a)) <font color="#008844">; create simple function</font>
(setq *evalhook* #'myeval) <font color="#008844">; and install MYEVAL as hook</font>
(foo 1) <font color="#008844">; prints exp: (FOO 1) env:NIL</font>
<font color="#008844">; exp: 1 env:NIL</font>
<font color="#008844">; exp: (+ A A) env:((((A . 1))))</font>
<font color="#008844">; exp: A env:((((A . 1))))</font>
<font color="#008844">; exp: A env:((((A . 1))))</font>
<font color="#008844">; returns 2</font>
(top-level) <font color="#008844">; to clean up *evalhook*</font>
</pre>
<p><b>Note:</b> The 'evalhook' function and
<a href="global-evalhook.htm">*evalhook*</a> system variable are very useful
in the construction of debugging facilities within XLISP. The
<a href="trace.htm">trace</a> and
<a href="untrace.htm">untrace</a> functions use 'evalhook' and
<a href="global-evalhook.htm">*evalhook*</a> to implement their
functionality. The other useful aspect of 'evalhook' and
<a href="global-evalhook.htm">*evalhook*</a> is to help in understanding
how XLISP works to see the expressions, their environment and how they
are evaluated.</p>
<p><b>Caution:</b> Be careful when using
<a href="global-evalhook.htm">*evalhook*</a> and 'evalhook'.
If you put in a bad definition into
<nobr><a href="global-evalhook.htm">*evalhook*</a> ,</nobr> you might not
be able to do anything and will need to exit XLISP.</p>
<p><b>Unusual behaviour:</b> The 'evalhook' function and
<a href="global-evalhook.htm">*evalhook*</a> system variable, by their
nature, cause some unusual things to happen. After you have set
<a href="global-evalhook.htm">*evalhook*</a> to some
non-<a href="nil.htm">NIL</a> value, your function will be
called. However, when you are all done and set
<a href="global-evalhook.htm">*evalhook*</a> to
<a href="nil.htm">NIL</a> or some other new routine, it will
never be set. This is because the 'xevalhook' function [in the 'xlbfun.c'
source file] saves the old value of
<a href="global-evalhook.htm">*evalhook*</a> before calling your routine,
and then restores it after the evaluation. The mechanism to reset
<a href="global-evalhook.htm">*evalhook*</a> is to execute the
<a href="top-level.htm">top-level</a> function, which sets
<a href="global-evalhook.htm">*evalhook*</a> to
<a href="nil.htm">NIL</a>.</p>
<p>See the
<a href="../manual/xlisp-man-022.htm#evalhook">evalhook</a>
function in the <nobr>XLISP 2.0</nobr> manual.</p>
<p><nobr> <a href="#top">Back to Top</nobr></a></p>
<hr>
<a href="../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>
</body></html>
|