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!IMPL")
14 (defparameter *eval-calls* 0)
16 (defun !eval-cold-init ()
18 *evaluator-mode* :compile)
20 (setf sb!eval::*eval-level* -1
21 sb!eval::*eval-verbose* nil))
23 ;;; general case of EVAL (except in that it can't handle toplevel
24 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
25 (defun %simple-eval (expr lexenv)
26 ;; FIXME: It might be nice to quieten the toplevel by muffling
27 ;; warnings generated by this compilation (since we're about to
28 ;; execute the results irrespective of the warnings). We might want
29 ;; to be careful about not muffling warnings arising from inner
30 ;; evaluations/compilations, though [e.g. the ignored variable in
31 ;; (DEFUN FOO (X) 1)]. -- CSR, 2003-05-13
33 ;; As of 1.0.21.6 we muffle compiler notes lexically here, which seems
35 (let* (;; why PROGN? So that attempts to eval free declarations
36 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
38 (declare (muffle-conditions compiler-note))
40 (fun (sb!c:compile-in-lexenv nil lambda lexenv)))
43 ;;; Handle PROGN and implicit PROGN.
44 (defun simple-eval-progn-body (progn-body lexenv)
45 (unless (list-with-length-p progn-body)
46 (let ((*print-circle* t))
47 (error 'simple-program-error
49 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
50 :format-arguments (list progn-body))))
52 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
53 ;; need to take care to return all the values of the final EVAL.
54 ;; * It's left as an exercise to the reader to verify that this
55 ;; gives the right result when PROGN-BODY is NIL, because
56 ;; (FIRST NIL) = (REST NIL) = NIL.
57 (do* ((i progn-body rest-i)
58 (rest-i (rest i) (rest i)))
60 (if rest-i ; if not last element of list
61 (simple-eval-in-lexenv (first i) lexenv)
62 (return (simple-eval-in-lexenv (first i) lexenv)))))
64 (defun simple-eval-locally (exp lexenv &key vars)
65 (multiple-value-bind (body decls)
66 (parse-body (rest exp) :doc-string-allowed nil)
68 ;; KLUDGE: Uh, yeah. I'm not anticipating
69 ;; winning any prizes for this code, which was
70 ;; written on a "let's get it to work" basis.
71 ;; These seem to be the variables that need
72 ;; bindings for PROCESS-DECLS to work
73 ;; (*FREE-FUNS* and *FREE-VARS* so that
74 ;; references to free functions and variables
75 ;; in the declarations can be noted;
76 ;; *UNDEFINED-WARNINGS* so that warnings about
77 ;; undefined things can be accumulated [and
78 ;; then thrown away, as it happens]). -- CSR,
80 (let* ((sb!c:*lexenv* lexenv)
81 (sb!c::*free-funs* (make-hash-table :test 'equal))
82 (sb!c::*free-vars* (make-hash-table :test 'eq))
83 (sb!c::*undefined-warnings* nil))
84 ;; FIXME: VALUES declaration
85 (sb!c::process-decls decls
90 (simple-eval-progn-body body lexenv))))
94 ;;;; Analogous to COMPILER-ERROR, but simpler.
96 (define-condition eval-error (encapsulated-condition)
98 (:report (lambda (condition stream)
99 (print-object (encapsulated-condition condition) stream))))
101 (defun eval-error (condition)
102 (signal 'eval-error :condition condition)
103 (bug "Unhandled EVAL-ERROR"))
105 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
106 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
107 (defun simple-eval-in-lexenv (original-exp lexenv)
108 (declare (optimize (safety 1)))
109 ;; (aver (lexenv-simple-p lexenv))
112 ((sb!c:compiler-error
114 (if (boundp 'sb!c::*compiler-error-bailout*)
115 ;; if we're in the compiler, delegate either to a higher
116 ;; authority or, if that's us, back down to the
117 ;; outermost compiler handler...
121 ;; ... if we're not in the compiler, better signal the
122 ;; error straight away.
123 (invoke-restart 'sb!c::signal-error)))))
124 (let ((exp (macroexpand original-exp lexenv)))
125 (handler-bind ((eval-error
127 (error 'interpreted-program-error
128 :condition (encapsulated-condition condition)
132 (ecase (info :variable :kind exp)
133 ((:special :global :constant)
135 ;; FIXME: This special case here is a symptom of non-ANSI
136 ;; weirdness in SBCL's ALIEN implementation, which could
137 ;; cause problems for e.g. code walkers. It'd probably be
138 ;; good to ANSIfy it by making alien variable accessors
139 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
140 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
141 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
142 ;; be retained for compatibility, it can be implemented
143 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
146 (%simple-eval original-exp lexenv))))
148 (let ((name (first exp))
149 (n-args (1- (length exp))))
153 (error "wrong number of args to FUNCTION:~% ~S" exp))
154 (let ((name (second exp)))
155 (if (and (legal-fun-name-p name)
156 (not (consp (let ((sb!c:*lexenv* lexenv))
157 (sb!c:lexenv-find name funs)))))
158 (%coerce-name-to-fun name)
159 (%simple-eval original-exp lexenv))))
162 (error "wrong number of args to QUOTE:~% ~S" exp))
165 (unless (evenp n-args)
166 (error "odd number of args to SETQ:~% ~S" exp))
167 (unless (zerop n-args)
168 (do ((name (cdr exp) (cddr name)))
170 (do ((args (cdr exp) (cddr args)))
172 ;; We duplicate the call to SET so that the
173 ;; correct value gets returned.
175 (simple-eval-in-lexenv (second args) lexenv)))
177 (simple-eval-in-lexenv (second args) lexenv))))
178 (let ((symbol (first name)))
179 (case (info :variable :kind symbol)
181 (t (return (%simple-eval original-exp lexenv))))
182 (unless (type= (info :variable :type symbol)
184 ;; let the compiler deal with type checking
185 (return (%simple-eval original-exp lexenv)))))))
187 (simple-eval-progn-body (rest exp) lexenv))
189 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
190 ;; instead of PROGRAM-ERROR when there's something wrong
191 ;; with the syntax here (e.g. missing SITUATIONS). This
192 ;; could be fixed by hand-crafting clauses to catch and
193 ;; report each possibility, but it would probably be
194 ;; cleaner to write a new macro
195 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
196 ;; DESTRUCTURING-BIND and promotes any mismatch to
197 ;; PROGRAM-ERROR, then to use it here and in (probably
198 ;; dozens of) other places where the same problem
200 (destructuring-bind (eval-when situations &rest body) exp
201 (declare (ignore eval-when))
202 (multiple-value-bind (ct lt e)
203 (sb!c:parse-eval-when-situations situations)
204 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
205 ;; the situation :EXECUTE (or EVAL) controls whether
206 ;; evaluation occurs for other EVAL-WHEN forms; that
207 ;; is, those that are not top level forms, or those
208 ;; in code processed by EVAL or COMPILE. If the
209 ;; :EXECUTE situation is specified in such a form,
210 ;; then the body forms are processed as an implicit
211 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
212 (declare (ignore ct lt))
214 (simple-eval-progn-body body lexenv)))))
216 (simple-eval-locally exp lexenv))
218 (destructuring-bind (definitions &rest body)
221 (let ((sb!c:*lexenv* lexenv))
222 (sb!c::funcall-in-macrolet-lexenv
225 (declare (ignore funs))
228 (simple-eval-locally `(locally ,@body) lexenv))))
230 (destructuring-bind (definitions &rest body) (rest exp)
231 (multiple-value-bind (lexenv vars)
232 (let ((sb!c:*lexenv* lexenv))
233 (sb!c::funcall-in-symbol-macrolet-lexenv
236 (values sb!c:*lexenv* vars))
238 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
240 (destructuring-bind (test then &optional else) (rest exp)
241 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
246 (destructuring-bind (definitions &rest body) (rest exp)
247 (if (null definitions)
248 (simple-eval-locally `(locally ,@body) lexenv)
249 (%simple-eval exp lexenv))))
251 (if (and (symbolp name)
252 (eq (info :function :kind name) :function))
254 (dolist (arg (rest exp))
255 (args (eval-in-lexenv arg lexenv)))
256 (apply (symbol-function name) (args)))
257 (%simple-eval exp lexenv))))))
261 (defun eval-in-lexenv (exp lexenv)
263 (if (eq *evaluator-mode* :compile)
264 (simple-eval-in-lexenv exp lexenv)
265 (sb!eval:eval-in-native-environment exp lexenv))
267 (simple-eval-in-lexenv exp lexenv))
269 (defun eval (original-exp)
271 "Evaluate the argument in a null lexical environment, returning the
273 (eval-in-lexenv original-exp (make-null-lexenv)))
276 ;;; miscellaneous full function definitions of things which are
277 ;;; ordinarily handled magically by the compiler
279 (defun apply (function arg &rest arguments)
281 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
282 the manner of LIST*. That is, a list is made of the values of all but the
283 last argument, appended to the value of the last argument, which must be a
285 (cond ((atom arguments)
286 (apply function arg))
287 ((atom (cdr arguments))
288 (apply function (cons arg (car arguments))))
289 (t (do* ((a1 arguments a2)
290 (a2 (cdr arguments) (cdr a2)))
293 (apply function (cons arg arguments)))))))
295 (defun funcall (function &rest arguments)
297 "Call FUNCTION with the given ARGUMENTS."
298 (apply function arguments))
300 (defun values (&rest values)
302 "Return all arguments, in order, as values."
303 (declare (truly-dynamic-extent values))
304 (values-list values))
306 (defun values-list (list)
308 "Return all of the elements of LIST, in order, as values."