1 ;;;; An interpreting EVAL
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!EVAL")
14 ;; (declaim (optimize (speed 3) (debug 1) (safety 1)))
16 ;;; Values used for marking specials/macros/etc in environments.
17 (defvar *special* (gensym "SPECIAL"))
18 (defvar *macro* (gensym "MACRO"))
19 (defvar *symbol-macro* (gensym "SYMBOL-MACRO"))
20 (defvar *not-present* (gensym "NOT-PRESENT"))
22 (define-condition interpreted-program-error (program-error simple-condition sb!impl::encapsulated-condition)
24 (:report (lambda (condition stream)
25 (if (slot-boundp condition 'condition)
27 (format stream "Error evaluating a form:~% ~A"
28 (sb!impl::encapsulated-condition condition)))
29 (format stream "Error evaluating a form:~% ~?"
30 (simple-condition-format-control condition)
31 (simple-condition-format-arguments condition))))))
33 ;;; ANSI defines that program syntax errors should be of type
34 ;;; PROGRAM-ERROR. Therefore...
35 (define-condition arg-count-program-error (sb!kernel::arg-count-error
39 (defun arg-count-program-error (datum &rest arguments)
40 (declare (ignore datum))
41 (apply #'error 'arg-count-program-error arguments))
43 ;; OAOOM? (see destructuring-bind.lisp)
44 (defmacro program-destructuring-bind (lambda-list arg-list &body body)
45 (let ((arg-list-name (gensym "ARG-LIST-")))
46 (multiple-value-bind (body local-decls)
47 (sb!kernel:parse-defmacro lambda-list arg-list-name body nil
48 'program-destructuring-bind
50 :doc-string-allowed nil
52 :error-fun 'arg-count-program-error)
53 `(let ((,arg-list-name ,arg-list))
57 (defun ip-error (format-control &rest format-arguments)
58 (error 'interpreted-program-error
59 :format-control format-control
60 :format-arguments format-arguments))
62 (defmacro nconc-2 (a b)
68 (progn (setf (cdr (last ,tmp)) ,tmp2) ,tmp)
71 ;;; Construct a compiler LEXENV from the same data that's used for
72 ;;; creating an interpreter ENV. This is needed for example when
73 ;;; passing the environment to macroexpanders or when compiling an
74 ;;; interpreted function.
75 (defun fabricate-new-native-environment (old-lexenv new-funs new-expanders
76 new-vars new-symbol-expansions
78 (labels ((to-native-funs (binding)
79 ;; Non-macroexpander function entries are irrelevant for
80 ;; the LEXENV. If we're using the LEXENV for
81 ;; macro-expansion any references to local non-macro
82 ;; function bindings are undefined behaviour. If we're
83 ;; compiling an interpreted function, a lexical environment
84 ;; with non-macro functions will be too hairy to compile.
85 (if (eq (cdr binding) *macro*)
88 (cdr (assoc (car binding) new-expanders))))
91 (to-native-vars (binding)
92 ;; And likewise for symbol macros.
93 (if (eq (cdr binding) *symbol-macro*)
96 (cdr (assoc (car binding) new-symbol-expansions))))
99 (let ((lexenv (sb!c::internal-make-lexenv
100 (nconc-2 (mapcar #'to-native-funs new-funs)
101 (sb!c::lexenv-funs old-lexenv))
102 (nconc-2 (mapcar #'to-native-vars new-vars)
103 (sb!c::lexenv-vars old-lexenv))
105 (sb!c::lexenv-handled-conditions old-lexenv)
106 (sb!c::lexenv-disabled-package-locks old-lexenv)
107 (sb!c::lexenv-policy old-lexenv))))
108 (dolist (declaration declarations)
109 (unless (consp declaration)
110 (ip-error "malformed declaration specifier ~S in ~S"
111 declaration (cons 'declare declarations)))
112 (case (car declaration)
114 (dolist (element (cdr declaration))
115 (multiple-value-bind (quality value)
116 (if (not (consp element))
118 (program-destructuring-bind (quality value)
120 (values quality value)))
121 (if (sb!c::policy-quality-name-p quality)
122 (push (cons quality value)
123 (sb!c::lexenv-%policy lexenv))
124 (warn "ignoring unknown optimization quality ~
126 (cons 'declare declarations))))))
127 (sb!ext:muffle-conditions
128 (setf (sb!c::lexenv-handled-conditions lexenv)
129 (sb!c::process-muffle-conditions-decl
131 (sb!c::lexenv-handled-conditions lexenv))))
132 (sb!ext:unmuffle-conditions
133 (setf (sb!c::lexenv-handled-conditions lexenv)
134 (sb!c::process-unmuffle-conditions-decl
136 (sb!c::lexenv-handled-conditions lexenv))))
137 ((sb!ext:disable-package-locks sb!ext:enable-package-locks)
138 (setf (sb!c::lexenv-disabled-package-locks lexenv)
139 (sb!c::process-package-lock-decl
141 (sb!c::lexenv-disabled-package-locks lexenv))))))
145 (:constructor %make-env
146 (parent vars funs expanders symbol-expansions
147 tags blocks declarations native-lexenv)))
158 (defun make-env (&key parent vars funs expanders
159 symbol-expansions tags blocks declarations)
161 (append vars (env-vars parent))
162 (append funs (env-funs parent))
163 (append expanders (env-expanders parent))
164 (append symbol-expansions (env-symbol-expansions parent))
165 (nconc-2 tags (env-tags parent))
166 (nconc-2 blocks (env-blocks parent))
168 (fabricate-new-native-environment (env-native-lexenv parent)
170 vars symbol-expansions
173 (defun make-null-environment ()
174 (%make-env nil nil nil nil nil nil nil nil
175 (sb!c::internal-make-lexenv
177 nil nil nil nil nil nil nil
180 ;;; Augment ENV with a special or lexical variable binding
181 (declaim (inline push-var))
182 (defun push-var (name value env)
183 (push (cons name value) (env-vars env))
184 (push (cons name :bogus) (sb!c::lexenv-vars (env-native-lexenv env))))
186 ;;; Augment ENV with a local function binding
187 (declaim (inline push-fun))
188 (defun push-fun (name value calling-env body-env)
190 (let ((sb!c:*lexenv* (env-native-lexenv calling-env)))
191 (program-assert-symbol-home-package-unlocked
192 :eval name "binding ~A as a local function")))
193 (push (cons name value) (env-funs body-env))
194 (push (cons name :bogus) (sb!c::lexenv-funs (env-native-lexenv body-env))))
196 (sb!int:def!method print-object ((env env) stream)
197 (print-unreadable-object (env stream :type t :identity t)))
199 (macrolet ((define-get-binding (name accessor &key (test '#'eq))
200 ;; A macro, sadly, because an inline function here is
202 `(defmacro ,name (symbol env)
203 `(assoc ,symbol (,',accessor ,env) :test ,',test))))
204 (define-get-binding get-binding env-vars)
205 (define-get-binding get-fbinding env-funs :test #'equal)
206 (define-get-binding get-expander-binding env-expanders)
207 (define-get-binding get-symbol-expansion-binding env-symbol-expansions)
208 (define-get-binding get-tag-binding env-tags :test #'eql)
209 (define-get-binding get-block-binding env-blocks))
211 ;;; Return a list of all symbols that are declared special in the
212 ;;; declarations listen in DECLS.
213 (defun declared-specials (decls)
214 (let ((specials nil))
216 (when (eql (car decl) 'special)
217 (dolist (var (cdr decl))
218 (push var specials))))
221 ;;; Given a list of variables that should be marked as special in an
222 ;;; environment, return the appropriate binding forms to be given
224 (defun special-bindings (specials env)
225 (mapcar #'(lambda (var)
226 (let ((sb!c:*lexenv* (env-native-lexenv env)))
227 (program-assert-symbol-home-package-unlocked
228 :eval var "declaring ~A special"))
229 (cons var *special*))
232 ;;; Return true if SYMBOL has been declared special either globally
233 ;;; or is in the DECLARED-SPECIALS list.
234 (defun specialp (symbol declared-specials)
235 (let ((type (sb!int:info :variable :kind symbol)))
238 ;; Horrible place for this, but it works.
239 (ip-error "Can't bind constant symbol: ~S" symbol))
242 (ip-error "Can't bind a global variable: ~S" symbol))
243 ((eq type :special) t)
244 ((member symbol declared-specials :test #'eq)
248 (defun binding-name (binding)
249 (if (consp binding) (first binding) binding))
250 (defun binding-value (binding)
251 (if (consp binding) (second binding) nil))
252 (defun supplied-p-parameter (spec)
253 (if (consp spec) (third spec) nil))
254 (defun keyword-name (spec)
256 (if (consp (first spec))
257 (second (first spec))
260 (defun keyword-key (spec)
262 (if (consp (first spec))
264 (intern (symbol-name (first spec)) "KEYWORD"))
265 (intern (symbol-name spec) "KEYWORD")))
266 (defun keyword-default-value (spec)
267 (if (consp spec) (second spec) nil))
269 ;;; Given a list of ARGUMENTS and a LAMBDA-LIST, return two values:
270 ;;; * An alist[*] mapping the required parameters of the function to
271 ;;; the corresponding argument values
272 ;;; * An alist mapping the keyword, optional and rest parameters of
273 ;;; the function to the corresponding argument values (if supplied)
274 ;;; or to the parameter's default expression (if not). Supplied-p
275 ;;; parameters and aux variables are handled in a similar manner.
277 ;;; For example given the argument list of (1 2) and the lambda-list of
278 ;;; (A &OPTIONAL (B A) (C (1+ A))), we'd return the values
279 ;;; (A . '1) and ((B . '2) (C . (1+ A))).
281 ;;; Used only for implementing calls to interpreted functions.
282 (defun parse-arguments (arguments lambda-list)
283 (multiple-value-bind (required optional rest-p rest keyword-p
284 keyword allow-other-keys-p aux-p aux)
285 (handler-bind ((style-warning #'muffle-warning))
286 (sb!int:parse-lambda-list lambda-list))
287 (let* ((original-arguments arguments)
288 (arguments-present (length arguments))
289 (required-length (length required))
290 (optional-length (length optional))
291 (non-keyword-arguments (+ required-length optional-length))
292 (optionals-present (- (min non-keyword-arguments arguments-present)
294 (keywords-present-p (> arguments-present non-keyword-arguments))
295 (let-like-bindings nil)
296 (let*-like-bindings nil))
298 ((< arguments-present required-length)
299 (ip-error "~@<Too few arguments in ~S to satisfy lambda list ~S.~:@>"
300 arguments lambda-list))
301 ((and (not (or rest-p keyword-p)) keywords-present-p)
302 (ip-error "~@<Too many arguments in ~S to satisfy lambda list ~S.~:@>"
303 arguments lambda-list))
304 ((and keyword-p keywords-present-p
305 (oddp (- arguments-present non-keyword-arguments)))
306 (ip-error "~@<Odd number of &KEY arguments in ~S for ~S.~:@>"
307 arguments lambda-list)))
308 (dotimes (i required-length)
309 (push (cons (pop required) (pop arguments)) let-like-bindings))
310 (do ((optionals-parsed 0 (1+ optionals-parsed)))
312 (let ((this-optional (pop optional))
313 (supplied-p (< optionals-parsed optionals-present)))
314 (push (cons (binding-name this-optional)
316 (list 'quote (pop arguments))
317 (binding-value this-optional)))
319 (when (supplied-p-parameter this-optional)
320 (push (cons (supplied-p-parameter this-optional)
321 (list 'quote supplied-p))
322 let*-like-bindings))))
323 (let ((keyword-plist arguments))
325 (push (cons rest (list 'quote keyword-plist)) let*-like-bindings))
327 (unless (or allow-other-keys-p
328 (getf keyword-plist :allow-other-keys))
329 (loop for (key value) on keyword-plist by #'cddr doing
330 (when (and (not (eq key :allow-other-keys))
331 (not (member key keyword :key #'keyword-key)))
332 (ip-error "~@<Unknown &KEY argument ~S in ~S for ~S.~:@>"
333 key original-arguments lambda-list))))
334 (dolist (keyword-spec keyword)
335 (let ((supplied (getf keyword-plist (keyword-key keyword-spec)
337 (push (cons (keyword-name keyword-spec)
338 (if (eq supplied *not-present*)
339 (keyword-default-value keyword-spec)
340 (list 'quote supplied)))
342 (when (supplied-p-parameter keyword-spec)
343 (push (cons (supplied-p-parameter keyword-spec)
344 (list 'quote (not (eq supplied *not-present*))))
345 let*-like-bindings))))))
349 (let ((this-aux (pop aux)))
350 (push (cons (binding-name this-aux)
351 (binding-value this-aux))
352 let*-like-bindings))))
353 (values (nreverse let-like-bindings) (nreverse let*-like-bindings)))))
355 ;;; Evaluate LET*-like (sequential) bindings.
357 ;;; Given an alist of BINDINGS, evaluate the value form of the first
358 ;;; binding in ENV, bind the variable to the value in ENV, and then
359 ;;; evaluate the next binding form. Once all binding forms have been
360 ;;; handled, END-ACTION is funcalled.
362 ;;; SPECIALS is a list of variables that have a bound special declaration.
363 ;;; These variables (and those that have been declaimed as special) are
364 ;;; bound as special variables.
365 (defun eval-next-let*-binding (bindings specials env end-action)
366 (flet ((maybe-eval (exp)
367 ;; Pick off the easy (QUOTE x) case which is very common
368 ;; due to function calls. (see PARSE-ARGUMENTS)
369 (if (and (consp exp) (eq (car exp) 'quote))
373 (let* ((binding-name (car (car bindings)))
374 (binding-value (cdr (car bindings))))
375 (if (specialp binding-name specials)
378 (list (maybe-eval binding-value))
379 ;; Mark the variable as special in this environment
380 (push-var binding-name *special* env)
381 (eval-next-let*-binding (cdr bindings)
382 specials env end-action))
384 (push-var binding-name (maybe-eval binding-value) env)
385 (eval-next-let*-binding (cdr bindings)
386 specials env end-action))))
387 (funcall end-action))))
389 ;;; Create a new environment based on OLD-ENV by adding the variable
390 ;;; bindings in BINDINGS to it, and call FUNCTION with the new environment
391 ;;; as the only parameter. DECLARATIONS are the declarations that were
392 ;;; in a source position where bound declarations for the bindings could
395 ;;; FREE-SPECIALS-P controls whether all special declarations should
396 ;;; end cause the variables to be marked as special in the environment
397 ;;; (when true), or only bound declarations (when false). Basically
398 ;;; it'll be T when handling a LET, and NIL when handling a call to an
399 ;;; interpreted function.
400 (defun call-with-new-env (old-env bindings declarations
401 free-specials-p function)
402 (let* ((specials (declared-specials declarations))
404 (dynamic-values nil))
405 ;; To check for package-lock violations
406 (special-bindings specials old-env)
407 (flet ((generate-binding (binding)
408 (if (specialp (car binding) specials)
409 ;; If the variable being bound is globally special or
410 ;; there's a bound special declaration for it, record it
411 ;; in DYNAMIC-VARS / -VALUES separately:
412 ;; * To handle the case of FREE-SPECIALS-P == T more
414 ;; * The dynamic variables will be bound with PROGV just
417 (push (car binding) dynamic-vars)
418 (push (cdr binding) dynamic-values)
420 ;; Otherwise it's a lexical binding, and the value
421 ;; will be recorded in the environment.
423 (let ((new-env (make-env
425 :vars (mapcan #'generate-binding bindings)
426 :declarations declarations)))
427 (dolist (special (if free-specials-p specials dynamic-vars))
428 (push-var special *special* new-env))
430 (progv dynamic-vars dynamic-values
431 (funcall function new-env))
432 ;; When there are no specials, the PROGV would be a no-op,
433 ;; but it's better to elide it completely, since the
434 ;; funcall is then in tail position.
435 (funcall function new-env))))))
437 ;;; Create a new environment based on OLD-ENV by binding the argument
438 ;;; list ARGUMENTS to LAMBDA-LIST, and call FUNCTION with the new
439 ;;; environment as argument. DECLARATIONS are the declarations that
440 ;;; were in a source position where bound declarations for the
441 ;;; bindings could be introduced.
442 (defun call-with-new-env-full-parsing
443 (old-env lambda-list arguments declarations function)
444 (multiple-value-bind (let-like-bindings let*-like-binding)
445 (parse-arguments arguments lambda-list)
446 (let ((specials (declared-specials declarations))
447 var-specials free-specials)
448 ;; Separate the bound and free special declarations
449 (dolist (special specials)
450 (if (or (member special let-like-bindings :key #'car)
451 (member special let*-like-binding :key #'car))
452 (push special var-specials)
453 (push special free-specials)))
454 ;; First introduce the required parameters into the environment
455 ;; with CALL-WITH-NEW-ENV
457 old-env let-like-bindings declarations nil
459 ;; Then deal with optionals / keywords / etc.
460 (eval-next-let*-binding
461 let*-like-binding var-specials env
463 ;; And now that we have evaluated all the
464 ;; initialization forms for the bindings, add the free
465 ;; special declarations to the environment. To see why
466 ;; this is the right thing to do (instead of passing
467 ;; FREE-SPECIALS-P == T to CALL-WITH-NEW-ENV),
470 ;; (eval '(let ((*a* 1))
471 ;; (declare (special *a*))
473 ;; (funcall (lambda (&optional (b *a*))
474 ;; (declare (special *a*))
475 ;; (values b *a*))))))
477 ;; *A* should be special in the body of the lambda, but
478 ;; not when evaluating the default value of B.
479 (dolist (special free-specials)
480 (push-var special *special* env))
481 (funcall function env))))))))
483 ;;; Set the VALUE of the binding (either lexical or special) of the
484 ;;; variable named by SYMBOL in the environment ENV.
485 (defun set-variable (symbol value env)
486 (let ((binding (get-binding symbol env)))
489 ((eq (cdr binding) *special*)
490 (setf (symbol-value symbol) value))
491 ((eq (cdr binding) *symbol-macro*)
492 (error "Tried to set a symbol-macrolet!"))
493 (t (setf (cdr binding) value)))
494 (case (sb!int:info :variable :kind symbol)
495 (:macro (error "Tried to set a symbol-macrolet!"))
496 (:alien (let ((type (sb!int:info :variable :alien-info symbol)))
497 (setf (sb!alien::%heap-alien type) value)))
499 (let ((type (sb!c::info :variable :type symbol)))
501 (let ((type-specifier (sb!kernel:type-specifier type)))
502 (unless (typep value type-specifier)
505 :expected-type type-specifier))))
506 (setf (symbol-value symbol) value)))))))
508 ;;; Retrieve the value of the binding (either lexical or special) of
509 ;;; the variable named by SYMBOL in the environment ENV. For symbol
510 ;;; macros the expansion is returned instead.
511 (defun get-variable (symbol env)
512 (let ((binding (get-binding symbol env)))
515 ((eq (cdr binding) *special*)
516 (values (symbol-value symbol) :variable))
517 ((eq (cdr binding) *symbol-macro*)
518 (values (cdr (get-symbol-expansion-binding symbol env))
520 (t (values (cdr binding) :variable)))
521 (case (sb!int:info :variable :kind symbol)
522 (:macro (values (macroexpand-1 symbol) :expansion))
523 (:alien (let ((type (sb!int:info :variable :alien-info symbol)))
524 (values (sb!alien::%heap-alien type)
526 (t (values (symbol-value symbol) :variable))))))
528 ;;; Retrieve the function/macro binding of the symbol NAME in
529 ;;; environment ENV. The second return value will be :MACRO for macro
530 ;;; bindings, :FUNCTION for function bindings.
531 (defun get-function (name env)
532 (let ((binding (get-fbinding name env)))
535 ((eq (cdr binding) *macro*)
536 (values (cdr (get-expander-binding name env)) :macro))
537 (t (values (cdr binding) :function)))
539 ((and (symbolp name) (macro-function name))
540 (values (macro-function name) :macro))
541 (t (values (%coerce-name-to-fun name) :function))))))
543 ;;; Return true if EXP is a lambda form.
545 (case (car exp) ((lambda
547 sb!kernel:instance-lambda)
550 ;;; Split off the declarations (and the docstring, if
551 ;;; DOC-STRING-ALLOWED is true) from the actual forms of BODY.
552 ;;; Returns three values: the cons in BODY containing the first
553 ;;; non-header subform, the docstring, and a list of the declarations.
555 ;;; FIXME: The name of this function is somewhat misleading. It's not
556 ;;; used just for parsing the headers from lambda bodies, but for all
557 ;;; special forms that have attached declarations.
558 (defun parse-lambda-headers (body &key doc-string-allowed)
559 (loop with documentation = nil
560 with declarations = nil
563 ((and doc-string-allowed (stringp (car form)))
564 (if (cdr form) ; CLHS 3.4.11
566 (ip-error "~@<Duplicate doc string ~S.~:@>" (car form))
567 (setf documentation (car form)))
568 (return (values form documentation declarations))))
569 ((and (consp (car form)) (eql (caar form) 'declare))
570 (setf declarations (append declarations (cdar form))))
571 (t (return (values form documentation declarations))))
572 finally (return (values nil documentation declarations))))
574 ;;; Create an interpreted function from the lambda-form EXP evaluated
575 ;;; in the environment ENV.
576 (defun eval-lambda (exp env)
578 ((lambda sb!kernel:instance-lambda)
579 (multiple-value-bind (body documentation declarations)
580 (parse-lambda-headers (cddr exp) :doc-string-allowed t)
581 (make-interpreted-function :lambda-list (second exp)
583 :documentation documentation
584 :source-location (sb!c::make-definition-source-location)
585 :declarations declarations)))
586 ((sb!int:named-lambda)
587 (multiple-value-bind (body documentation declarations)
588 (parse-lambda-headers (cdddr exp) :doc-string-allowed t)
589 (make-interpreted-function :name (second exp)
590 :lambda-list (third exp)
592 :documentation documentation
593 :source-location (sb!c::make-definition-source-location)
594 :declarations declarations)))))
596 (defun eval-progn (body env)
597 (let ((previous-exp nil))
600 (%eval previous-exp env))
601 (setf previous-exp exp))
602 ;; Preserve tail call
603 (%eval previous-exp env)))
605 (defun eval-if (body env)
606 (program-destructuring-bind (test if-true &optional if-false) body
609 (%eval if-false env))))
611 (defun eval-let (body env)
612 (program-destructuring-bind (bindings &body body) body
613 ;; First evaluate the bindings in parallel
614 (let ((bindings (mapcar
616 (cons (binding-name binding)
617 (%eval (binding-value binding) env)))
619 (multiple-value-bind (body documentation declarations)
620 (parse-lambda-headers body :doc-string-allowed nil)
621 (declare (ignore documentation))
622 ;; Then establish them into the environment, and evaluate the
624 (call-with-new-env env bindings declarations t
626 (eval-progn body env)))))))
628 (defun eval-let* (body old-env)
629 (program-destructuring-bind (bindings &body body) body
630 (multiple-value-bind (body documentation declarations)
631 (parse-lambda-headers body :doc-string-allowed nil)
632 (declare (ignore documentation))
633 ;; First we separate the special declarations into bound and
634 ;; free declarations.
635 (let ((specials (declared-specials declarations))
636 var-specials free-specials)
637 (dolist (special specials)
638 (if (member special bindings :key #'binding-name)
639 (push special var-specials)
640 (push special free-specials)))
641 (let ((env (make-env :parent old-env
642 :declarations declarations)))
643 ;; Then we establish the bindings into the environment
645 (eval-next-let*-binding
646 (mapcar #'(lambda (binding)
647 (cons (binding-name binding)
648 (binding-value binding)))
652 ;; Now that we're done evaluating the bindings, add the
653 ;; free special declarations. See also
654 ;; CALL-WITH-NEW-ENV-FULL-PARSING.
655 (dolist (special free-specials)
656 (push-var special *special* env))
657 (eval-progn body env))))))))
659 ;; Return a named local function in the environment ENV, made from the
660 ;; definition form FUNCTION-DEF.
661 (defun eval-local-function-def (function-def env)
662 (program-destructuring-bind (name lambda-list &body local-body) function-def
663 (multiple-value-bind (local-body documentation declarations)
664 (parse-lambda-headers local-body :doc-string-allowed t)
665 (%eval `#'(sb!int:named-lambda ,name ,lambda-list
669 (declare ,@declarations)
670 (block ,(cond ((consp name) (second name))
675 (defun eval-flet (body env)
676 (program-destructuring-bind ((&rest local-functions) &body body) body
677 (multiple-value-bind (body documentation declarations)
678 (parse-lambda-headers body :doc-string-allowed nil)
679 (declare (ignore documentation))
680 (let* ((specials (declared-specials declarations))
681 (new-env (make-env :parent env
682 :vars (special-bindings specials env)
683 :declarations declarations)))
684 (dolist (function-def local-functions)
685 (push-fun (car function-def)
686 ;; Evaluate the function definitions in ENV.
687 (eval-local-function-def function-def env)
688 ;; Do package-lock checks in ENV.
690 ;; But add the bindings to the child environment.
692 (eval-progn body new-env)))))
694 (defun eval-labels (body old-env)
695 (program-destructuring-bind ((&rest local-functions) &body body) body
696 (multiple-value-bind (body documentation declarations)
697 (parse-lambda-headers body :doc-string-allowed nil)
698 (declare (ignore documentation))
699 ;; Create a child environment, evaluate the function definitions
700 ;; in it, and add them into the same environment.
701 (let ((env (make-env :parent old-env
702 :declarations declarations)))
703 (dolist (function-def local-functions)
704 (push-fun (car function-def)
705 (eval-local-function-def function-def env)
708 ;; And then add an environment for the body of the LABELS. A
709 ;; separate environment from the one where we added the
710 ;; functions to is needed, since any special variable
711 ;; declarations need to be in effect in the body, but not in
712 ;; the bodies of the local functions.
713 (let* ((specials (declared-specials declarations))
714 (new-env (make-env :parent env
715 :vars (special-bindings specials env))))
716 (eval-progn body new-env))))))
718 ;; Return a local macro-expander in the environment ENV, made from the
719 ;; definition form FUNCTION-DEF.
720 (defun eval-local-macro-def (function-def env)
721 (program-destructuring-bind (name lambda-list &body local-body) function-def
722 (multiple-value-bind (local-body documentation declarations)
723 (parse-lambda-headers local-body :doc-string-allowed t)
724 ;; HAS-ENVIRONMENT and HAS-WHOLE will be either NIL or the name
725 ;; of the variable. (Better names?)
726 (let (has-environment has-whole)
727 ;; Filter out &WHOLE and &ENVIRONMENT from the lambda-list, and
728 ;; do some syntax checking.
729 (when (eq (car lambda-list) '&whole)
730 (setf has-whole (second lambda-list))
731 (setf lambda-list (cddr lambda-list)))
734 for element in lambda-list
738 (setf has-environment element)
740 ((eq element '&environment)
742 (ip-error "Repeated &ENVIRONMENT.")
745 ((eq element '&whole)
746 (ip-error "&WHOLE may only appear first ~
747 in MACROLET lambda-list."))
750 (let ((outer-whole (gensym "WHOLE"))
751 (environment (or has-environment (gensym "ENVIRONMENT")))
752 (macro-name (gensym "NAME")))
753 (%eval `#'(lambda (,outer-whole ,environment)
757 (declare ,@(unless has-environment
758 `((ignore ,environment))))
759 (program-destructuring-bind
761 (list '&whole has-whole)
763 ,macro-name ,@lambda-list)
765 (declare (ignore ,macro-name)
767 (block ,name ,@local-body)))
770 (defun eval-macrolet (body env)
771 (program-destructuring-bind ((&rest local-functions) &body body) body
772 (flet ((generate-fbinding (macro-def)
773 (cons (car macro-def) *macro*))
774 (generate-mbinding (macro-def)
775 (let ((name (car macro-def))
776 (sb!c:*lexenv* (env-native-lexenv env)))
778 (program-assert-symbol-home-package-unlocked
779 :eval name "binding ~A as a local macro"))
780 (cons name (eval-local-macro-def macro-def env)))))
781 (multiple-value-bind (body documentation declarations)
782 (parse-lambda-headers body :doc-string-allowed nil)
783 (declare (ignore documentation))
784 (let* ((specials (declared-specials declarations))
785 (new-env (make-env :parent env
786 :vars (special-bindings specials env)
787 :funs (mapcar #'generate-fbinding
789 :expanders (mapcar #'generate-mbinding
791 :declarations declarations)))
792 (eval-progn body new-env))))))
794 (defun eval-symbol-macrolet (body env)
795 (program-destructuring-bind ((&rest bindings) &body body) body
796 (flet ((generate-binding (binding)
797 (cons (car binding) *symbol-macro*))
798 (generate-sm-binding (binding)
799 (let ((name (car binding))
800 (sb!c:*lexenv* (env-native-lexenv env)))
801 (when (or (boundp name)
802 (eq (sb!int:info :variable :kind name) :macro))
803 (program-assert-symbol-home-package-unlocked
804 :eval name "binding ~A as a local symbol-macro"))
805 (cons name (second binding)))))
806 (multiple-value-bind (body documentation declarations)
807 (parse-lambda-headers body :doc-string-allowed nil)
808 (declare (ignore documentation))
809 (let ((specials (declared-specials declarations)))
810 (dolist (binding bindings)
811 (when (specialp (binding-name binding) specials)
812 (ip-error "~@<Can't bind SYMBOL-MACROLET of special ~
814 (binding-name binding)))))
815 (let* ((specials (declared-specials declarations))
816 (new-env (make-env :parent env
817 :vars (nconc-2 (mapcar #'generate-binding
819 (special-bindings specials env))
820 :symbol-expansions (mapcar
821 #'generate-sm-binding
823 :declarations declarations)))
824 (eval-progn body new-env))))))
826 (defun eval-progv (body env)
827 (program-destructuring-bind (vars vals &body body) body
828 (progv (%eval vars env) (%eval vals env)
829 (eval-progn body env))))
831 (defun eval-function (body env)
832 (program-destructuring-bind (name) body
834 ;; LAMBDAP assumes that the argument is a cons, so we need the
835 ;; initial symbol case, instead of relying on the fall-through
836 ;; case that has the same function body.
837 ((symbolp name) (nth-value 0 (get-function name env)))
838 ((lambdap name) (eval-lambda name env))
839 (t (nth-value 0 (get-function name env))))))
841 (defun eval-eval-when (body env)
842 (program-destructuring-bind ((&rest situation) &body body) body
843 ;; FIXME: check that SITUATION only contains valid situations
844 (if (or (member :execute situation)
845 (member 'eval situation))
846 (eval-progn body env))))
848 (defun eval-quote (body env)
849 (declare (ignore env))
850 (program-destructuring-bind (object) body
853 (defun eval-setq (pairs env)
854 (when (oddp (length pairs))
855 (ip-error "~@<Odd number of args to SETQ: ~S~:@>" (cons 'setq pairs)))
857 (loop for (var new-val) on pairs by #'cddr do
859 (multiple-value-bind (expansion type) (get-variable var env)
863 (%eval (list 'setf expansion new-val) env)))
865 (setf last (set-variable var (%eval new-val env)
867 (unbound-variable (c)
869 (setf last (setf (symbol-value var)
870 (%eval new-val env))))))
873 (defun eval-multiple-value-call (body env)
874 (program-destructuring-bind (function-form &body forms) body
875 (%apply (%eval function-form env)
876 (loop for form in forms
877 nconc (multiple-value-list (%eval form env))))))
879 (defun eval-multiple-value-prog1 (body env)
880 (program-destructuring-bind (first-form &body forms) body
881 (multiple-value-prog1 (%eval first-form env)
882 (eval-progn forms env))))
884 (defun eval-catch (body env)
885 (program-destructuring-bind (tag &body forms) body
886 (catch (%eval tag env)
887 (eval-progn forms env))))
889 (defun eval-tagbody (body old-env)
890 (let ((env (make-env :parent old-env))
895 (flet ((go-to-tag (tag)
896 (setf target-tag tag)
898 ;; For each tag, store a trampoline function into the environment
899 ;; and the location in the body into the TAGS alist.
900 (do ((form body (cdr form)))
902 (when (atom (car form))
903 (when (assoc (car form) tags)
904 (ip-error "The tag :A appears more than once in a tagbody."))
905 (push (cons (car form) (cdr form)) tags)
906 (push (cons (car form) #'go-to-tag) (env-tags env)))))
907 ;; And then evaluate the forms in the body, starting from the
911 ;; The trampoline has set the TARGET-TAG. Restart evaluation of
912 ;; the body from the location in body that matches the tag.
913 (setf start (cdr (assoc target-tag tags)))
916 (when (not (atom form))
917 (%eval form env))))))
919 (defun eval-go (body env)
920 (program-destructuring-bind (tag) body
921 (let ((target (get-tag-binding tag env)))
923 ;; Call the GO-TO-TAG trampoline
924 (funcall (cdr target) tag)
925 (ip-error "~@<Attempt to GO to nonexistent tag: ~S~:@>" tag)))))
927 (defun eval-block (body old-env)
928 (flet ((return-from-eval-block (&rest values)
929 (return-from eval-block (values-list values))))
930 (program-destructuring-bind (name &body body) body
931 (unless (symbolp name)
932 (ip-error "~@<The block name ~S is not a symbol.~:@>" name))
934 :blocks (list (cons name #'return-from-eval-block))
936 (eval-progn body env)))))
938 (defun eval-return-from (body env)
939 (program-destructuring-bind (name &optional result) body
940 (let ((target (get-block-binding name env)))
942 (multiple-value-call (cdr target) (%eval result env))
943 (ip-error "~@<Return for unknown block: ~S~:@>" name)))))
945 (defun eval-the (body env)
946 (program-destructuring-bind (value-type form) body
947 (declare (ignore value-type))
948 ;; FIXME: We should probably check the types here, even though
949 ;; the consequences of the values not being of the asserted types
950 ;; are formally undefined.
953 (defun eval-unwind-protect (body env)
954 (program-destructuring-bind (protected-form &body cleanup-forms) body
955 (unwind-protect (%eval protected-form env)
956 (eval-progn cleanup-forms env))))
958 (defun eval-throw (body env)
959 (program-destructuring-bind (tag result-form) body
960 (throw (%eval tag env)
961 (%eval result-form env))))
963 (defun eval-load-time-value (body env)
964 (program-destructuring-bind (form &optional read-only-p) body
965 (declare (ignore read-only-p))
968 (defun eval-locally (body env)
969 (multiple-value-bind (body documentation declarations)
970 (parse-lambda-headers body :doc-string-allowed nil)
971 (declare (ignore documentation))
972 (let* ((specials (declared-specials declarations))
973 (new-env (if (or specials declarations)
974 (make-env :parent env
975 :vars (special-bindings specials env)
976 :declarations declarations)
978 (eval-progn body new-env))))
980 (defun eval-args (args env)
981 (mapcar #'(lambda (arg) (%eval arg env)) args))
983 ;;; The expansion of SB-SYS:WITH-PINNED-OBJECTS on GENCGC uses some
984 ;;; VOPs which can't be reasonably implemented in the interpreter. So
985 ;;; we special-case the macro.
986 (defun eval-with-pinned-objects (args env)
987 (program-destructuring-bind (values &body body) args
989 (eval-progn body env)
990 (sb!sys:with-pinned-objects ((car values))
991 (eval-with-pinned-objects (cons (cdr values) body) env)))))
993 (define-condition macroexpand-hook-type-error (type-error)
995 (:report (lambda (condition stream)
996 (format stream "The value of *MACROEXPAND-HOOK* is not a designator for a compiled function: ~A"
997 (type-error-datum condition)))))
999 (defvar *eval-dispatch-functions* nil)
1001 ;;; Dispatch to the appropriate EVAL-FOO function based on the contents of EXP.
1002 (declaim (inline %%eval))
1003 (defun %%eval (exp env)
1006 ;; CLHS 3.1.2.1.1 Symbols as Forms
1007 (multiple-value-bind (value kind) (get-variable exp env)
1010 (:expansion (%eval value env)))))
1011 ;; CLHS 3.1.2.1.3 Self-Evaluating Objects
1013 ;; CLHS 3.1.2.1.2 Conses as Forms
1016 ;; CLHS 3.1.2.1.2.1 Special Forms
1017 ((block) (eval-block (cdr exp) env))
1018 ((catch) (eval-catch (cdr exp) env))
1019 ((eval-when) (eval-eval-when (cdr exp) env))
1020 ((flet) (eval-flet (cdr exp) env))
1021 ((function) (eval-function (cdr exp) env))
1022 ((go) (eval-go (cdr exp) env))
1023 ((if) (eval-if (cdr exp) env))
1024 ((labels) (eval-labels (cdr exp) env))
1025 ((let) (eval-let (cdr exp) env))
1026 ((let*) (eval-let* (cdr exp) env))
1027 ((load-time-value) (eval-load-time-value (cdr exp) env))
1028 ((locally) (eval-locally (cdr exp) env))
1029 ((macrolet) (eval-macrolet (cdr exp) env))
1030 ((multiple-value-call) (eval-multiple-value-call (cdr exp) env))
1031 ((multiple-value-prog1) (eval-multiple-value-prog1 (cdr exp) env))
1032 ((progn) (eval-progn (cdr exp) env))
1033 ((progv) (eval-progv (cdr exp) env))
1034 ((quote) (eval-quote (cdr exp) env))
1035 ((return-from) (eval-return-from (cdr exp) env))
1036 ((setq) (eval-setq (cdr exp) env))
1037 ((symbol-macrolet) (eval-symbol-macrolet (cdr exp) env))
1038 ((tagbody) (eval-tagbody (cdr exp) env))
1039 ((the) (eval-the (cdr exp) env))
1040 ((throw) (eval-throw (cdr exp) env))
1041 ((unwind-protect) (eval-unwind-protect (cdr exp) env))
1043 ((sb!ext:truly-the) (eval-the (cdr exp) env))
1044 ;; Not a special form, but a macro whose expansion wouldn't be
1045 ;; handled correctly by the evaluator.
1046 ((sb!sys:with-pinned-objects) (eval-with-pinned-objects (cdr exp) env))
1048 (let ((dispatcher (getf *eval-dispatch-functions* (car exp))))
1051 (funcall dispatcher exp env))
1052 ;; CLHS 3.1.2.1.2.4 Lambda Forms
1053 ((and (consp (car exp)) (eq (caar exp) 'lambda))
1054 (interpreted-apply (eval-function (list (car exp)) env)
1055 (eval-args (cdr exp) env)))
1057 (multiple-value-bind (function kind) (get-function (car exp) env)
1059 ;; CLHS 3.1.2.1.2.3 Function Forms
1060 (:function (%apply function (eval-args (cdr exp) env)))
1061 ;; CLHS 3.1.2.1.2.2 Macro Forms
1063 (let ((hook *macroexpand-hook*))
1064 ;; Having an interpreted function as the
1065 ;; macroexpander hook could cause an infinite
1067 (unless (compiled-function-p
1070 (symbol (symbol-function hook))))
1071 (error 'macroexpand-hook-type-error
1073 :expected-type 'compiled-function))
1074 (%eval (funcall hook
1077 (env-native-lexenv env))
1080 (defun %eval (exp env)
1083 ;; Dynamically binding *EVAL-LEVEL* will prevent tail call
1084 ;; optimization. So only do it when its value will be used for
1085 ;; printing debug output.
1086 (let ((*eval-level* (1+ *eval-level*)))
1087 (let ((*print-circle* t))
1088 (format t "~&~vA~S~%" *eval-level* "" `(%eval ,exp)))
1092 (defun %apply (fun args)
1094 (interpreted-function (interpreted-apply fun args))
1095 (function (apply fun args))
1096 (symbol (apply fun args))))
1098 (defun interpreted-apply (fun args)
1099 (let ((lambda-list (interpreted-function-lambda-list fun))
1100 (env (interpreted-function-env fun))
1101 (body (interpreted-function-body fun))
1102 (declarations (interpreted-function-declarations fun)))
1103 (call-with-new-env-full-parsing
1104 env lambda-list args declarations
1106 (eval-progn body env)))))
1108 ;;; We need separate conditions for the different *-TOO-COMPLEX-ERRORs to
1109 ;;; avoid spuriously triggering the handler in EVAL-IN-NATIVE-ENVIRONMENT
1112 ;;; (let ((sb-ext:*evaluator-mode* :interpret))
1113 ;;; (let ((fun (eval '(let ((a 1)) (lambda () a)))))
1114 ;;; (eval `(compile nil ,fun))))
1116 ;;; FIXME: should these be exported?
1117 (define-condition interpreter-environment-too-complex-error (simple-error)
1119 (define-condition compiler-environment-too-complex-error (simple-error)
1122 ;;; Try to compile an interpreted function. If the environment
1123 ;;; contains local functions or lexical variables we'll punt on
1125 (defun prepare-for-compile (function)
1126 (let ((env (interpreted-function-env function)))
1127 (when (or (env-tags env)
1129 (find-if-not #'(lambda (x) (eq x *macro*))
1130 (env-funs env) :key #'cdr)
1131 (find-if-not #'(lambda (x) (eq x *symbol-macro*))
1134 (error 'interpreter-environment-too-complex-error
1136 "~@<Lexical environment of ~S is too complex to compile.~:@>"
1140 `(sb!int:named-lambda ,(interpreted-function-name function)
1141 ,(interpreted-function-lambda-list function)
1142 (declare ,@(interpreted-function-declarations function))
1143 ,@(interpreted-function-body function))
1144 (env-native-lexenv env))))
1146 ;;; Convert a compiler LEXENV to an interpreter ENV. This is needed
1147 ;;; for EVAL-IN-LEXENV.
1148 (defun make-env-from-native-environment (lexenv)
1149 (let ((native-funs (sb!c::lexenv-funs lexenv))
1150 (native-vars (sb!c::lexenv-vars lexenv)))
1151 (flet ((is-macro (thing)
1152 (and (consp thing) (eq (car thing) 'sb!sys:macro))))
1153 (when (or (sb!c::lexenv-blocks lexenv)
1154 (sb!c::lexenv-cleanup lexenv)
1155 (sb!c::lexenv-lambda lexenv)
1156 (sb!c::lexenv-tags lexenv)
1157 (sb!c::lexenv-type-restrictions lexenv)
1158 (find-if-not #'is-macro native-funs :key #'cdr)
1159 (find-if-not #'is-macro native-vars :key #'cdr))
1160 (error 'compiler-environment-too-complex-error
1162 "~@<Lexical environment is too complex to evaluate in: ~S~:@>"
1165 (flet ((make-binding (native)
1166 (cons (car native) *symbol-macro*))
1167 (make-sm-binding (native)
1168 (cons (car native) (cddr native)))
1169 (make-fbinding (native)
1170 (cons (car native) *macro*))
1171 (make-mbinding (native)
1172 (cons (car native) (cddr native))))
1174 (mapcar #'make-binding native-vars)
1175 (mapcar #'make-fbinding native-funs)
1176 (mapcar #'make-mbinding native-funs)
1177 (mapcar #'make-sm-binding native-vars)
1183 (defun eval-in-environment (form env)
1186 (defun eval-in-native-environment (form lexenv)
1188 ((sb!impl::eval-error
1190 (error 'interpreted-program-error
1191 :condition (sb!int:encapsulated-condition condition)
1193 (sb!c:compiler-error
1195 (if (boundp 'sb!c::*compiler-error-bailout*)
1196 ;; if we're in the compiler, delegate either to a higher
1197 ;; authority or, if that's us, back down to the
1198 ;; outermost compiler handler...
1202 ;; ... if we're not in the compiler, better signal the
1203 ;; error straight away.
1204 (invoke-restart 'sb!c::signal-error)))))
1206 (let ((env (make-env-from-native-environment lexenv)))
1208 (compiler-environment-too-complex-error (condition)
1209 (declare (ignore condition))
1210 (sb!int:style-warn 'sb!kernel:lexical-environment-too-complex
1211 :form form :lexenv lexenv)
1212 (sb!int:simple-eval-in-lexenv form lexenv)))))