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 ;;; FIXME: This macro is not clearly better than plain destructuring-bind.
41 ;;; First of all, it's ridiculous that the error message says
42 ;;; "error while parsing arguments to PROGRAM-DESTRUCTURING-BIND".
43 ;;; The user doesn't care what the macro was that parsed the arguments
44 ;;; to the special operator. It should instead say
45 ;;; "... while parsing arguments to special operator <foo>"
47 ;;; Second, it is naive to think that existence of this macro suffices
48 ;;; to always signal an INTEPRETED-PROGRAM-ERROR and not just ERROR.
49 ;;; e.g. (LET ((X 1)) . JUNK) binds the &BODY variable to the non-list JUNK.
50 ;;; To fix the general problem, every use of DOLIST and other things
51 ;;; would have to be replaced by something like SB-PCL::DOLIST-CAREFULLY.
52 ;;; Similarly for ((&REST BINDINGS) &BODY BODY) wherein it's not even
53 ;;; obvious that BINDINGS is enforced by the macro to be a list. [lp#1469275]
55 ;; OAOOM? (see destructuring-bind.lisp)
56 (defmacro program-destructuring-bind
(lambda-list arg-list
&body body
)
57 ;; (:EVAL) is a dummy context. We don't have enough information to
58 ;; show the operator name without using debugger internals to get the stack frame.
59 ;; It would be easier to make the name an argument to this macro.
60 `(sb!int
:binding
* ,(sb!c
::expand-ds-bind lambda-list arg-list t nil
'(:eval
))
63 (defun ip-error (format-control &rest format-arguments
)
64 (error 'interpreted-program-error
65 :format-control format-control
66 :format-arguments format-arguments
))
68 (defmacro nconc-2
(a b
)
74 (progn (setf (cdr (last ,tmp
)) ,tmp2
) ,tmp
)
77 ;;; Construct a compiler LEXENV from the same data that's used for
78 ;;; creating an interpreter ENV. This is needed for example when
79 ;;; passing the environment to macroexpanders or when compiling an
80 ;;; interpreted function.
81 (defun fabricate-new-native-environment (old-lexenv new-funs new-expanders
82 new-vars new-symbol-expansions
84 (labels ((to-native-funs (binding)
85 ;; Non-macroexpander function entries are irrelevant for
86 ;; the LEXENV. If we're using the LEXENV for
87 ;; macro-expansion any references to local non-macro
88 ;; function bindings are undefined behaviour. If we're
89 ;; compiling an interpreted function, a lexical environment
90 ;; with non-macro functions will be too hairy to compile.
91 (if (eq (cdr binding
) *macro
*)
94 (cdr (assoc (car binding
) new-expanders
))))
97 (to-native-vars (binding)
98 ;; And likewise for symbol macros.
99 (if (eq (cdr binding
) *symbol-macro
*)
102 (cdr (assoc (car binding
) new-symbol-expansions
))))
105 (let ((lexenv (sb!c
::internal-make-lexenv
106 (nconc-2 (mapcar #'to-native-funs new-funs
)
107 (sb!c
::lexenv-funs old-lexenv
))
108 (nconc-2 (mapcar #'to-native-vars new-vars
)
109 (sb!c
::lexenv-vars old-lexenv
))
111 (sb!c
::lexenv-handled-conditions old-lexenv
)
112 (sb!c
::lexenv-disabled-package-locks old-lexenv
)
113 (sb!c
::lexenv-policy old-lexenv
) ; = (OR %POLICY *POLICY*)
114 (sb!c
::lexenv-user-data old-lexenv
)
116 (dolist (declaration declarations
)
117 (unless (consp declaration
)
118 (ip-error "malformed declaration specifier ~S in ~S"
119 declaration
(cons 'declare declarations
)))
120 (case (car declaration
)
122 (setf (sb!c
::lexenv-%policy lexenv
)
123 (copy-structure (sb!c
::lexenv-%policy lexenv
)))
124 (dolist (element (cdr declaration
))
125 (multiple-value-bind (quality value
)
126 (if (not (consp element
)) ; FIXME: OAOOM w/'proclaim'
128 (program-destructuring-bind (quality value
)
130 (values quality value
)))
132 ((sb!c
::policy-quality-name-p quality
)
133 (sb!c
::alter-policy
(sb!c
::lexenv-%policy lexenv
)
135 (t (warn "ignoring unknown optimization quality ~S in ~S"
136 quality
(cons 'declare declarations
)))))))
138 (setf (sb!c
::lexenv-handled-conditions lexenv
)
139 (sb!c
::process-muffle-conditions-decl
141 (sb!c
::lexenv-handled-conditions lexenv
))))
143 (setf (sb!c
::lexenv-handled-conditions lexenv
)
144 (sb!c
::process-unmuffle-conditions-decl
146 (sb!c
::lexenv-handled-conditions lexenv
))))
147 ((disable-package-locks sb
!ext
:enable-package-locks
)
148 (setf (sb!c
::lexenv-disabled-package-locks lexenv
)
149 (sb!c
::process-package-lock-decl
151 (sb!c
::lexenv-disabled-package-locks lexenv
))))))
155 (:constructor %make-env
156 (parent vars funs expanders symbol-expansions
157 tags blocks declarations native-lexenv
)))
168 (defun make-env (&key parent vars funs expanders
169 symbol-expansions tags blocks declarations
)
171 (append vars
(env-vars parent
))
172 (append funs
(env-funs parent
))
173 (append expanders
(env-expanders parent
))
174 (append symbol-expansions
(env-symbol-expansions parent
))
175 (nconc-2 tags
(env-tags parent
))
176 (nconc-2 blocks
(env-blocks parent
))
178 (fabricate-new-native-environment (env-native-lexenv parent
)
180 vars symbol-expansions
183 (defun make-null-environment ()
184 (%make-env nil nil nil nil nil nil nil nil
185 (sb!c
::internal-make-lexenv
187 nil nil nil nil nil nil nil
191 ;;; Augment ENV with a special or lexical variable binding
192 (declaim (inline push-var
))
193 (defun push-var (name value env
)
194 (push (cons name value
) (env-vars env
))
195 (push (cons name
:bogus
) (sb!c
::lexenv-vars
(env-native-lexenv env
))))
197 ;;; Augment ENV with a local function binding
198 (declaim (inline push-fun
))
199 (defun push-fun (name value calling-env body-env
)
201 (let ((sb!c
:*lexenv
* (env-native-lexenv calling-env
)))
202 (program-assert-symbol-home-package-unlocked
203 :eval name
"binding ~A as a local function")))
204 (push (cons name value
) (env-funs body-env
))
205 (push (cons name
:bogus
) (sb!c
::lexenv-funs
(env-native-lexenv body-env
))))
207 (defmethod print-object ((env env
) stream
)
208 (print-unreadable-object (env stream
:type t
:identity t
)))
210 (macrolet ((define-get-binding (name accessor
&key
(test '#'eq
))
211 ;; A macro, sadly, because an inline function here is
213 `(defmacro ,name
(symbol env
)
214 `(assoc ,symbol
(,',accessor
,env
) :test
,',test
))))
215 (define-get-binding get-binding env-vars
)
216 (define-get-binding get-fbinding env-funs
:test
#'equal
)
217 (define-get-binding get-expander-binding env-expanders
)
218 (define-get-binding get-symbol-expansion-binding env-symbol-expansions
)
219 (define-get-binding get-tag-binding env-tags
:test
#'eql
)
220 (define-get-binding get-block-binding env-blocks
))
222 ;;; Return a list of all symbols that are declared special in the
223 ;;; declarations listen in DECLS.
224 (defun declared-specials (decls)
225 (let ((specials nil
))
227 (when (eql (car decl
) 'special
)
228 (dolist (var (cdr decl
))
229 (push var specials
))))
232 ;;; Given a list of variables that should be marked as special in an
233 ;;; environment, return the appropriate binding forms to be given
235 (defun special-bindings (specials env
)
236 (mapcar #'(lambda (var)
237 (let ((sb!c
:*lexenv
* (env-native-lexenv env
)))
238 (program-assert-symbol-home-package-unlocked
239 :eval var
"declaring ~A special"))
240 (cons var
*special
*))
243 ;;; Return true if SYMBOL has been declared special either globally
244 ;;; or is in the DECLARED-SPECIALS list.
245 (defun specialp (symbol declared-specials
)
246 (let ((type (sb!int
:info
:variable
:kind symbol
)))
249 ;; Horrible place for this, but it works.
250 (ip-error "Can't bind constant symbol: ~S" symbol
))
253 (ip-error "Can't bind a global variable: ~S" symbol
))
254 ((eq type
:special
) t
)
255 ((member symbol declared-specials
:test
#'eq
)
259 (defun binding-name (binding)
260 (if (consp binding
) (first binding
) binding
))
261 (defun binding-value (binding)
262 (if (consp binding
) (second binding
) nil
))
263 (defun supplied-p-parameter (spec)
264 (if (consp spec
) (third spec
) nil
))
265 (defun keyword-name (spec)
267 (if (consp (first spec
))
268 (second (first spec
))
271 (defun keyword-key (spec)
273 (if (consp (first spec
))
275 (intern (symbol-name (first spec
)) "KEYWORD"))
276 (intern (symbol-name spec
) "KEYWORD")))
277 (defun keyword-default-value (spec)
278 (if (consp spec
) (second spec
) nil
))
280 ;;; Given a list of ARGUMENTS and a LAMBDA-LIST, return two values:
281 ;;; * An alist[*] mapping the required parameters of the function to
282 ;;; the corresponding argument values
283 ;;; * An alist mapping the keyword, optional and rest parameters of
284 ;;; the function to the corresponding argument values (if supplied)
285 ;;; or to the parameter's default expression (if not). Supplied-p
286 ;;; parameters and aux variables are handled in a similar manner.
288 ;;; For example given the argument list of (1 2) and the lambda-list of
289 ;;; (A &OPTIONAL (B A) (C (1+ A))), we'd return the values
290 ;;; (A . '1) and ((B . '2) (C . (1+ A))).
292 ;;; Used only for implementing calls to interpreted functions.
293 (defun parse-arguments (arguments lambda-list
)
294 (multiple-value-bind (llks required optional rest keyword aux
)
295 ;; FIXME: shouldn't this just pass ":silent t" ?
296 (handler-bind ((style-warning #'muffle-warning
))
297 (sb!int
:parse-lambda-list lambda-list
))
298 (let* ((original-arguments arguments
)
299 (rest-p (not (null rest
)))
301 (keyword-p (sb!int
:ll-kwds-keyp llks
))
302 (allow-other-keys-p (sb!int
:ll-kwds-allowp llks
))
303 (arguments-present (length arguments
))
304 (required-length (length required
))
305 (optional-length (length optional
))
306 (non-keyword-arguments (+ required-length optional-length
))
307 (optionals-present (- (min non-keyword-arguments arguments-present
)
309 (keywords-present-p (> arguments-present non-keyword-arguments
))
310 (let-like-bindings nil
)
311 (let*-like-bindings nil
))
313 ((< arguments-present required-length
)
314 (ip-error "~@<Too few arguments in ~S to satisfy lambda list ~S.~:@>"
315 arguments lambda-list
))
316 ((and (not (or rest-p keyword-p
)) keywords-present-p
)
317 (ip-error "~@<Too many arguments in ~S to satisfy lambda list ~S.~:@>"
318 arguments lambda-list
))
319 ((and keyword-p keywords-present-p
320 (oddp (- arguments-present non-keyword-arguments
)))
321 (ip-error "~@<Odd number of &KEY arguments in ~S for ~S.~:@>"
322 arguments lambda-list
)))
323 (dotimes (i required-length
)
324 (push (cons (pop required
) (pop arguments
)) let-like-bindings
))
325 (do ((optionals-parsed 0 (1+ optionals-parsed
)))
327 (let ((this-optional (pop optional
))
328 (supplied-p (< optionals-parsed optionals-present
)))
329 (push (cons (binding-name this-optional
)
331 (list 'quote
(pop arguments
))
332 (binding-value this-optional
)))
334 (when (supplied-p-parameter this-optional
)
335 (push (cons (supplied-p-parameter this-optional
)
336 (list 'quote supplied-p
))
337 let
*-like-bindings
))))
338 (let ((keyword-plist arguments
))
340 (push (cons rest
(list 'quote keyword-plist
)) let
*-like-bindings
))
342 (unless (or allow-other-keys-p
343 (getf keyword-plist
:allow-other-keys
))
344 (loop for
(key value
) on keyword-plist by
#'cddr doing
345 (when (and (not (eq key
:allow-other-keys
))
346 (not (member key keyword
:key
#'keyword-key
)))
347 (ip-error "~@<Unknown &KEY argument ~S in ~S for ~S.~:@>"
348 key original-arguments lambda-list
))))
349 (dolist (keyword-spec keyword
)
350 (let ((supplied (getf keyword-plist
(keyword-key keyword-spec
)
352 (push (cons (keyword-name keyword-spec
)
353 (if (eq supplied
*not-present
*)
354 (keyword-default-value keyword-spec
)
355 (list 'quote supplied
)))
357 (when (supplied-p-parameter keyword-spec
)
358 (push (cons (supplied-p-parameter keyword-spec
)
359 (list 'quote
(not (eq supplied
*not-present
*))))
360 let
*-like-bindings
))))))
364 (let ((this-aux (pop aux
)))
365 (push (cons (binding-name this-aux
)
366 (binding-value this-aux
))
367 let
*-like-bindings
))))
368 (values (nreverse let-like-bindings
) (nreverse let
*-like-bindings
)))))
370 ;;; Evaluate LET*-like (sequential) bindings.
372 ;;; Given an alist of BINDINGS, evaluate the value form of the first
373 ;;; binding in ENV, generate an augmented environment with a binding
374 ;;; of the variable to the value in ENV, and then evaluate the next
375 ;;; binding form. Once all binding forms have been handled, END-ACTION
376 ;;; is funcalled with the final environment.
378 ;;; SPECIALS is a list of variables that have a bound special declaration.
379 ;;; These variables (and those that have been declaimed as special) are
380 ;;; bound as special variables.
381 (defun eval-next-let*-binding
(bindings specials env end-action
)
382 (flet ((maybe-eval (exp)
383 ;; Pick off the easy (QUOTE x) case which is very common
384 ;; due to function calls. (see PARSE-ARGUMENTS)
385 (if (and (consp exp
) (eq (car exp
) 'quote
))
389 (let* ((binding-name (car (car bindings
)))
390 (binding-value (cdr (car bindings
)))
391 (new-env (make-env :parent env
)))
392 (if (specialp binding-name specials
)
395 (list (maybe-eval binding-value
))
396 ;; Mark the variable as special in this environment
397 (push-var binding-name
*special
* new-env
)
398 (eval-next-let*-binding
399 (cdr bindings
) specials new-env end-action
))
401 (push-var binding-name
(maybe-eval binding-value
) new-env
)
402 (eval-next-let*-binding
403 (cdr bindings
) specials new-env end-action
))))
404 (funcall end-action env
))))
406 ;;; Create a new environment based on OLD-ENV by adding the variable
407 ;;; bindings in BINDINGS to it, and call FUNCTION with the new environment
408 ;;; as the only parameter. DECLARATIONS are the declarations that were
409 ;;; in a source position where bound declarations for the bindings could
412 ;;; FREE-SPECIALS-P controls whether all special declarations should
413 ;;; end cause the variables to be marked as special in the environment
414 ;;; (when true), or only bound declarations (when false). Basically
415 ;;; it'll be T when handling a LET, and NIL when handling a call to an
416 ;;; interpreted function.
417 (defun call-with-new-env (old-env bindings declarations
418 free-specials-p function
)
419 (let* ((specials (declared-specials declarations
))
421 (dynamic-values nil
))
422 ;; To check for package-lock violations
423 (special-bindings specials old-env
)
424 (flet ((generate-binding (binding)
425 (if (specialp (car binding
) specials
)
426 ;; If the variable being bound is globally special or
427 ;; there's a bound special declaration for it, record it
428 ;; in DYNAMIC-VARS / -VALUES separately:
429 ;; * To handle the case of FREE-SPECIALS-P == T more
431 ;; * The dynamic variables will be bound with PROGV just
434 (push (car binding
) dynamic-vars
)
435 (push (cdr binding
) dynamic-values
)
437 ;; Otherwise it's a lexical binding, and the value
438 ;; will be recorded in the environment.
440 (let ((new-env (make-env
442 :vars
(mapcan #'generate-binding bindings
)
443 :declarations declarations
)))
444 (dolist (special (if free-specials-p specials dynamic-vars
))
445 (push-var special
*special
* new-env
))
447 (progv dynamic-vars dynamic-values
448 (funcall function new-env
))
449 ;; When there are no specials, the PROGV would be a no-op,
450 ;; but it's better to elide it completely, since the
451 ;; funcall is then in tail position.
452 (funcall function new-env
))))))
454 ;;; Create a new environment based on OLD-ENV by binding the argument
455 ;;; list ARGUMENTS to LAMBDA-LIST, and call FUNCTION with the new
456 ;;; environment as argument. DECLARATIONS are the declarations that
457 ;;; were in a source position where bound declarations for the
458 ;;; bindings could be introduced.
459 (defun call-with-new-env-full-parsing
460 (old-env lambda-list arguments declarations function
)
461 (multiple-value-bind (let-like-bindings let
*-like-binding
)
462 (parse-arguments arguments lambda-list
)
463 (let ((specials (declared-specials declarations
))
464 var-specials free-specials
)
465 ;; Separate the bound and free special declarations
466 (dolist (special specials
)
467 (if (or (member special let-like-bindings
:key
#'car
)
468 (member special let
*-like-binding
:key
#'car
))
469 (push special var-specials
)
470 (push special free-specials
)))
471 ;; First introduce the required parameters into the environment
472 ;; with CALL-WITH-NEW-ENV
474 old-env let-like-bindings declarations nil
476 ;; Then deal with optionals / keywords / etc.
477 (eval-next-let*-binding
478 let
*-like-binding var-specials env
480 ;; And now that we have evaluated all the
481 ;; initialization forms for the bindings, add the free
482 ;; special declarations to the environment. To see why
483 ;; this is the right thing to do (instead of passing
484 ;; FREE-SPECIALS-P == T to CALL-WITH-NEW-ENV),
487 ;; (eval '(let ((*a* 1))
488 ;; (declare (special *a*))
490 ;; (funcall (lambda (&optional (b *a*))
491 ;; (declare (special *a*))
492 ;; (values b *a*))))))
494 ;; *A* should be special in the body of the lambda, but
495 ;; not when evaluating the default value of B.
496 (dolist (special free-specials
)
497 (push-var special
*special
* env
))
498 (funcall function env
))))))))
500 ;;; Set the VALUE of the binding (either lexical or special) of the
501 ;;; variable named by SYMBOL in the environment ENV.
502 (defun set-variable (symbol value env
)
503 (let ((binding (get-binding symbol env
)))
506 ((eq (cdr binding
) *special
*)
507 (setf (symbol-value symbol
) value
))
508 ((eq (cdr binding
) *symbol-macro
*)
509 (error "Tried to set a symbol-macrolet!"))
510 (t (setf (cdr binding
) value
)))
511 (case (sb!int
:info
:variable
:kind symbol
)
512 (:macro
(error "Tried to set a symbol-macrolet!"))
513 (:alien
(let ((type (sb!int
:info
:variable
:alien-info symbol
)))
514 (setf (sb!alien
::%heap-alien type
) value
)))
516 (let ((type (sb!c
::info
:variable
:type symbol
)))
518 (let ((type-specifier (type-specifier type
)))
519 (unless (typep value type-specifier
)
522 :expected-type type-specifier
))))
523 (setf (symbol-value symbol
) value
)))))))
525 ;;; Retrieve the value of the binding (either lexical or special) of
526 ;;; the variable named by SYMBOL in the environment ENV. For symbol
527 ;;; macros the expansion is returned instead.
528 (defun get-variable (symbol env
)
529 (let ((binding (get-binding symbol env
)))
532 ((eq (cdr binding
) *special
*)
533 (values (symbol-value symbol
) :variable
))
534 ((eq (cdr binding
) *symbol-macro
*)
535 (values (cdr (get-symbol-expansion-binding symbol env
))
537 (t (values (cdr binding
) :variable
)))
538 (case (sb!int
:info
:variable
:kind symbol
)
539 (:macro
(values (macroexpand-1 symbol
) :expansion
))
540 (:alien
(values (sb!alien-internals
:alien-value symbol
) :variable
))
541 (t (values (symbol-value symbol
) :variable
))))))
543 ;;; Retrieve the function/macro binding of the symbol NAME in
544 ;;; environment ENV. The second return value will be :MACRO for macro
545 ;;; bindings, :FUNCTION for function bindings.
546 (defun get-function (name env
)
547 (let ((binding (get-fbinding name env
)))
550 ((eq (cdr binding
) *macro
*)
551 (values (cdr (get-expander-binding name env
)) :macro
))
552 (t (values (cdr binding
) :function
)))
554 ((and (symbolp name
) (macro-function name
))
555 (values (macro-function name
) :macro
))
556 (t (values (%coerce-name-to-fun name
) :function
))))))
558 ;;; Return true if EXP is a lambda form.
561 ((lambda sb
!int
:named-lambda
) t
)))
563 ;;; Split off the declarations (and the docstring, if
564 ;;; DOC-STRING-ALLOWED is true) from the actual forms of BODY.
565 ;;; Returns three values: the cons in BODY containing the first
566 ;;; non-header subform, the docstring, and a list of the declarations.
568 ;;; FIXME: The name of this function is somewhat misleading. It's not
569 ;;; used just for parsing the headers from lambda bodies, but for all
570 ;;; special forms that have attached declarations.
571 (defun parse-lambda-headers (body &key doc-string-allowed
)
572 (loop with documentation
= nil
573 with declarations
= nil
574 with lambda-list
= :unspecified
577 ((and doc-string-allowed
(stringp (car form
)))
578 (if (cdr form
) ; CLHS 3.4.11
580 (ip-error "~@<Duplicate doc string ~S.~:@>" (car form
))
581 (setf documentation
(car form
)))
582 (return (values form documentation declarations
))))
583 ((and (consp (car form
)) (eql (caar form
) 'declare
))
584 (when (eq lambda-list
:unspecified
)
585 (dolist (item (cdar form
))
586 (when (and (consp item
) (eq (car item
) 'sb
!c
::lambda-list
))
587 (setq lambda-list
(second item
)))))
588 (setf declarations
(append declarations
(cdar form
))))
589 (t (return (values form documentation declarations lambda-list
))))
590 finally
(return (values nil documentation declarations lambda-list
))))
592 ;;; Create an interpreted function from the lambda-form EXP evaluated
593 ;;; in the environment ENV.
594 (defun eval-lambda (exp env
)
595 (sb!int
:binding
* (((name rest
)
597 ((lambda) (values nil
(cdr exp
)))
598 ((sb!int
:named-lambda
) (values (second exp
) (cddr exp
)))))
599 (lambda-list (car rest
))
600 ((forms documentation declarations debug-lambda-list
)
601 (parse-lambda-headers (cdr rest
) :doc-string-allowed t
)))
602 (make-interpreted-function :name name
603 :lambda-list lambda-list
605 (if (eq debug-lambda-list
:unspecified
)
606 lambda-list debug-lambda-list
)
608 :documentation documentation
609 :source-location
(sb!c
::make-definition-source-location
)
610 :declarations declarations
)))
612 (defun eval-progn (body env
)
613 (let ((previous-exp nil
))
616 (%eval previous-exp env
))
617 (setf previous-exp exp
))
618 ;; Preserve tail call
619 (%eval previous-exp env
)))
621 (defun eval-if (body env
)
622 (program-destructuring-bind (test if-true
&optional if-false
) body
625 (%eval if-false env
))))
627 (defun eval-let (body env
)
628 (program-destructuring-bind (bindings &body body
) body
629 ;; First evaluate the bindings in parallel
630 (let ((bindings (mapcar
632 (cons (binding-name binding
)
633 (%eval
(binding-value binding
) env
)))
635 (multiple-value-bind (body documentation declarations
)
636 (parse-lambda-headers body
:doc-string-allowed nil
)
637 (declare (ignore documentation
))
638 ;; Then establish them into the environment, and evaluate the
640 (call-with-new-env env bindings declarations t
642 (eval-progn body env
)))))))
644 (defun eval-let* (body old-env
)
645 (program-destructuring-bind (bindings &body body
) body
646 (multiple-value-bind (body documentation declarations
)
647 (parse-lambda-headers body
:doc-string-allowed nil
)
648 (declare (ignore documentation
))
649 ;; First we separate the special declarations into bound and
650 ;; free declarations.
651 (let ((specials (declared-specials declarations
))
652 var-specials free-specials
)
653 (dolist (special specials
)
654 (if (member special bindings
:key
#'binding-name
)
655 (push special var-specials
)
656 (push special free-specials
)))
657 (let ((env (make-env :parent old-env
658 :declarations declarations
)))
659 ;; Then we establish the bindings into the environment
661 (eval-next-let*-binding
662 (mapcar #'(lambda (binding)
663 (cons (binding-name binding
)
664 (binding-value binding
)))
668 ;; Now that we're done evaluating the bindings, add the
669 ;; free special declarations. See also
670 ;; CALL-WITH-NEW-ENV-FULL-PARSING.
671 (dolist (special free-specials
)
672 (push-var special
*special
* env
))
673 (eval-progn body env
))))))))
675 ;; Return a named local function in the environment ENV, made from the
676 ;; definition form FUNCTION-DEF.
677 (defun eval-local-function-def (function-def env
)
678 (program-destructuring-bind (name lambda-list
&body local-body
) function-def
679 (multiple-value-bind (local-body documentation declarations
)
680 (parse-lambda-headers local-body
:doc-string-allowed t
)
681 (%eval
`#'(sb!int
:named-lambda
,name
,lambda-list
685 (declare ,@declarations
)
686 (block ,(cond ((consp name
) (second name
))
691 (defun eval-flet (body 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 (let* ((specials (declared-specials declarations
))
697 (new-env (make-env :parent env
698 :vars
(special-bindings specials env
)
699 :declarations declarations
)))
700 (dolist (function-def local-functions
)
701 (push-fun (car function-def
)
702 ;; Evaluate the function definitions in ENV.
703 (eval-local-function-def function-def env
)
704 ;; Do package-lock checks in ENV.
706 ;; But add the bindings to the child environment.
708 (eval-progn body new-env
)))))
710 (defun eval-labels (body old-env
)
711 (program-destructuring-bind ((&rest local-functions
) &body body
) body
712 (multiple-value-bind (body documentation declarations
)
713 (parse-lambda-headers body
:doc-string-allowed nil
)
714 (declare (ignore documentation
))
715 ;; Create a child environment, evaluate the function definitions
716 ;; in it, and add them into the same environment.
717 (let ((env (make-env :parent old-env
718 :declarations declarations
)))
719 (dolist (function-def local-functions
)
720 (push-fun (car function-def
)
721 (eval-local-function-def function-def env
)
724 ;; And then add an environment for the body of the LABELS. A
725 ;; separate environment from the one where we added the
726 ;; functions to is needed, since any special variable
727 ;; declarations need to be in effect in the body, but not in
728 ;; the bodies of the local functions.
729 (let* ((specials (declared-specials declarations
))
730 (new-env (make-env :parent env
731 :vars
(special-bindings specials env
))))
732 (eval-progn body new-env
))))))
734 ;; Return a local macro-expander in the environment ENV, made from the
735 ;; definition form FUNCTION-DEF.
736 (defun eval-local-macro-def (function-def env
)
737 (program-destructuring-bind (name lambda-list
&body local-body
) function-def
738 (%eval
(sb!int
:make-macro-lambda nil
; the lambda is anonymous.
739 lambda-list local-body
743 (defun eval-macrolet (body env
)
744 (program-destructuring-bind ((&rest local-functions
) &body body
) body
745 (flet ((generate-fbinding (macro-def)
746 (cons (car macro-def
) *macro
*))
747 (generate-mbinding (macro-def)
748 (let ((name (car macro-def
))
749 (sb!c
:*lexenv
* (env-native-lexenv env
)))
751 (program-assert-symbol-home-package-unlocked
752 :eval name
"binding ~A as a local macro"))
753 (cons name
(eval-local-macro-def macro-def env
)))))
754 (multiple-value-bind (body documentation declarations
)
755 (parse-lambda-headers body
:doc-string-allowed nil
)
756 (declare (ignore documentation
))
757 (let* ((specials (declared-specials declarations
))
758 (new-env (make-env :parent env
759 :vars
(special-bindings specials env
)
760 :funs
(mapcar #'generate-fbinding
762 :expanders
(mapcar #'generate-mbinding
764 :declarations declarations
)))
765 (eval-progn body new-env
))))))
767 (defun eval-symbol-macrolet (body env
)
768 (program-destructuring-bind ((&rest bindings
) &body body
) body
769 (flet ((generate-binding (binding)
770 (cons (car binding
) *symbol-macro
*))
771 (generate-sm-binding (binding)
772 (let ((name (car binding
))
773 (sb!c
:*lexenv
* (env-native-lexenv env
)))
774 (when (or (boundp name
)
775 (eq (sb!int
:info
:variable
:kind name
) :macro
))
776 (program-assert-symbol-home-package-unlocked
777 :eval name
"binding ~A as a local symbol-macro"))
778 (cons name
(second binding
)))))
779 (multiple-value-bind (body documentation declarations
)
780 (parse-lambda-headers body
:doc-string-allowed nil
)
781 (declare (ignore documentation
))
782 (let ((specials (declared-specials declarations
)))
783 (dolist (binding bindings
)
784 (when (specialp (binding-name binding
) specials
)
785 (ip-error "~@<Can't bind SYMBOL-MACROLET of special ~
787 (binding-name binding
)))))
788 (let* ((specials (declared-specials declarations
))
789 (new-env (make-env :parent env
790 :vars
(nconc-2 (mapcar #'generate-binding
792 (special-bindings specials env
))
793 :symbol-expansions
(mapcar
794 #'generate-sm-binding
796 :declarations declarations
)))
797 (eval-progn body new-env
))))))
799 (defun eval-progv (body env
)
800 (program-destructuring-bind (vars vals
&body body
) body
801 (progv (%eval vars env
) (%eval vals env
)
802 (eval-progn body env
))))
804 (defun eval-function (body env
)
805 (program-destructuring-bind (name) body
807 ;; LAMBDAP assumes that the argument is a cons, so we need the
808 ;; initial symbol case, instead of relying on the fall-through
809 ;; case that has the same function body.
810 ((symbolp name
) (nth-value 0 (get-function name env
)))
811 ((lambdap name
) (eval-lambda name env
))
812 (t (nth-value 0 (get-function name env
))))))
814 (defun eval-eval-when (body env
)
815 (program-destructuring-bind ((&rest situation
) &body body
) body
816 ;; FIXME: check that SITUATION only contains valid situations
817 (if (or (member :execute situation
)
818 (member 'eval situation
))
819 (eval-progn body env
))))
821 (defun eval-quote (body env
)
822 (declare (ignore env
))
823 (program-destructuring-bind (object) body
826 (defun eval-setq (pairs env
)
827 (when (oddp (length pairs
))
828 (ip-error "~@<Odd number of args to SETQ: ~S~:@>" (cons 'setq pairs
)))
830 (loop for
(var new-val
) on pairs by
#'cddr do
832 (multiple-value-bind (expansion type
) (get-variable var env
)
836 (%eval
(list 'setf expansion new-val
) env
)))
838 (setf last
(set-variable var
(%eval new-val env
)
840 (unbound-variable (c)
842 (setf last
(setf (symbol-value var
)
843 (%eval new-val env
))))))
846 (defun eval-multiple-value-call (body env
)
847 (program-destructuring-bind (function-form &body forms
) body
848 (%apply
(%eval function-form env
)
849 (loop for form in forms
850 nconc
(multiple-value-list (%eval form env
))))))
852 (defun eval-multiple-value-prog1 (body env
)
853 (program-destructuring-bind (first-form &body forms
) body
854 (multiple-value-prog1 (%eval first-form env
)
855 (eval-progn forms env
))))
857 (defun eval-catch (body env
)
858 (program-destructuring-bind (tag &body forms
) body
859 (catch (%eval tag env
)
860 (eval-progn forms env
))))
862 (defun eval-tagbody (body old-env
)
863 (let ((env (make-env :parent old-env
))
868 (flet ((go-to-tag (tag)
869 (setf target-tag tag
)
871 ;; For each tag, store a trampoline function into the environment
872 ;; and the location in the body into the TAGS alist.
873 (do ((form body
(cdr form
)))
875 (when (atom (car form
))
876 (when (assoc (car form
) tags
)
877 (ip-error "The tag :A appears more than once in a tagbody."))
878 (push (cons (car form
) (cdr form
)) tags
)
879 (push (cons (car form
) #'go-to-tag
) (env-tags env
)))))
880 ;; And then evaluate the forms in the body, starting from the
884 ;; The trampoline has set the TARGET-TAG. Restart evaluation of
885 ;; the body from the location in body that matches the tag.
886 (setf start
(cdr (assoc target-tag tags
)))
889 (when (not (atom form
))
890 (%eval form env
))))))
892 (defun eval-go (body env
)
893 (program-destructuring-bind (tag) body
894 (let ((target (get-tag-binding tag env
)))
896 ;; Call the GO-TO-TAG trampoline
897 (funcall (cdr target
) tag
)
898 (ip-error "~@<Attempt to GO to nonexistent tag: ~S~:@>" tag
)))))
900 (defun eval-block (body old-env
)
901 (flet ((return-from-eval-block (&rest values
)
902 (return-from eval-block
(values-list values
))))
903 (program-destructuring-bind (name &body body
) body
904 (unless (symbolp name
)
905 (ip-error "~@<The block name ~S is not a symbol.~:@>" name
))
907 :blocks
(list (cons name
#'return-from-eval-block
))
909 (eval-progn body env
)))))
911 (defun eval-return-from (body env
)
912 (program-destructuring-bind (name &optional result
) body
913 (let ((target (get-block-binding name env
)))
915 (multiple-value-call (cdr target
) (%eval result env
))
916 (ip-error "~@<Return for unknown block: ~S~:@>" name
)))))
918 (defun eval-the (body env
)
919 (program-destructuring-bind (value-type form
) body
920 (let ((values (multiple-value-list (%eval form env
)))
921 (vtype (if (ctype-p value-type
) value-type
(values-specifier-type value-type
))))
922 ;; FIXME: we should probably do this only if SAFETY>SPEED
924 ((eq vtype
*wild-type
*) (values-list values
))
925 ((values-type-p vtype
)
926 (do ((vs values
(cdr vs
))
927 (ts (values-type-required vtype
) (cdr ts
)))
929 (do ((vs vs
(cdr vs
))
930 (ts (values-type-optional vtype
) (cdr ts
)))
932 (do ((vs vs
(cdr vs
))
933 (rest (values-type-rest vtype
)))
934 ((null vs
) (values-list values
))
936 (unless (%%typep
(car vs
) rest nil
)
937 (error 'type-error
:datum
(car vs
) :expected-type
(type-specifier rest
)))
938 (error 'type-error
:datum vs
:expected-type nil
))))
942 (unless (%%typep v type nil
)
943 (error 'type-error
:datum v
:expected-type
(type-specifier type
)))))))
946 (unless (%%typep v type nil
)
947 (error 'type-error
:datum v
:expected-type
(type-specifier type
))))))
949 ((%%typep
(car values
) vtype nil
) (values-list values
))
950 (t (error 'type-error
:datum
(car values
) :expected-type
(type-specifier vtype
)))))))
952 (defun eval-unwind-protect (body env
)
953 (program-destructuring-bind (protected-form &body cleanup-forms
) body
954 (unwind-protect (%eval protected-form env
)
955 (eval-progn cleanup-forms env
))))
957 (defun eval-throw (body env
)
958 (program-destructuring-bind (tag result-form
) body
959 (throw (%eval tag env
)
960 (%eval result-form env
))))
962 (defun eval-load-time-value (body env
)
963 (program-destructuring-bind (form &optional read-only-p
) body
964 (declare (ignore read-only-p
))
967 (defun eval-locally (body env
)
968 (multiple-value-bind (body documentation declarations
)
969 (parse-lambda-headers body
:doc-string-allowed nil
)
970 (declare (ignore documentation
))
971 (let* ((specials (declared-specials declarations
))
972 (new-env (if (or specials declarations
)
973 (make-env :parent env
974 :vars
(special-bindings specials env
)
975 :declarations declarations
)
977 (eval-progn body new-env
))))
979 (defun eval-args (args env
)
980 (mapcar #'(lambda (arg) (%eval arg env
)) args
))
982 ;;; The expansion of SB-SYS:WITH-PINNED-OBJECTS on GENCGC uses some
983 ;;; VOPs which can't be reasonably implemented in the interpreter. So
984 ;;; we special-case the macro.
985 (defun eval-with-pinned-objects (args env
)
986 (program-destructuring-bind (values &body body
) args
988 (eval-progn body env
)
989 (sb!sys
:with-pinned-objects
((car values
))
990 (eval-with-pinned-objects (cons (cdr values
) body
) env
)))))
992 (defvar *eval-dispatch-functions
* nil
)
994 ;;; Dispatch to the appropriate EVAL-FOO function based on the contents of EXP.
995 (declaim (inline %%eval
))
996 (defun %%eval
(exp env
)
999 ;; CLHS 3.1.2.1.1 Symbols as Forms
1000 (multiple-value-bind (value kind
) (get-variable exp env
)
1003 (:expansion
(%eval value env
)))))
1004 ;; CLHS 3.1.2.1.3 Self-Evaluating Objects
1006 ;; CLHS 3.1.2.1.2 Conses as Forms
1009 ;; CLHS 3.1.2.1.2.1 Special Forms
1010 ((block) (eval-block (cdr exp
) env
))
1011 ((catch) (eval-catch (cdr exp
) env
))
1012 ((eval-when) (eval-eval-when (cdr exp
) env
))
1013 ((flet) (eval-flet (cdr exp
) env
))
1014 ((function) (eval-function (cdr exp
) env
))
1015 ((go) (eval-go (cdr exp
) env
))
1016 ((if) (eval-if (cdr exp
) env
))
1017 ((labels) (eval-labels (cdr exp
) env
))
1018 ((let) (eval-let (cdr exp
) env
))
1019 ((let*) (eval-let* (cdr exp
) env
))
1020 ((load-time-value) (eval-load-time-value (cdr exp
) env
))
1021 ((locally) (eval-locally (cdr exp
) env
))
1022 ((macrolet) (eval-macrolet (cdr exp
) env
))
1023 ((multiple-value-call) (eval-multiple-value-call (cdr exp
) env
))
1024 ((multiple-value-prog1) (eval-multiple-value-prog1 (cdr exp
) env
))
1025 ((progn) (eval-progn (cdr exp
) env
))
1026 ((progv) (eval-progv (cdr exp
) env
))
1027 ((quote) (eval-quote (cdr exp
) env
))
1028 ((return-from) (eval-return-from (cdr exp
) env
))
1029 ((setq) (eval-setq (cdr exp
) env
))
1030 ((symbol-macrolet) (eval-symbol-macrolet (cdr exp
) env
))
1031 ((tagbody) (eval-tagbody (cdr exp
) env
))
1032 ((the) (eval-the (cdr exp
) env
))
1033 ((throw) (eval-throw (cdr exp
) env
))
1034 ((unwind-protect) (eval-unwind-protect (cdr exp
) env
))
1036 ((truly-the) (eval-the (cdr exp
) env
))
1037 ;; Not a special form, but a macro whose expansion wouldn't be
1038 ;; handled correctly by the evaluator.
1039 ((sb!sys
:with-pinned-objects
) (eval-with-pinned-objects (cdr exp
) env
))
1041 (let ((dispatcher (getf *eval-dispatch-functions
* (car exp
))))
1044 (funcall dispatcher exp env
))
1045 ;; CLHS 3.1.2.1.2.4 Lambda Forms
1046 ((and (consp (car exp
)) (eq (caar exp
) 'lambda
))
1047 (interpreted-apply (eval-function (list (car exp
)) env
)
1048 (eval-args (cdr exp
) env
)))
1050 (multiple-value-bind (function kind
) (get-function (car exp
) env
)
1052 ;; CLHS 3.1.2.1.2.3 Function Forms
1053 (:function
(%apply function
(eval-args (cdr exp
) env
)))
1054 ;; CLHS 3.1.2.1.2.2 Macro Forms
1056 (let ((hook (valid-macroexpand-hook)))
1057 (%eval
(funcall (truly-the function hook
)
1060 (env-native-lexenv env
))
1063 (defun %eval
(exp env
)
1066 ;; Dynamically binding *EVAL-LEVEL* will prevent tail call
1067 ;; optimization. So only do it when its value will be used for
1068 ;; printing debug output.
1069 (let ((*eval-level
* (1+ *eval-level
*)))
1070 (let ((*print-circle
* t
))
1071 (format t
"~&~vA~S~%" *eval-level
* "" `(%eval
,exp
)))
1075 (defun %apply
(fun args
)
1077 (interpreted-function (interpreted-apply fun args
))
1078 (function (apply fun args
))
1079 (symbol (apply fun args
))))
1081 (defun interpreted-apply (fun args
)
1082 (let ((lambda-list (interpreted-function-lambda-list fun
))
1083 (env (interpreted-function-env fun
))
1084 (body (interpreted-function-body fun
))
1085 (declarations (interpreted-function-declarations fun
)))
1086 (call-with-new-env-full-parsing
1087 env lambda-list args declarations
1089 (eval-progn body env
)))))
1091 ;;; We need separate conditions for the different *-TOO-COMPLEX-ERRORs to
1092 ;;; avoid spuriously triggering the handler in EVAL-IN-NATIVE-ENVIRONMENT
1095 ;;; (let ((sb-ext:*evaluator-mode* :interpret))
1096 ;;; (let ((fun (eval '(let ((a 1)) (lambda () a)))))
1097 ;;; (eval `(compile nil ,fun))))
1099 ;;; FIXME: should these be exported?
1100 (define-condition interpreter-environment-too-complex-error
(simple-error)
1102 (define-condition compiler-environment-too-complex-error
(simple-error)
1105 ;;; Try to compile an interpreted function. If the environment
1106 ;;; contains local functions or lexical variables we'll punt on
1108 (defun prepare-for-compile (function)
1109 (let ((env (interpreted-function-env function
)))
1110 (when (or (env-tags env
)
1112 (find-if-not #'(lambda (x) (eq x
*macro
*))
1113 (env-funs env
) :key
#'cdr
)
1114 (find-if-not #'(lambda (x) (eq x
*symbol-macro
*))
1117 (error 'interpreter-environment-too-complex-error
1119 "~@<Lexical environment of ~S is too complex to compile.~:@>"
1123 `(sb!int
:named-lambda
,(interpreted-function-name function
)
1124 ,(interpreted-function-lambda-list function
)
1125 (declare ,@(interpreted-function-declarations function
))
1126 ,@(interpreted-function-body function
))
1127 (env-native-lexenv env
))))
1129 ;;; Convert a compiler LEXENV to an interpreter ENV. This is needed
1130 ;;; for EVAL-IN-LEXENV.
1131 (defun make-env-from-native-environment (lexenv)
1132 (let ((native-funs (sb!c
::lexenv-funs lexenv
))
1133 (native-vars (sb!c
::lexenv-vars lexenv
)))
1134 (flet ((is-macro (thing)
1135 (and (consp thing
) (eq (car thing
) 'sb
!sys
:macro
))))
1136 (when (or (sb!c
::lexenv-blocks lexenv
)
1137 (sb!c
::lexenv-cleanup lexenv
)
1138 (sb!c
::lexenv-lambda lexenv
)
1139 (sb!c
::lexenv-tags lexenv
)
1140 (sb!c
::lexenv-type-restrictions lexenv
)
1141 (find-if-not #'is-macro native-funs
:key
#'cdr
)
1142 (find-if-not #'is-macro native-vars
:key
#'cdr
))
1143 (error 'compiler-environment-too-complex-error
1145 "~@<Lexical environment is too complex to evaluate in: ~S~:@>"
1148 (flet ((make-binding (native)
1149 (cons (car native
) *symbol-macro
*))
1150 (make-sm-binding (native)
1151 (cons (car native
) (cddr native
)))
1152 (make-fbinding (native)
1153 (cons (car native
) *macro
*))
1154 (make-mbinding (native)
1155 (cons (car native
) (cddr native
))))
1157 (mapcar #'make-binding native-vars
)
1158 (mapcar #'make-fbinding native-funs
)
1159 (mapcar #'make-mbinding native-funs
)
1160 (mapcar #'make-sm-binding native-vars
)
1166 (defun eval-in-environment (form env
)
1169 (defun eval-in-native-environment (form lexenv
)
1171 ((sb!impl
::eval-error
1173 (error 'interpreted-program-error
1174 :condition
(sb!int
:encapsulated-condition condition
)
1176 (sb!c
:with-compiler-error-resignalling
1178 (let ((env (make-env-from-native-environment lexenv
)))
1180 (compiler-environment-too-complex-error (condition)
1181 (declare (ignore condition
))
1182 (sb!int
:style-warn
'lexical-environment-too-complex
1183 :form form
:lexenv lexenv
)
1184 (sb!int
:simple-eval-in-lexenv form lexenv
))))))