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
))
240 ((eq type
:special
) t
)
241 ((member symbol declared-specials
:test
#'eq
)
245 (defun binding-name (binding)
246 (if (consp binding
) (first binding
) binding
))
247 (defun binding-value (binding)
248 (if (consp binding
) (second binding
) nil
))
249 (defun supplied-p-parameter (spec)
250 (if (consp spec
) (third spec
) nil
))
251 (defun keyword-name (spec)
253 (if (consp (first spec
))
254 (second (first spec
))
257 (defun keyword-key (spec)
259 (if (consp (first spec
))
261 (intern (symbol-name (first spec
)) "KEYWORD"))
262 (intern (symbol-name spec
) "KEYWORD")))
263 (defun keyword-default-value (spec)
264 (if (consp spec
) (second spec
) nil
))
266 ;;; Given a list of ARGUMENTS and a LAMBDA-LIST, return two values:
267 ;;; * An alist[*] mapping the required parameters of the function to
268 ;;; the corresponding argument values
269 ;;; * An alist mapping the keyword, optional and rest parameters of
270 ;;; the function to the corresponding argument values (if supplied)
271 ;;; or to the parameter's default expression (if not). Supplied-p
272 ;;; parameters and aux variables are handled in a similar manner.
274 ;;; For example given the argument list of (1 2) and the lambda-list of
275 ;;; (A &OPTIONAL (B A) (C (1+ A))), we'd return the values
276 ;;; (A . '1) and ((B . '2) (C . (1+ A))).
278 ;;; Used only for implementing calls to interpreted functions.
279 (defun parse-arguments (arguments lambda-list
)
280 (multiple-value-bind (required optional rest-p rest keyword-p
281 keyword allow-other-keys-p aux-p aux
)
282 (handler-bind ((style-warning #'muffle-warning
))
283 (sb!int
:parse-lambda-list lambda-list
))
284 (let* ((original-arguments arguments
)
285 (arguments-present (length arguments
))
286 (required-length (length required
))
287 (optional-length (length optional
))
288 (non-keyword-arguments (+ required-length optional-length
))
289 (optionals-present (- (min non-keyword-arguments arguments-present
)
291 (keywords-present-p (> arguments-present non-keyword-arguments
))
292 (let-like-bindings nil
)
293 (let*-like-bindings nil
))
295 ((< arguments-present required-length
)
296 (ip-error "~@<Too few arguments in ~S to satisfy lambda list ~S.~:@>"
297 arguments lambda-list
))
298 ((and (not (or rest-p keyword-p
)) keywords-present-p
)
299 (ip-error "~@<Too many arguments in ~S to satisfy lambda list ~S.~:@>"
300 arguments lambda-list
))
301 ((and keyword-p keywords-present-p
302 (oddp (- arguments-present non-keyword-arguments
)))
303 (ip-error "~@<Odd number of &KEY arguments in ~S for ~S.~:@>"
304 arguments lambda-list
)))
305 (dotimes (i required-length
)
306 (push (cons (pop required
) (pop arguments
)) let-like-bindings
))
307 (do ((optionals-parsed 0 (1+ optionals-parsed
)))
309 (let ((this-optional (pop optional
))
310 (supplied-p (< optionals-parsed optionals-present
)))
311 (push (cons (binding-name this-optional
)
313 (list 'quote
(pop arguments
))
314 (binding-value this-optional
)))
316 (when (supplied-p-parameter this-optional
)
317 (push (cons (supplied-p-parameter this-optional
)
318 (list 'quote supplied-p
))
319 let
*-like-bindings
))))
320 (let ((keyword-plist arguments
))
322 (push (cons rest
(list 'quote keyword-plist
)) let
*-like-bindings
))
324 (unless (or allow-other-keys-p
325 (getf keyword-plist
:allow-other-keys
))
326 (loop for
(key value
) on keyword-plist by
#'cddr doing
327 (when (and (not (eq key
:allow-other-keys
))
328 (not (member key keyword
:key
#'keyword-key
)))
329 (ip-error "~@<Unknown &KEY argument ~S in ~S for ~S.~:@>"
330 key original-arguments lambda-list
))))
331 (dolist (keyword-spec keyword
)
332 (let ((supplied (getf keyword-plist
(keyword-key keyword-spec
)
334 (push (cons (keyword-name keyword-spec
)
335 (if (eq supplied
*not-present
*)
336 (keyword-default-value keyword-spec
)
337 (list 'quote supplied
)))
339 (when (supplied-p-parameter keyword-spec
)
340 (push (cons (supplied-p-parameter keyword-spec
)
341 (list 'quote
(not (eq supplied
*not-present
*))))
342 let
*-like-bindings
))))))
346 (let ((this-aux (pop aux
)))
347 (push (cons (binding-name this-aux
)
348 (binding-value this-aux
))
349 let
*-like-bindings
))))
350 (values (nreverse let-like-bindings
) (nreverse let
*-like-bindings
)))))
352 ;;; Evaluate LET*-like (sequential) bindings.
354 ;;; Given an alist of BINDINGS, evaluate the value form of the first
355 ;;; binding in ENV, bind the variable to the value in ENV, and then
356 ;;; evaluate the next binding form. Once all binding forms have been
357 ;;; handled, END-ACTION is funcalled.
359 ;;; SPECIALS is a list of variables that have a bound special declaration.
360 ;;; These variables (and those that have been declaimed as special) are
361 ;;; bound as special variables.
362 (defun eval-next-let*-binding
(bindings specials env end-action
)
363 (flet ((maybe-eval (exp)
364 ;; Pick off the easy (QUOTE x) case which is very common
365 ;; due to function calls. (see PARSE-ARGUMENTS)
366 (if (and (consp exp
) (eq (car exp
) 'quote
))
370 (let* ((binding-name (car (car bindings
)))
371 (binding-value (cdr (car bindings
))))
372 (if (specialp binding-name specials
)
375 (list (maybe-eval binding-value
))
376 ;; Mark the variable as special in this environment
377 (push-var binding-name
*special
* env
)
378 (eval-next-let*-binding
(cdr bindings
)
379 specials env end-action
))
381 (push-var binding-name
(maybe-eval binding-value
) env
)
382 (eval-next-let*-binding
(cdr bindings
)
383 specials env end-action
))))
384 (funcall end-action
))))
386 ;;; Create a new environment based on OLD-ENV by adding the variable
387 ;;; bindings in BINDINGS to it, and call FUNCTION with the new environment
388 ;;; as the only parameter. DECLARATIONS are the declarations that were
389 ;;; in a source position where bound declarations for the bindings could
392 ;;; FREE-SPECIALS-P controls whether all special declarations should
393 ;;; end cause the variables to be marked as special in the environment
394 ;;; (when true), or only bound declarations (when false). Basically
395 ;;; it'll be T when handling a LET, and NIL when handling a call to an
396 ;;; interpreted function.
397 (defun call-with-new-env (old-env bindings declarations
398 free-specials-p function
)
399 (let* ((specials (declared-specials declarations
))
401 (dynamic-values nil
))
402 ;; To check for package-lock violations
403 (special-bindings specials old-env
)
404 (flet ((generate-binding (binding)
405 (if (specialp (car binding
) specials
)
406 ;; If the variable being bound is globally special or
407 ;; there's a bound special declaration for it, record it
408 ;; in DYNAMIC-VARS / -VALUES separately:
409 ;; * To handle the case of FREE-SPECIALS-P == T more
411 ;; * The dynamic variables will be bound with PROGV just
414 (push (car binding
) dynamic-vars
)
415 (push (cdr binding
) dynamic-values
)
417 ;; Otherwise it's a lexical binding, and the value
418 ;; will be recorded in the environment.
420 (let ((new-env (make-env
422 :vars
(mapcan #'generate-binding bindings
)
423 :declarations declarations
)))
424 (dolist (special (if free-specials-p specials dynamic-vars
))
425 (push-var special
*special
* new-env
))
427 (progv dynamic-vars dynamic-values
428 (funcall function new-env
))
429 ;; When there are no specials, the PROGV would be a no-op,
430 ;; but it's better to elide it completely, since the
431 ;; funcall is then in tail position.
432 (funcall function new-env
))))))
434 ;;; Create a new environment based on OLD-ENV by binding the argument
435 ;;; list ARGUMENTS to LAMBDA-LIST, and call FUNCTION with the new
436 ;;; environment as argument. DECLARATIONS are the declarations that
437 ;;; were in a source position where bound declarations for the
438 ;;; bindings could be introduced.
439 (defun call-with-new-env-full-parsing
440 (old-env lambda-list arguments declarations function
)
441 (multiple-value-bind (let-like-bindings let
*-like-binding
)
442 (parse-arguments arguments lambda-list
)
443 (let ((specials (declared-specials declarations
))
444 var-specials free-specials
)
445 ;; Separate the bound and free special declarations
446 (dolist (special specials
)
447 (if (or (member special let-like-bindings
:key
#'car
)
448 (member special let
*-like-binding
:key
#'car
))
449 (push special var-specials
)
450 (push special free-specials
)))
451 ;; First introduce the required parameters into the environment
452 ;; with CALL-WITH-NEW-ENV
454 old-env let-like-bindings declarations nil
456 ;; Then deal with optionals / keywords / etc.
457 (eval-next-let*-binding
458 let
*-like-binding var-specials env
460 ;; And now that we have evaluated all the
461 ;; initialization forms for the bindings, add the free
462 ;; special declarations to the environment. To see why
463 ;; this is the right thing to do (instead of passing
464 ;; FREE-SPECIALS-P == T to CALL-WITH-NEW-ENV),
467 ;; (eval '(let ((*a* 1))
468 ;; (declare (special *a*))
470 ;; (funcall (lambda (&optional (b *a*))
471 ;; (declare (special *a*))
472 ;; (values b *a*))))))
474 ;; *A* should be special in the body of the lambda, but
475 ;; not when evaluating the default value of B.
476 (dolist (special free-specials
)
477 (push-var special
*special
* env
))
478 (funcall function env
))))))))
480 ;;; Set the VALUE of the binding (either lexical or special) of the
481 ;;; variable named by SYMBOL in the environment ENV.
482 (defun set-variable (symbol value env
)
483 (let ((binding (get-binding symbol env
)))
486 ((eq (cdr binding
) *special
*)
487 (setf (symbol-value symbol
) value
))
488 ((eq (cdr binding
) *symbol-macro
*)
489 (error "Tried to set a symbol-macrolet!"))
490 (t (setf (cdr binding
) value
)))
491 (case (sb!int
:info
:variable
:kind symbol
)
492 (:macro
(error "Tried to set a symbol-macrolet!"))
493 (:alien
(let ((type (sb!int
:info
:variable
:alien-info symbol
)))
494 (setf (sb!alien
::%heap-alien type
) value
)))
496 (let ((type (sb!c
::info
:variable
:type symbol
)))
498 (let ((type-specifier (sb!kernel
:type-specifier type
)))
499 (unless (typep value type-specifier
)
502 :expected-type type-specifier
))))
503 (setf (symbol-value symbol
) value
)))))))
505 ;;; Retrieve the value of the binding (either lexical or special) of
506 ;;; the variable named by SYMBOL in the environment ENV. For symbol
507 ;;; macros the expansion is returned instead.
508 (defun get-variable (symbol env
)
509 (let ((binding (get-binding symbol env
)))
512 ((eq (cdr binding
) *special
*)
513 (values (symbol-value symbol
) :variable
))
514 ((eq (cdr binding
) *symbol-macro
*)
515 (values (cdr (get-symbol-expansion-binding symbol env
))
517 (t (values (cdr binding
) :variable
)))
518 (case (sb!int
:info
:variable
:kind symbol
)
519 (:macro
(values (macroexpand-1 symbol
) :expansion
))
520 (:alien
(let ((type (sb!int
:info
:variable
:alien-info symbol
)))
521 (values (sb!alien
::%heap-alien type
)
523 (t (values (symbol-value symbol
) :variable
))))))
525 ;;; Retrieve the function/macro binding of the symbol NAME in
526 ;;; environment ENV. The second return value will be :MACRO for macro
527 ;;; bindings, :FUNCTION for function bindings.
528 (defun get-function (name env
)
529 (let ((binding (get-fbinding name env
)))
532 ((eq (cdr binding
) *macro
*)
533 (values (cdr (get-expander-binding name env
)) :macro
))
534 (t (values (cdr binding
) :function
)))
536 ((and (symbolp name
) (macro-function name
))
537 (values (macro-function name
) :macro
))
538 (t (values (%coerce-name-to-fun name
) :function
))))))
540 ;;; Return true if EXP is a lambda form.
542 (case (car exp
) ((lambda
544 sb
!kernel
:instance-lambda
)
547 ;;; Split off the declarations (and the docstring, if
548 ;;; DOC-STRING-ALLOWED is true) from the actual forms of BODY.
549 ;;; Returns three values: the cons in BODY containing the first
550 ;;; non-header subform, the docstring, and a list of the declarations.
552 ;;; FIXME: The name of this function is somewhat misleading. It's not
553 ;;; used just for parsing the headers from lambda bodies, but for all
554 ;;; special forms that have attached declarations.
555 (defun parse-lambda-headers (body &key doc-string-allowed
)
556 (loop with documentation
= nil
557 with declarations
= nil
560 ((and doc-string-allowed
(stringp (car form
)))
561 (if (cdr form
) ; CLHS 3.4.11
563 (ip-error "~@<Duplicate doc string ~S.~:@>" (car form
))
564 (setf documentation
(car form
)))
565 (return (values form documentation declarations
))))
566 ((and (consp (car form
)) (eql (caar form
) 'declare
))
567 (setf declarations
(append declarations
(cdar form
))))
568 (t (return (values form documentation declarations
))))
569 finally
(return (values nil documentation declarations
))))
571 ;;; Create an interpreted function from the lambda-form EXP evaluated
572 ;;; in the environment ENV.
573 (defun eval-lambda (exp env
)
575 ((lambda sb
!kernel
:instance-lambda
)
576 (multiple-value-bind (body documentation declarations
)
577 (parse-lambda-headers (cddr exp
) :doc-string-allowed t
)
578 (make-interpreted-function :lambda-list
(second exp
)
580 :documentation documentation
581 :source-location
(sb!c
::make-definition-source-location
)
582 :declarations declarations
)))
583 ((sb!int
:named-lambda
)
584 (multiple-value-bind (body documentation declarations
)
585 (parse-lambda-headers (cdddr exp
) :doc-string-allowed t
)
586 (make-interpreted-function :name
(second exp
)
587 :lambda-list
(third exp
)
589 :documentation documentation
590 :source-location
(sb!c
::make-definition-source-location
)
591 :declarations declarations
)))))
593 (defun eval-progn (body env
)
594 (let ((previous-exp nil
))
597 (%eval previous-exp env
))
598 (setf previous-exp exp
))
599 ;; Preserve tail call
600 (%eval previous-exp env
)))
602 (defun eval-if (body env
)
603 (program-destructuring-bind (test if-true
&optional if-false
) body
606 (%eval if-false env
))))
608 (defun eval-let (body env
)
609 (program-destructuring-bind (bindings &body body
) body
610 ;; First evaluate the bindings in parallel
611 (let ((bindings (mapcar
613 (cons (binding-name binding
)
614 (%eval
(binding-value binding
) env
)))
616 (multiple-value-bind (body documentation declarations
)
617 (parse-lambda-headers body
:doc-string-allowed nil
)
618 (declare (ignore documentation
))
619 ;; Then establish them into the environment, and evaluate the
621 (call-with-new-env env bindings declarations t
623 (eval-progn body env
)))))))
625 (defun eval-let* (body old-env
)
626 (program-destructuring-bind (bindings &body body
) body
627 (multiple-value-bind (body documentation declarations
)
628 (parse-lambda-headers body
:doc-string-allowed nil
)
629 (declare (ignore documentation
))
630 ;; First we separate the special declarations into bound and
631 ;; free declarations.
632 (let ((specials (declared-specials declarations
))
633 var-specials free-specials
)
634 (dolist (special specials
)
635 (if (member special bindings
:key
#'binding-name
)
636 (push special var-specials
)
637 (push special free-specials
)))
638 (let ((env (make-env :parent old-env
639 :declarations declarations
)))
640 ;; Then we establish the bindings into the environment
642 (eval-next-let*-binding
643 (mapcar #'(lambda (binding)
644 (cons (binding-name binding
)
645 (binding-value binding
)))
649 ;; Now that we're done evaluating the bindings, add the
650 ;; free special declarations. See also
651 ;; CALL-WITH-NEW-ENV-FULL-PARSING.
652 (dolist (special free-specials
)
653 (push-var special
*special
* env
))
654 (eval-progn body env
))))))))
656 ;; Return a named local function in the environment ENV, made from the
657 ;; definition form FUNCTION-DEF.
658 (defun eval-local-function-def (function-def env
)
659 (program-destructuring-bind (name lambda-list
&body local-body
) function-def
660 (multiple-value-bind (local-body documentation declarations
)
661 (parse-lambda-headers local-body
:doc-string-allowed t
)
662 (%eval
`#'(sb!int
:named-lambda
,name
,lambda-list
666 (declare ,@declarations
)
667 (block ,(cond ((consp name
) (second name
))
672 (defun eval-flet (body env
)
673 (program-destructuring-bind ((&rest local-functions
) &body body
) body
674 (multiple-value-bind (body documentation declarations
)
675 (parse-lambda-headers body
:doc-string-allowed nil
)
676 (declare (ignore documentation
))
677 (let* ((specials (declared-specials declarations
))
678 (new-env (make-env :parent env
679 :vars
(special-bindings specials env
)
680 :declarations declarations
)))
681 (dolist (function-def local-functions
)
682 (push-fun (car function-def
)
683 ;; Evaluate the function definitions in ENV.
684 (eval-local-function-def function-def env
)
685 ;; Do package-lock checks in ENV.
687 ;; But add the bindings to the child environment.
689 (eval-progn body new-env
)))))
691 (defun eval-labels (body old-env
)
692 (program-destructuring-bind ((&rest local-functions
) &body body
) body
693 (multiple-value-bind (body documentation declarations
)
694 (parse-lambda-headers body
:doc-string-allowed nil
)
695 (declare (ignore documentation
))
696 ;; Create a child environment, evaluate the function definitions
697 ;; in it, and add them into the same environment.
698 (let ((env (make-env :parent old-env
699 :declarations declarations
)))
700 (dolist (function-def local-functions
)
701 (push-fun (car function-def
)
702 (eval-local-function-def function-def env
)
705 ;; And then add an environment for the body of the LABELS. A
706 ;; separate environment from the one where we added the
707 ;; functions to is needed, since any special variable
708 ;; declarations need to be in effect in the body, but not in
709 ;; the bodies of the local functions.
710 (let* ((specials (declared-specials declarations
))
711 (new-env (make-env :parent env
712 :vars
(special-bindings specials env
))))
713 (eval-progn body new-env
))))))
715 ;; Return a local macro-expander in the environment ENV, made from the
716 ;; definition form FUNCTION-DEF.
717 (defun eval-local-macro-def (function-def env
)
718 (program-destructuring-bind (name lambda-list
&body local-body
) function-def
719 (multiple-value-bind (local-body documentation declarations
)
720 (parse-lambda-headers local-body
:doc-string-allowed t
)
721 ;; HAS-ENVIRONMENT and HAS-WHOLE will be either NIL or the name
722 ;; of the variable. (Better names?)
723 (let (has-environment has-whole
)
724 ;; Filter out &WHOLE and &ENVIRONMENT from the lambda-list, and
725 ;; do some syntax checking.
726 (when (eq (car lambda-list
) '&whole
)
727 (setf has-whole
(second lambda-list
))
728 (setf lambda-list
(cddr lambda-list
)))
731 for element in lambda-list
735 (setf has-environment element
)
737 ((eq element
'&environment
)
739 (ip-error "Repeated &ENVIRONMENT.")
742 ((eq element
'&whole
)
743 (ip-error "&WHOLE may only appear first ~
744 in MACROLET lambda-list."))
747 (let ((outer-whole (gensym "WHOLE"))
748 (environment (or has-environment
(gensym "ENVIRONMENT")))
749 (macro-name (gensym "NAME")))
750 (%eval
`#'(lambda (,outer-whole
,environment
)
754 (declare ,@(unless has-environment
755 `((ignore ,environment
))))
756 (program-destructuring-bind
758 (list '&whole has-whole
)
760 ,macro-name
,@lambda-list
)
762 (declare (ignore ,macro-name
)
764 (block ,name
,@local-body
)))
767 (defun eval-macrolet (body env
)
768 (program-destructuring-bind ((&rest local-functions
) &body body
) body
769 (flet ((generate-fbinding (macro-def)
770 (cons (car macro-def
) *macro
*))
771 (generate-mbinding (macro-def)
772 (let ((name (car macro-def
))
773 (sb!c
:*lexenv
* (env-native-lexenv env
)))
775 (program-assert-symbol-home-package-unlocked
776 :eval name
"binding ~A as a local macro"))
777 (cons name
(eval-local-macro-def macro-def env
)))))
778 (multiple-value-bind (body documentation declarations
)
779 (parse-lambda-headers body
:doc-string-allowed nil
)
780 (declare (ignore documentation
))
781 (let* ((specials (declared-specials declarations
))
782 (new-env (make-env :parent env
783 :vars
(special-bindings specials env
)
784 :funs
(mapcar #'generate-fbinding
786 :expanders
(mapcar #'generate-mbinding
788 :declarations declarations
)))
789 (eval-progn body new-env
))))))
791 (defun eval-symbol-macrolet (body env
)
792 (program-destructuring-bind ((&rest bindings
) &body body
) body
793 (flet ((generate-binding (binding)
794 (cons (car binding
) *symbol-macro
*))
795 (generate-sm-binding (binding)
796 (let ((name (car binding
))
797 (sb!c
:*lexenv
* (env-native-lexenv env
)))
798 (when (or (boundp name
)
799 (eq (sb!int
:info
:variable
:kind name
) :macro
))
800 (program-assert-symbol-home-package-unlocked
801 :eval name
"binding ~A as a local symbol-macro"))
802 (cons name
(second binding
)))))
803 (multiple-value-bind (body documentation declarations
)
804 (parse-lambda-headers body
:doc-string-allowed nil
)
805 (declare (ignore documentation
))
806 (let ((specials (declared-specials declarations
)))
807 (dolist (binding bindings
)
808 (when (specialp (binding-name binding
) specials
)
809 (ip-error "~@<Can't bind SYMBOL-MACROLET of special ~
811 (binding-name binding
)))))
812 (let* ((specials (declared-specials declarations
))
813 (new-env (make-env :parent env
814 :vars
(nconc-2 (mapcar #'generate-binding
816 (special-bindings specials env
))
817 :symbol-expansions
(mapcar
818 #'generate-sm-binding
820 :declarations declarations
)))
821 (eval-progn body new-env
))))))
823 (defun eval-progv (body env
)
824 (program-destructuring-bind (vars vals
&body body
) body
825 (progv (%eval vars env
) (%eval vals env
)
826 (eval-progn body env
))))
828 (defun eval-function (body env
)
829 (program-destructuring-bind (name) body
831 ;; LAMBDAP assumes that the argument is a cons, so we need the
832 ;; initial symbol case, instead of relying on the fall-through
833 ;; case that has the same function body.
834 ((symbolp name
) (nth-value 0 (get-function name env
)))
835 ((lambdap name
) (eval-lambda name env
))
836 (t (nth-value 0 (get-function name env
))))))
838 (defun eval-eval-when (body env
)
839 (program-destructuring-bind ((&rest situation
) &body body
) body
840 ;; FIXME: check that SITUATION only contains valid situations
841 (if (or (member :execute situation
)
842 (member 'eval situation
))
843 (eval-progn body env
))))
845 (defun eval-quote (body env
)
846 (declare (ignore env
))
847 (program-destructuring-bind (object) body
850 (defun eval-setq (pairs env
)
851 (when (oddp (length pairs
))
852 (ip-error "~@<Odd number of args to SETQ: ~S~:@>" (cons 'setq pairs
)))
854 (loop for
(var new-val
) on pairs by
#'cddr do
856 (multiple-value-bind (expansion type
) (get-variable var env
)
860 (%eval
(list 'setf expansion new-val
) env
)))
862 (setf last
(set-variable var
(%eval new-val env
)
864 (unbound-variable (c)
866 (setf last
(setf (symbol-value var
)
867 (%eval new-val env
))))))
870 (defun eval-multiple-value-call (body env
)
871 (program-destructuring-bind (function-form &body forms
) body
872 (%apply
(%eval function-form env
)
873 (loop for form in forms
874 nconc
(multiple-value-list (%eval form env
))))))
876 (defun eval-multiple-value-prog1 (body env
)
877 (program-destructuring-bind (first-form &body forms
) body
878 (multiple-value-prog1 (%eval first-form env
)
879 (eval-progn forms env
))))
881 (defun eval-catch (body env
)
882 (program-destructuring-bind (tag &body forms
) body
883 (catch (%eval tag env
)
884 (eval-progn forms env
))))
886 (defun eval-tagbody (body old-env
)
887 (let ((env (make-env :parent old-env
))
892 (flet ((go-to-tag (tag)
893 (setf target-tag tag
)
895 ;; For each tag, store a trampoline function into the environment
896 ;; and the location in the body into the TAGS alist.
897 (do ((form body
(cdr form
)))
899 (when (atom (car form
))
900 (when (assoc (car form
) tags
)
901 (ip-error "The tag :A appears more than once in a tagbody."))
902 (push (cons (car form
) (cdr form
)) tags
)
903 (push (cons (car form
) #'go-to-tag
) (env-tags env
)))))
904 ;; And then evaluate the forms in the body, starting from the
908 ;; The trampoline has set the TARGET-TAG. Restart evaluation of
909 ;; the body from the location in body that matches the tag.
910 (setf start
(cdr (assoc target-tag tags
)))
913 (when (not (atom form
))
914 (%eval form env
))))))
916 (defun eval-go (body env
)
917 (program-destructuring-bind (tag) body
918 (let ((target (get-tag-binding tag env
)))
920 ;; Call the GO-TO-TAG trampoline
921 (funcall (cdr target
) tag
)
922 (ip-error "~@<Attempt to GO to nonexistent tag: ~S~:@>" tag
)))))
924 (defun eval-block (body old-env
)
925 (flet ((return-from-eval-block (&rest values
)
926 (return-from eval-block
(values-list values
))))
927 (program-destructuring-bind (name &body body
) body
928 (unless (symbolp name
)
929 (ip-error "~@<The block name ~S is not a symbol.~:@>" name
))
931 :blocks
(list (cons name
#'return-from-eval-block
))
933 (eval-progn body env
)))))
935 (defun eval-return-from (body env
)
936 (program-destructuring-bind (name &optional result
) body
937 (let ((target (get-block-binding name env
)))
939 (multiple-value-call (cdr target
) (%eval result env
))
940 (ip-error "~@<Return for unknown block: ~S~:@>" name
)))))
942 (defun eval-the (body env
)
943 (program-destructuring-bind (value-type form
) body
944 (declare (ignore value-type
))
945 ;; FIXME: We should probably check the types here, even though
946 ;; the consequences of the values not being of the asserted types
947 ;; are formally undefined.
950 (defun eval-unwind-protect (body env
)
951 (program-destructuring-bind (protected-form &body cleanup-forms
) body
952 (unwind-protect (%eval protected-form env
)
953 (eval-progn cleanup-forms env
))))
955 (defun eval-throw (body env
)
956 (program-destructuring-bind (tag result-form
) body
957 (throw (%eval tag env
)
958 (%eval result-form env
))))
960 (defun eval-load-time-value (body env
)
961 (program-destructuring-bind (form &optional read-only-p
) body
962 (declare (ignore read-only-p
))
965 (defun eval-locally (body env
)
966 (multiple-value-bind (body documentation declarations
)
967 (parse-lambda-headers body
:doc-string-allowed nil
)
968 (declare (ignore documentation
))
969 (let* ((specials (declared-specials declarations
))
970 (new-env (if (or specials declarations
)
971 (make-env :parent env
972 :vars
(special-bindings specials env
)
973 :declarations declarations
)
975 (eval-progn body new-env
))))
977 (defun eval-args (args env
)
978 (mapcar #'(lambda (arg) (%eval arg env
)) args
))
980 ;;; The expansion of SB-SYS:WITH-PINNED-OBJECTS on GENCGC uses some
981 ;;; VOPs which can't be reasonably implemented in the interpreter. So
982 ;;; we special-case the macro.
983 (defun eval-with-pinned-objects (args env
)
984 (program-destructuring-bind (values &body body
) args
986 (eval-progn body env
)
987 (sb!sys
:with-pinned-objects
((car values
))
988 (eval-with-pinned-objects (cons (cdr values
) body
) env
)))))
990 (define-condition macroexpand-hook-type-error
(type-error)
992 (:report
(lambda (condition stream
)
993 (format stream
"The value of *MACROEXPAND-HOOK* is not a designator for a compiled function: ~A"
994 (type-error-datum condition
)))))
996 (defvar *eval-dispatch-functions
* nil
)
998 ;;; Dispatch to the appropriate EVAL-FOO function based on the contents of EXP.
999 (declaim (inline %%eval
))
1000 (defun %%eval
(exp env
)
1003 ;; CLHS 3.1.2.1.1 Symbols as Forms
1004 (multiple-value-bind (value kind
) (get-variable exp env
)
1007 (:expansion
(%eval value env
)))))
1008 ;; CLHS 3.1.2.1.3 Self-Evaluating Objects
1010 ;; CLHS 3.1.2.1.2 Conses as Forms
1013 ;; CLHS 3.1.2.1.2.1 Special Forms
1014 ((block) (eval-block (cdr exp
) env
))
1015 ((catch) (eval-catch (cdr exp
) env
))
1016 ((eval-when) (eval-eval-when (cdr exp
) env
))
1017 ((flet) (eval-flet (cdr exp
) env
))
1018 ((function) (eval-function (cdr exp
) env
))
1019 ((go) (eval-go (cdr exp
) env
))
1020 ((if) (eval-if (cdr exp
) env
))
1021 ((labels) (eval-labels (cdr exp
) env
))
1022 ((let) (eval-let (cdr exp
) env
))
1023 ((let*) (eval-let* (cdr exp
) env
))
1024 ((load-time-value) (eval-load-time-value (cdr exp
) env
))
1025 ((locally) (eval-locally (cdr exp
) env
))
1026 ((macrolet) (eval-macrolet (cdr exp
) env
))
1027 ((multiple-value-call) (eval-multiple-value-call (cdr exp
) env
))
1028 ((multiple-value-prog1) (eval-multiple-value-prog1 (cdr exp
) env
))
1029 ((progn) (eval-progn (cdr exp
) env
))
1030 ((progv) (eval-progv (cdr exp
) env
))
1031 ((quote) (eval-quote (cdr exp
) env
))
1032 ((return-from) (eval-return-from (cdr exp
) env
))
1033 ((setq) (eval-setq (cdr exp
) env
))
1034 ((symbol-macrolet) (eval-symbol-macrolet (cdr exp
) env
))
1035 ((tagbody) (eval-tagbody (cdr exp
) env
))
1036 ((the) (eval-the (cdr exp
) env
))
1037 ((throw) (eval-throw (cdr exp
) env
))
1038 ((unwind-protect) (eval-unwind-protect (cdr exp
) env
))
1040 ((sb!ext
:truly-the
) (eval-the (cdr exp
) env
))
1041 ;; Not a special form, but a macro whose expansion wouldn't be
1042 ;; handled correctly by the evaluator.
1043 ((sb!sys
:with-pinned-objects
) (eval-with-pinned-objects (cdr exp
) env
))
1045 (let ((dispatcher (getf *eval-dispatch-functions
* (car exp
))))
1048 (funcall dispatcher exp env
))
1049 ;; CLHS 3.1.2.1.2.4 Lambda Forms
1050 ((and (consp (car exp
)) (eq (caar exp
) 'lambda
))
1051 (interpreted-apply (eval-function (list (car exp
)) env
)
1052 (eval-args (cdr exp
) env
)))
1054 (multiple-value-bind (function kind
) (get-function (car exp
) env
)
1056 ;; CLHS 3.1.2.1.2.3 Function Forms
1057 (:function
(%apply function
(eval-args (cdr exp
) env
)))
1058 ;; CLHS 3.1.2.1.2.2 Macro Forms
1060 (let ((hook *macroexpand-hook
*))
1061 ;; Having an interpreted function as the
1062 ;; macroexpander hook could cause an infinite
1064 (unless (compiled-function-p
1067 (symbol (symbol-function hook
))))
1068 (error 'macroexpand-hook-type-error
1070 :expected-type
'compiled-function
))
1071 (%eval
(funcall hook
1074 (env-native-lexenv env
))
1077 (defun %eval
(exp env
)
1080 ;; Dynamically binding *EVAL-LEVEL* will prevent tail call
1081 ;; optimization. So only do it when its value will be used for
1082 ;; printing debug output.
1083 (let ((*eval-level
* (1+ *eval-level
*)))
1084 (let ((*print-circle
* t
))
1085 (format t
"~&~vA~S~%" *eval-level
* "" `(%eval
,exp
)))
1089 (defun %apply
(fun args
)
1091 (interpreted-function (interpreted-apply fun args
))
1092 (function (apply fun args
))
1093 (symbol (apply fun args
))))
1095 (defun interpreted-apply (fun args
)
1096 (let ((lambda-list (interpreted-function-lambda-list fun
))
1097 (env (interpreted-function-env fun
))
1098 (body (interpreted-function-body fun
))
1099 (declarations (interpreted-function-declarations fun
)))
1100 (call-with-new-env-full-parsing
1101 env lambda-list args declarations
1103 (eval-progn body env
)))))
1105 ;;; We need separate conditions for the different *-TOO-COMPLEX-ERRORs to
1106 ;;; avoid spuriously triggering the handler in EVAL-IN-NATIVE-ENVIRONMENT
1109 ;;; (let ((sb-ext:*evaluator-mode* :interpret))
1110 ;;; (let ((fun (eval '(let ((a 1)) (lambda () a)))))
1111 ;;; (eval `(compile nil ,fun))))
1113 ;;; FIXME: should these be exported?
1114 (define-condition interpreter-environment-too-complex-error
(simple-error)
1116 (define-condition compiler-environment-too-complex-error
(simple-error)
1119 ;;; Try to compile an interpreted function. If the environment
1120 ;;; contains local functions or lexical variables we'll punt on
1122 (defun prepare-for-compile (function)
1123 (let ((env (interpreted-function-env function
)))
1124 (when (or (env-tags env
)
1126 (find-if-not #'(lambda (x) (eq x
*macro
*))
1127 (env-funs env
) :key
#'cdr
)
1128 (find-if-not #'(lambda (x) (eq x
*symbol-macro
*))
1131 (error 'interpreter-environment-too-complex-error
1133 "~@<Lexical environment of ~S is too complex to compile.~:@>"
1137 `(sb!int
:named-lambda
,(interpreted-function-name function
)
1138 ,(interpreted-function-lambda-list function
)
1139 (declare ,@(interpreted-function-declarations function
))
1140 ,@(interpreted-function-body function
))
1141 (env-native-lexenv env
))))
1143 ;;; Convert a compiler LEXENV to an interpreter ENV. This is needed
1144 ;;; for EVAL-IN-LEXENV.
1145 (defun make-env-from-native-environment (lexenv)
1146 (let ((native-funs (sb!c
::lexenv-funs lexenv
))
1147 (native-vars (sb!c
::lexenv-vars lexenv
)))
1148 (flet ((is-macro (thing)
1149 (and (consp thing
) (eq (car thing
) 'sb
!sys
:macro
))))
1150 (when (or (sb!c
::lexenv-blocks lexenv
)
1151 (sb!c
::lexenv-cleanup lexenv
)
1152 (sb!c
::lexenv-lambda lexenv
)
1153 (sb!c
::lexenv-tags lexenv
)
1154 (sb!c
::lexenv-type-restrictions lexenv
)
1155 (find-if-not #'is-macro native-funs
:key
#'cdr
)
1156 (find-if-not #'is-macro native-vars
:key
#'cdr
))
1157 (error 'compiler-environment-too-complex-error
1159 "~@<Lexical environment is too complex to evaluate in: ~S~:@>"
1162 (flet ((make-binding (native)
1163 (cons (car native
) *symbol-macro
*))
1164 (make-sm-binding (native)
1165 (cons (car native
) (cddr native
)))
1166 (make-fbinding (native)
1167 (cons (car native
) *macro
*))
1168 (make-mbinding (native)
1169 (cons (car native
) (cddr native
))))
1171 (mapcar #'make-binding native-vars
)
1172 (mapcar #'make-fbinding native-funs
)
1173 (mapcar #'make-mbinding native-funs
)
1174 (mapcar #'make-sm-binding native-vars
)
1180 (defun eval-in-environment (form env
)
1183 (defun eval-in-native-environment (form lexenv
)
1185 ((sb!impl
::eval-error
1187 (error 'interpreted-program-error
1188 :condition
(sb!int
:encapsulated-condition condition
)
1190 (sb!c
:compiler-error
1192 (if (boundp 'sb
!c
::*compiler-error-bailout
*)
1193 ;; if we're in the compiler, delegate either to a higher
1194 ;; authority or, if that's us, back down to the
1195 ;; outermost compiler handler...
1199 ;; ... if we're not in the compiler, better signal the
1200 ;; error straight away.
1201 (invoke-restart 'sb
!c
::signal-error
)))))
1203 (let ((env (make-env-from-native-environment lexenv
)))
1205 (compiler-environment-too-complex-error (condition)
1206 (declare (ignore condition
))
1207 (sb!int
:style-warn
'sb
!kernel
:lexical-environment-too-complex
1208 :form form
:lexenv lexenv
)
1209 (sb!int
:simple-eval-in-lexenv form lexenv
)))))