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
(((sym1 . val1) (sym2 . val2) ... )))
(setq a 100) ; set up global values (setq b 200) (evalhook '(+ a b) NIL NIL) ; returns 300 - no 'env' was given (evalhook '(+ a b) NIL NIL ; eval with a=1 and b=2 '((((a . 1)(b . 2))))) ; returns 3 (defun myeval (exp env) ; define MYEVAL routine (princ "exp: ") (print exp) (princ "env: ") (print env) (evalhook exp #'myeval NIL env)) (defun foo (a) (+ a a)) ; create simple function (setq *evalhook* #'myeval) ; and install MYEVAL as hook (foo 1) ; prints exp: (FOO 1) env:NIL ; exp: 1 env:NIL ; exp: (+ A A) env:((((A . 1)))) ; exp: A env:((((A . 1)))) ; exp: A env:((((A . 1)))) ; returns 2 (top-level) ; to clean up *evalhook*
Note: The 'evalhook' function and *evalhook* system variable are very useful in the construction of debugging facilities within XLISP. The trace and untrace functions use 'evalhook' and *evalhook* to implement their functionality. The other useful aspect of 'evalhook' and *evalhook* is to help in understanding how XLISP works to see the expressions, their environment and how they are evaluated.
Caution: Be careful when using
*evalhook* and 'evalhook'.
If you put in a bad definition into
Unusual behaviour: The 'evalhook' function and *evalhook* system variable, by their nature, cause some unusual things to happen. After you have set *evalhook* to some non-NIL value, your function will be called. However, when you are all done and set *evalhook* to NIL 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 *evalhook* before calling your routine, and then restores it after the evaluation. The mechanism to reset *evalhook* is to execute the top-level function, which sets *evalhook* to NIL.
See the
evalhook
function in the