fix another LET*/:interpret bug
[sbcl.git] / src / code / full-eval.lisp
1 ;;;; An interpreting EVAL
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!EVAL")
13
14 ;; (declaim (optimize (speed 3) (debug 1) (safety 1)))
15
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"))
21
22 (define-condition interpreted-program-error (program-error simple-condition sb!impl::encapsulated-condition)
23   ()
24   (:report (lambda (condition stream)
25              (if (slot-boundp condition 'condition)
26                  (progn
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))))))
32
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
36                                            program-error)
37   ())
38
39 (defun arg-count-program-error (datum &rest arguments)
40   (declare (ignore datum))
41   (apply #'error 'arg-count-program-error arguments))
42
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
49                                   :anonymousp t
50                                   :doc-string-allowed nil
51                                   :wrap-block nil
52                                   :error-fun 'arg-count-program-error)
53       `(let ((,arg-list-name ,arg-list))
54          ,@local-decls
55          ,body))))
56
57 (defun ip-error (format-control &rest format-arguments)
58   (error 'interpreted-program-error
59          :format-control format-control
60          :format-arguments format-arguments))
61
62 (defmacro nconc-2 (a b)
63   (let ((tmp (gensym))
64         (tmp2 (gensym)))
65     `(let ((,tmp ,a)
66            (,tmp2 ,b))
67        (if ,tmp
68            (progn (setf (cdr (last ,tmp)) ,tmp2) ,tmp)
69            ,tmp2))))
70
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
77                                          declarations)
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*)
86                  (cons (car binding)
87                        (cons 'sb!sys:macro
88                              (cdr (assoc (car binding) new-expanders))))
89                  (cons (car binding)
90                        :bogus)))
91            (to-native-vars (binding)
92              ;; And likewise for symbol macros.
93              (if (eq (cdr binding) *symbol-macro*)
94                  (cons (car binding)
95                        (cons 'sb!sys:macro
96                              (cdr (assoc (car binding) new-symbol-expansions))))
97                  (cons (car binding)
98                        :bogus))))
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))
104                    nil nil nil nil nil
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                    (sb!c::lexenv-user-data old-lexenv))))
109       (dolist (declaration declarations)
110         (unless (consp declaration)
111           (ip-error "malformed declaration specifier ~S in ~S"
112                     declaration (cons 'declare declarations)))
113         (case (car declaration)
114           ((optimize)
115            (dolist (element (cdr declaration))
116              (multiple-value-bind (quality value)
117                  (if (not (consp element))
118                      (values element 3)
119                      (program-destructuring-bind (quality value)
120                          element
121                        (values quality value)))
122                (if (sb!c::policy-quality-name-p quality)
123                    (push (cons quality value)
124                          (sb!c::lexenv-%policy lexenv))
125                    (warn "ignoring unknown optimization quality ~
126                                       ~S in ~S" quality
127                                       (cons 'declare declarations))))))
128           (sb!ext:muffle-conditions
129            (setf (sb!c::lexenv-handled-conditions lexenv)
130                  (sb!c::process-muffle-conditions-decl
131                   declaration
132                   (sb!c::lexenv-handled-conditions lexenv))))
133           (sb!ext:unmuffle-conditions
134            (setf (sb!c::lexenv-handled-conditions lexenv)
135                  (sb!c::process-unmuffle-conditions-decl
136                   declaration
137                   (sb!c::lexenv-handled-conditions lexenv))))
138           ((sb!ext:disable-package-locks sb!ext:enable-package-locks)
139            (setf (sb!c::lexenv-disabled-package-locks lexenv)
140                  (sb!c::process-package-lock-decl
141                   declaration
142                   (sb!c::lexenv-disabled-package-locks lexenv))))))
143       lexenv)))
144
145 (defstruct (env
146              (:constructor %make-env
147                            (parent vars funs expanders symbol-expansions
148                             tags blocks declarations native-lexenv)))
149   parent
150   vars
151   funs
152   expanders
153   symbol-expansions
154   tags
155   blocks
156   declarations
157   native-lexenv)
158
159 (defun make-env (&key parent vars funs expanders
160                  symbol-expansions tags blocks declarations)
161   (%make-env parent
162              (append vars (env-vars parent))
163              (append funs (env-funs parent))
164              (append expanders (env-expanders parent))
165              (append symbol-expansions (env-symbol-expansions parent))
166              (nconc-2 tags (env-tags parent))
167              (nconc-2 blocks (env-blocks parent))
168              declarations
169              (fabricate-new-native-environment (env-native-lexenv parent)
170                                                funs expanders
171                                                vars symbol-expansions
172                                                declarations)))
173
174 (defun make-null-environment ()
175   (%make-env nil nil nil nil nil nil nil nil
176              (sb!c::internal-make-lexenv
177               nil nil
178               nil nil nil nil nil nil nil
179               sb!c::*policy*
180               nil)))
181
182 ;;; Augment ENV with a special or lexical variable binding
183 (declaim (inline push-var))
184 (defun push-var (name value env)
185   (push (cons name value) (env-vars env))
186   (push (cons name :bogus) (sb!c::lexenv-vars (env-native-lexenv env))))
187
188 ;;; Augment ENV with a local function binding
189 (declaim (inline push-fun))
190 (defun push-fun (name value calling-env body-env)
191   (when (fboundp name)
192     (let ((sb!c:*lexenv* (env-native-lexenv calling-env)))
193       (program-assert-symbol-home-package-unlocked
194        :eval name "binding ~A as a local function")))
195   (push (cons name value) (env-funs body-env))
196   (push (cons name :bogus) (sb!c::lexenv-funs (env-native-lexenv body-env))))
197
198 (sb!int:def!method print-object ((env env) stream)
199   (print-unreadable-object (env stream :type t :identity t)))
200
201 (macrolet ((define-get-binding (name accessor &key (test '#'eq))
202              ;; A macro, sadly, because an inline function here is
203              ;; "too hairy"
204              `(defmacro ,name (symbol env)
205                 `(assoc ,symbol (,',accessor ,env) :test ,',test))))
206   (define-get-binding get-binding env-vars)
207   (define-get-binding get-fbinding env-funs :test #'equal)
208   (define-get-binding get-expander-binding env-expanders)
209   (define-get-binding get-symbol-expansion-binding env-symbol-expansions)
210   (define-get-binding get-tag-binding env-tags :test #'eql)
211   (define-get-binding get-block-binding env-blocks))
212
213 ;;; Return a list of all symbols that are declared special in the
214 ;;; declarations listen in DECLS.
215 (defun declared-specials (decls)
216   (let ((specials nil))
217     (dolist (decl decls)
218       (when (eql (car decl) 'special)
219         (dolist (var (cdr decl))
220           (push var specials))))
221     specials))
222
223 ;;; Given a list of variables that should be marked as special in an
224 ;;; environment, return the appropriate binding forms to be given
225 ;;; to MAKE-ENV.
226 (defun special-bindings (specials env)
227   (mapcar #'(lambda (var)
228               (let ((sb!c:*lexenv* (env-native-lexenv env)))
229                 (program-assert-symbol-home-package-unlocked
230                  :eval var "declaring ~A special"))
231               (cons var *special*))
232           specials))
233
234 ;;; Return true if SYMBOL has been declared special either globally
235 ;;; or is in the DECLARED-SPECIALS list.
236 (defun specialp (symbol declared-specials)
237   (let ((type (sb!int:info :variable :kind symbol)))
238     (cond
239       ((eq type :constant)
240        ;; Horrible place for this, but it works.
241        (ip-error "Can't bind constant symbol: ~S" symbol))
242       ((eq type :global)
243        ;; Ditto...
244        (ip-error "Can't bind a global variable: ~S" symbol))
245       ((eq type :special) t)
246       ((member symbol declared-specials :test #'eq)
247        t)
248       (t nil))))
249
250 (defun binding-name (binding)
251   (if (consp binding) (first binding) binding))
252 (defun binding-value (binding)
253   (if (consp binding) (second binding) nil))
254 (defun supplied-p-parameter (spec)
255   (if (consp spec) (third spec) nil))
256 (defun keyword-name (spec)
257   (if (consp spec)
258       (if (consp (first spec))
259           (second (first spec))
260           (first spec))
261       spec))
262 (defun keyword-key (spec)
263   (if (consp spec)
264       (if (consp (first spec))
265           (first (first spec))
266           (intern (symbol-name (first spec)) "KEYWORD"))
267       (intern (symbol-name spec) "KEYWORD")))
268 (defun keyword-default-value (spec)
269   (if (consp spec) (second spec) nil))
270
271 ;;; Given a list of ARGUMENTS and a LAMBDA-LIST, return two values:
272 ;;;   * An alist[*] mapping the required parameters of the function to
273 ;;;     the corresponding argument values
274 ;;;   * An alist mapping the keyword, optional and rest parameters of
275 ;;;     the function to the corresponding argument values (if supplied)
276 ;;;     or to the parameter's default expression (if not). Supplied-p
277 ;;;     parameters and aux variables are handled in a similar manner.
278 ;;;
279 ;;; For example given the argument list of (1 2) and the lambda-list of
280 ;;; (A &OPTIONAL (B A) (C (1+ A))), we'd return the values
281 ;;; (A . '1) and ((B . '2) (C . (1+ A))).
282 ;;;
283 ;;; Used only for implementing calls to interpreted functions.
284 (defun parse-arguments (arguments lambda-list)
285   (multiple-value-bind (required optional rest-p rest keyword-p
286                         keyword allow-other-keys-p aux-p aux)
287       (handler-bind ((style-warning #'muffle-warning))
288         (sb!int:parse-lambda-list lambda-list))
289     (let* ((original-arguments arguments)
290            (arguments-present (length arguments))
291            (required-length (length required))
292            (optional-length (length optional))
293            (non-keyword-arguments (+ required-length optional-length))
294            (optionals-present (- (min non-keyword-arguments arguments-present)
295                                  required-length))
296            (keywords-present-p (> arguments-present non-keyword-arguments))
297            (let-like-bindings nil)
298            (let*-like-bindings nil))
299       (cond
300         ((< arguments-present required-length)
301          (ip-error "~@<Too few arguments in ~S to satisfy lambda list ~S.~:@>"
302                    arguments lambda-list))
303         ((and (not (or rest-p keyword-p)) keywords-present-p)
304          (ip-error "~@<Too many arguments in ~S to satisfy lambda list ~S.~:@>"
305                    arguments lambda-list))
306         ((and keyword-p keywords-present-p
307               (oddp (- arguments-present non-keyword-arguments)))
308          (ip-error "~@<Odd number of &KEY arguments in ~S for ~S.~:@>"
309                    arguments lambda-list)))
310       (dotimes (i required-length)
311         (push (cons (pop required) (pop arguments)) let-like-bindings))
312       (do ((optionals-parsed 0 (1+ optionals-parsed)))
313           ((null optional))
314         (let ((this-optional (pop optional))
315               (supplied-p (< optionals-parsed optionals-present)))
316           (push (cons (binding-name this-optional)
317                       (if supplied-p
318                           (list 'quote (pop arguments))
319                           (binding-value this-optional)))
320                 let*-like-bindings)
321           (when (supplied-p-parameter this-optional)
322             (push (cons (supplied-p-parameter this-optional)
323                         (list 'quote supplied-p))
324                   let*-like-bindings))))
325       (let ((keyword-plist arguments))
326         (when rest-p
327           (push (cons rest (list 'quote keyword-plist)) let*-like-bindings))
328         (when keyword-p
329           (unless (or allow-other-keys-p
330                       (getf keyword-plist :allow-other-keys))
331             (loop for (key value) on keyword-plist by #'cddr doing
332                   (when (and (not (eq key :allow-other-keys))
333                              (not (member key keyword :key #'keyword-key)))
334                     (ip-error "~@<Unknown &KEY argument ~S in ~S for ~S.~:@>"
335                               key original-arguments lambda-list))))
336           (dolist (keyword-spec keyword)
337             (let ((supplied (getf keyword-plist (keyword-key keyword-spec)
338                                   *not-present*)))
339               (push (cons (keyword-name keyword-spec)
340                           (if (eq supplied *not-present*)
341                               (keyword-default-value keyword-spec)
342                               (list 'quote supplied)))
343                     let*-like-bindings)
344               (when (supplied-p-parameter keyword-spec)
345                 (push (cons (supplied-p-parameter keyword-spec)
346                             (list 'quote (not (eq supplied *not-present*))))
347                       let*-like-bindings))))))
348       (when aux-p
349         (do ()
350             ((null aux))
351           (let ((this-aux (pop aux)))
352             (push (cons (binding-name this-aux)
353                         (binding-value this-aux))
354                   let*-like-bindings))))
355       (values (nreverse let-like-bindings) (nreverse let*-like-bindings)))))
356
357 ;;; Evaluate LET*-like (sequential) bindings.
358 ;;;
359 ;;; Given an alist of BINDINGS, evaluate the value form of the first
360 ;;; binding in ENV, generate an augmented environment with a binding
361 ;;; of the variable to the value in ENV, and then evaluate the next
362 ;;; binding form. Once all binding forms have been handled, END-ACTION
363 ;;; is funcalled with the final environment.
364 ;;;
365 ;;; SPECIALS is a list of variables that have a bound special declaration.
366 ;;; These variables (and those that have been declaimed as special) are
367 ;;; bound as special variables.
368 (defun eval-next-let*-binding (bindings specials env end-action)
369   (flet ((maybe-eval (exp)
370            ;; Pick off the easy (QUOTE x) case which is very common
371            ;; due to function calls.  (see PARSE-ARGUMENTS)
372            (if (and (consp exp) (eq (car exp) 'quote))
373                (second exp)
374                (%eval exp env))))
375     (if bindings
376         (let* ((binding-name (car (car bindings)))
377                (binding-value (cdr (car bindings)))
378                (new-env (make-env :parent env)))
379           (if (specialp binding-name specials)
380               (progv
381                   (list binding-name)
382                   (list (maybe-eval binding-value))
383                 ;; Mark the variable as special in this environment
384                 (push-var binding-name *special* new-env)
385                 (eval-next-let*-binding
386                  (cdr bindings) specials new-env end-action))
387               (progn
388                 (push-var binding-name (maybe-eval binding-value) new-env)
389                 (eval-next-let*-binding
390                  (cdr bindings) specials new-env end-action))))
391         (funcall end-action env))))
392
393 ;;; Create a new environment based on OLD-ENV by adding the variable
394 ;;; bindings in BINDINGS to it, and call FUNCTION with the new environment
395 ;;; as the only parameter. DECLARATIONS are the declarations that were
396 ;;; in a source position where bound declarations for the bindings could
397 ;;; be introduced.
398 ;;;
399 ;;; FREE-SPECIALS-P controls whether all special declarations should
400 ;;; end cause the variables to be marked as special in the environment
401 ;;; (when true), or only bound declarations (when false). Basically
402 ;;; it'll be T when handling a LET, and NIL when handling a call to an
403 ;;; interpreted function.
404 (defun call-with-new-env (old-env bindings declarations
405                           free-specials-p function)
406   (let* ((specials (declared-specials declarations))
407          (dynamic-vars nil)
408          (dynamic-values nil))
409     ;; To check for package-lock violations
410     (special-bindings specials old-env)
411     (flet ((generate-binding (binding)
412              (if (specialp (car binding) specials)
413                  ;; If the variable being bound is globally special or
414                  ;; there's a bound special declaration for it, record it
415                  ;; in DYNAMIC-VARS / -VALUES separately:
416                  ;;   * To handle the case of FREE-SPECIALS-P == T more
417                  ;;     cleanly.
418                  ;;   * The dynamic variables will be bound with PROGV just
419                  ;;     before funcalling
420                  (progn
421                    (push (car binding) dynamic-vars)
422                    (push (cdr binding) dynamic-values)
423                    nil)
424                  ;; Otherwise it's a lexical binding, and the value
425                  ;; will be recorded in the environment.
426                  (list binding))))
427       (let ((new-env (make-env
428                       :parent old-env
429                       :vars (mapcan #'generate-binding bindings)
430                       :declarations declarations)))
431         (dolist (special (if free-specials-p specials dynamic-vars))
432           (push-var special *special* new-env))
433         (if dynamic-vars
434             (progv dynamic-vars dynamic-values
435               (funcall function new-env))
436             ;; When there are no specials, the PROGV would be a no-op,
437             ;; but it's better to elide it completely, since the
438             ;; funcall is then in tail position.
439             (funcall function new-env))))))
440
441 ;;; Create a new environment based on OLD-ENV by binding the argument
442 ;;; list ARGUMENTS to LAMBDA-LIST, and call FUNCTION with the new
443 ;;; environment as argument. DECLARATIONS are the declarations that
444 ;;; were in a source position where bound declarations for the
445 ;;; bindings could be introduced.
446 (defun call-with-new-env-full-parsing
447     (old-env lambda-list arguments declarations function)
448   (multiple-value-bind (let-like-bindings let*-like-binding)
449       (parse-arguments arguments lambda-list)
450     (let ((specials (declared-specials declarations))
451           var-specials free-specials)
452       ;; Separate the bound and free special declarations
453       (dolist (special specials)
454         (if (or (member special let-like-bindings :key #'car)
455                 (member special let*-like-binding :key #'car))
456             (push special var-specials)
457             (push special free-specials)))
458       ;; First introduce the required parameters into the environment
459       ;; with CALL-WITH-NEW-ENV
460       (call-with-new-env
461        old-env let-like-bindings declarations nil
462        #'(lambda (env)
463            ;; Then deal with optionals / keywords / etc.
464            (eval-next-let*-binding
465             let*-like-binding var-specials env
466             #'(lambda (env)
467                 ;; And now that we have evaluated all the
468                 ;; initialization forms for the bindings, add the free
469                 ;; special declarations to the environment. To see why
470                 ;; this is the right thing to do (instead of passing
471                 ;; FREE-SPECIALS-P == T to CALL-WITH-NEW-ENV),
472                 ;; consider:
473                 ;;
474                 ;;   (eval '(let ((*a* 1))
475                 ;;     (declare (special *a*))
476                 ;;     (let ((*a* 2))
477                 ;;       (funcall (lambda (&optional (b *a*))
478                 ;;                  (declare (special *a*))
479                 ;;                  (values b *a*))))))
480                 ;;
481                 ;; *A* should be special in the body of the lambda, but
482                 ;; not when evaluating the default value of B.
483                 (dolist (special free-specials)
484                   (push-var special *special* env))
485                 (funcall function env))))))))
486
487 ;;; Set the VALUE of the binding (either lexical or special) of the
488 ;;; variable named by SYMBOL in the environment ENV.
489 (defun set-variable (symbol value env)
490   (let ((binding (get-binding symbol env)))
491     (if binding
492         (cond
493           ((eq (cdr binding) *special*)
494            (setf (symbol-value symbol) value))
495           ((eq (cdr binding) *symbol-macro*)
496            (error "Tried to set a symbol-macrolet!"))
497           (t (setf (cdr binding) value)))
498         (case (sb!int:info :variable :kind symbol)
499           (:macro (error "Tried to set a symbol-macrolet!"))
500           (:alien (let ((type (sb!int:info :variable :alien-info symbol)))
501                     (setf (sb!alien::%heap-alien type) value)))
502           (t
503            (let ((type (sb!c::info :variable :type symbol)))
504              (when type
505                (let ((type-specifier (sb!kernel:type-specifier type)))
506                  (unless (typep value type-specifier)
507                    (error 'type-error
508                           :datum value
509                           :expected-type type-specifier))))
510              (setf (symbol-value symbol) value)))))))
511
512 ;;; Retrieve the value of the binding (either lexical or special) of
513 ;;; the variable named by SYMBOL in the environment ENV. For symbol
514 ;;; macros the expansion is returned instead.
515 (defun get-variable (symbol env)
516   (let ((binding (get-binding symbol env)))
517     (if binding
518         (cond
519           ((eq (cdr binding) *special*)
520            (values (symbol-value symbol) :variable))
521           ((eq (cdr binding) *symbol-macro*)
522            (values (cdr (get-symbol-expansion-binding symbol env))
523                    :expansion))
524           (t (values (cdr binding) :variable)))
525         (case (sb!int:info :variable :kind symbol)
526           (:macro (values (macroexpand-1 symbol) :expansion))
527           (:alien (values (sb!alien-internals:alien-value symbol) :variable))
528           (t (values (symbol-value symbol) :variable))))))
529
530 ;;; Retrieve the function/macro binding of the symbol NAME in
531 ;;; environment ENV. The second return value will be :MACRO for macro
532 ;;; bindings, :FUNCTION for function bindings.
533 (defun get-function (name env)
534   (let ((binding (get-fbinding name env)))
535     (if binding
536         (cond
537           ((eq (cdr binding) *macro*)
538            (values (cdr (get-expander-binding name env)) :macro))
539           (t (values (cdr binding) :function)))
540         (cond
541           ((and (symbolp name) (macro-function name))
542            (values (macro-function name) :macro))
543           (t (values (%coerce-name-to-fun name) :function))))))
544
545 ;;; Return true if EXP is a lambda form.
546 (defun lambdap (exp)
547   (case (car exp)
548     ((lambda sb!int:named-lambda) t)))
549
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.
554 ;;;
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
561         for form on body do
562         (cond
563           ((and doc-string-allowed (stringp (car form)))
564            (if (cdr form)               ; CLHS 3.4.11
565                (if documentation
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))))
573
574 ;;; Create an interpreted function from the lambda-form EXP evaluated
575 ;;; in the environment ENV.
576 (defun eval-lambda (exp env)
577   (case (car exp)
578     ((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)
582                                   :env env :body body
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)
591                                   :env env :body body
592                                   :documentation documentation
593                                   :source-location (sb!c::make-definition-source-location)
594                                   :declarations declarations)))))
595
596 (defun eval-progn (body env)
597   (let ((previous-exp nil))
598     (dolist (exp body)
599       (if previous-exp
600           (%eval previous-exp env))
601       (setf previous-exp exp))
602     ;; Preserve tail call
603     (%eval previous-exp env)))
604
605 (defun eval-if (body env)
606   (program-destructuring-bind (test if-true &optional if-false) body
607     (if (%eval test env)
608         (%eval if-true env)
609         (%eval if-false env))))
610
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
615                      #'(lambda (binding)
616                          (cons (binding-name binding)
617                                (%eval (binding-value binding) env)))
618                      bindings)))
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
623         ;; body.
624         (call-with-new-env env bindings declarations t
625                            #'(lambda (env)
626                                (eval-progn body env)))))))
627
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
644           ;; sequentially.
645           (eval-next-let*-binding
646            (mapcar #'(lambda (binding)
647                        (cons (binding-name binding)
648                              (binding-value binding)))
649                    bindings)
650            var-specials env
651            #'(lambda (env)
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))))))))
658
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
666                   ,@(if documentation
667                         (list documentation)
668                         nil)
669                   (declare ,@declarations)
670                   (block ,(cond ((consp name) (second name))
671                                 (t name))
672                     ,@local-body))
673              env))))
674
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.
689                     env
690                     ;; But add the bindings to the child environment.
691                     new-env))
692         (eval-progn body new-env)))))
693
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)
706                     old-env
707                     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))))))
717
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)))
732         (setf lambda-list
733               (loop with skip = 0
734                     for element in lambda-list
735                     if (cond
736                          ((/= skip 0)
737                           (decf skip)
738                           (setf has-environment element)
739                           nil)
740                          ((eq element '&environment)
741                           (if has-environment
742                               (ip-error "Repeated &ENVIRONMENT.")
743                               (setf skip 1))
744                           nil)
745                          ((eq element '&whole)
746                           (ip-error "&WHOLE may only appear first ~
747                                      in MACROLET lambda-list."))
748                          (t t))
749                     collect element))
750         (let ((outer-whole (gensym "WHOLE"))
751               (environment (or has-environment (gensym "ENVIRONMENT")))
752               (macro-name (gensym "NAME")))
753           (%eval `#'(lambda (,outer-whole ,environment)
754                       ,@(if documentation
755                             (list documentation)
756                             nil)
757                       (declare ,@(unless has-environment
758                                          `((ignore ,environment))))
759                       (program-destructuring-bind
760                           (,@(if has-whole
761                                  (list '&whole has-whole)
762                                  nil)
763                              ,macro-name ,@lambda-list)
764                           ,outer-whole
765                         (declare (ignore ,macro-name)
766                                  ,@declarations)
767                         (block ,name ,@local-body)))
768                  env))))))
769
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)))
777                (when (fboundp name)
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
788                                                 local-functions)
789                                   :expanders (mapcar #'generate-mbinding
790                                                      local-functions)
791                                   :declarations declarations)))
792           (eval-progn body new-env))))))
793
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 ~
813                          variable ~S.~:@>"
814                         (binding-name binding)))))
815         (let* ((specials (declared-specials declarations))
816                (new-env (make-env :parent env
817                                   :vars (nconc-2 (mapcar #'generate-binding
818                                                          bindings)
819                                                  (special-bindings specials env))
820                                   :symbol-expansions (mapcar
821                                                       #'generate-sm-binding
822                                                       bindings)
823                                   :declarations declarations)))
824           (eval-progn body new-env))))))
825
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))))
830
831 (defun eval-function (body env)
832   (program-destructuring-bind (name) body
833     (cond
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))))))
840
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))))
847
848 (defun eval-quote (body env)
849   (declare (ignore env))
850   (program-destructuring-bind (object) body
851     object))
852
853 (defun eval-setq (pairs env)
854   (when (oddp (length pairs))
855     (ip-error "~@<Odd number of args to SETQ: ~S~:@>" (cons 'setq pairs)))
856   (let ((last nil))
857     (loop for (var new-val) on pairs by #'cddr do
858           (handler-case
859               (multiple-value-bind (expansion type) (get-variable var env)
860                 (ecase type
861                   (:expansion
862                    (setf last
863                          (%eval (list 'setf expansion new-val) env)))
864                   (:variable
865                    (setf last (set-variable var (%eval new-val env)
866                                             env)))))
867             (unbound-variable (c)
868               (declare (ignore c))
869               (setf last (setf (symbol-value var)
870                                (%eval new-val env))))))
871     last))
872
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))))))
878
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))))
883
884 (defun eval-catch (body env)
885   (program-destructuring-bind (tag &body forms) body
886     (catch (%eval tag env)
887       (eval-progn forms env))))
888
889 (defun eval-tagbody (body old-env)
890   (let ((env (make-env :parent old-env))
891         (tags nil)
892         (start body)
893         (target-tag nil))
894     (tagbody
895        (flet ((go-to-tag (tag)
896                 (setf target-tag tag)
897                 (go go-to-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)))
901              ((null form) nil)
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
908        ;; first one.
909        (go execute)
910      go-to-tag
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)))
914      execute
915        (dolist (form start)
916          (when (not (atom form))
917            (%eval form env))))))
918
919 (defun eval-go (body env)
920   (program-destructuring-bind (tag) body
921     (let ((target (get-tag-binding tag env)))
922       (if target
923           ;; Call the GO-TO-TAG trampoline
924           (funcall (cdr target) tag)
925           (ip-error "~@<Attempt to GO to nonexistent tag: ~S~:@>" tag)))))
926
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))
933       (let ((env (make-env
934                   :blocks (list (cons name #'return-from-eval-block))
935                   :parent old-env)))
936         (eval-progn body env)))))
937
938 (defun eval-return-from (body env)
939   (program-destructuring-bind (name &optional result) body
940     (let ((target (get-block-binding name env)))
941       (if target
942           (multiple-value-call (cdr target) (%eval result env))
943           (ip-error "~@<Return for unknown block: ~S~:@>" name)))))
944
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.
951     (%eval form env)))
952
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))))
957
958 (defun eval-throw (body env)
959   (program-destructuring-bind (tag result-form) body
960     (throw (%eval tag env)
961       (%eval result-form env))))
962
963 (defun eval-load-time-value (body env)
964   (program-destructuring-bind (form &optional read-only-p) body
965     (declare (ignore read-only-p))
966     (%eval form env)))
967
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)
977                         env)))
978       (eval-progn body new-env))))
979
980 (defun eval-args (args env)
981   (mapcar #'(lambda (arg) (%eval arg env)) args))
982
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
988     (if (null values)
989         (eval-progn body env)
990         (sb!sys:with-pinned-objects ((car values))
991           (eval-with-pinned-objects (cons (cdr values) body) env)))))
992
993 (define-condition macroexpand-hook-type-error (type-error)
994   ()
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)))))
998
999 (defvar *eval-dispatch-functions* nil)
1000
1001 ;;; Dispatch to the appropriate EVAL-FOO function based on the contents of EXP.
1002 (declaim (inline %%eval))
1003 (defun %%eval (exp env)
1004   (cond
1005     ((symbolp exp)
1006      ;; CLHS 3.1.2.1.1 Symbols as Forms
1007      (multiple-value-bind (value kind) (get-variable exp env)
1008        (ecase kind
1009          (:variable value)
1010          (:expansion (%eval value env)))))
1011     ;; CLHS 3.1.2.1.3 Self-Evaluating Objects
1012     ((atom exp) exp)
1013     ;; CLHS 3.1.2.1.2 Conses as Forms
1014     ((consp exp)
1015      (case (car exp)
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))
1042        ;; SBCL-specific:
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))
1047        (t
1048         (let ((dispatcher (getf *eval-dispatch-functions* (car exp))))
1049           (cond
1050             (dispatcher
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)))
1056             (t
1057              (multiple-value-bind (function kind) (get-function (car exp) env)
1058                (ecase kind
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
1062                  (:macro
1063                   (let ((hook *macroexpand-hook*))
1064                     ;; Having an interpreted function as the
1065                     ;; macroexpander hook could cause an infinite
1066                     ;; loop.
1067                     (unless (compiled-function-p
1068                              (etypecase hook
1069                                (function hook)
1070                                (symbol (symbol-function hook))))
1071                       (error 'macroexpand-hook-type-error
1072                              :datum hook
1073                              :expected-type 'compiled-function))
1074                     (%eval (funcall hook
1075                                     function
1076                                     exp
1077                                     (env-native-lexenv env))
1078                            env)))))))))))))
1079
1080 (defun %eval (exp env)
1081   (incf *eval-calls*)
1082   (if *eval-verbose*
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)))
1089         (%%eval exp env))
1090       (%%eval exp env)))
1091
1092 (defun %apply (fun args)
1093   (etypecase fun
1094     (interpreted-function (interpreted-apply fun args))
1095     (function (apply fun args))
1096     (symbol (apply fun args))))
1097
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
1105      #'(lambda (env)
1106          (eval-progn body env)))))
1107
1108 ;;; We need separate conditions for the different *-TOO-COMPLEX-ERRORs to
1109 ;;; avoid spuriously triggering the handler in EVAL-IN-NATIVE-ENVIRONMENT
1110 ;;; on code like:
1111 ;;;
1112 ;;;   (let ((sb-ext:*evaluator-mode* :interpret))
1113 ;;;     (let ((fun (eval '(let ((a 1)) (lambda () a)))))
1114 ;;;         (eval `(compile nil ,fun))))
1115 ;;;
1116 ;;; FIXME: should these be exported?
1117 (define-condition interpreter-environment-too-complex-error (simple-error)
1118   ())
1119 (define-condition compiler-environment-too-complex-error (simple-error)
1120   ())
1121
1122 ;;; Try to compile an interpreted function. If the environment
1123 ;;; contains local functions or lexical variables we'll punt on
1124 ;;; compiling it.
1125 (defun prepare-for-compile (function)
1126   (let ((env (interpreted-function-env function)))
1127     (when (or (env-tags env)
1128               (env-blocks 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*))
1132                            (env-vars env)
1133                            :key #'cdr))
1134       (error 'interpreter-environment-too-complex-error
1135              :format-control
1136              "~@<Lexical environment of ~S is too complex to compile.~:@>"
1137              :format-arguments
1138              (list function)))
1139     (values
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))))
1145
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
1161                :format-control
1162                "~@<Lexical environment is too complex to evaluate in: ~S~:@>"
1163                :format-arguments
1164                (list lexenv))))
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))))
1173       (%make-env nil
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)
1178                  nil
1179                  nil
1180                  nil
1181                  lexenv))))
1182
1183 (defun eval-in-environment (form env)
1184   (%eval form env))
1185
1186 (defun eval-in-native-environment (form lexenv)
1187   (handler-bind
1188       ((sb!impl::eval-error
1189          (lambda (condition)
1190            (error 'interpreted-program-error
1191                   :condition (sb!int:encapsulated-condition condition)
1192                   :form form))))
1193     (sb!c:with-compiler-error-resignalling
1194       (handler-case
1195           (let ((env (make-env-from-native-environment lexenv)))
1196             (%eval form env))
1197         (compiler-environment-too-complex-error (condition)
1198           (declare (ignore condition))
1199           (sb!int:style-warn 'sb!kernel:lexical-environment-too-complex
1200                              :form form :lexenv lexenv)
1201           (sb!int:simple-eval-in-lexenv form lexenv))))))