[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

eval-while-possible



I've found the following function to be of general use when you're uncertain as to
how many evaluations are necessary, or just don't want to be bothered
in figuring it out. My own application for it is for a language
embedded in lisp, where the user is going to be calling functions in the language
with arguments that may be symbols, whose values are other expressions which should be
evaled, possibly more than once.

(defun eval-while-possible (expression &optional (instance nil) &aux values)
  "Evals expression then evals the result of the previous evaluation, and so on
   until error, or until evaluation returns a value eq to a previous value,
    then returns the version of expression
   just before the error.
   If INSTANCE is non-nil, then each evaluation
   is performed in the context of the instance. "
  (setq values (list expression))
  (loop	do
	(multiple-value-bind (the-value error?)
	    (ignore-errors
	      (cond (instance
		     (send instance :eval-inside-yourself (car values)))
		    (t (eval (car values)))))
	  (cond ((or error? (member the-value values))
		 (return (car values)))
		(t (push the-value values))))))