XLISP evalhook

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# evalhook

Type: | - | function (subr)
Source: | - | xlbfun.c, xleval.c

## Syntax

(evalhook expr eval-expr apply-expr [env])
  expr - an expression to evaluate
  eval-expr - an expression for the evaluation routine
  apply-expr - an expression for apply [not used]
  env - an environment expression, default is NIL
  returns - the result of evaluating the expression

## Description

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 NIL , 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, NIL 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:

    (((sym1 . val1) (sym2 . val2) ... )))

## Examples

    (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 *evalhook* , you might not be able to do anything and will need to exit XLISP.

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 XLISP 2.0 manual.

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference