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 (defvar *eval-source-context* nil)
25 ;;; general case of EVAL (except in that it can't handle toplevel
26 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
27 (defun %simple-eval (expr lexenv)
28 ;; FIXME: It might be nice to quieten the toplevel by muffling
29 ;; warnings generated by this compilation (since we're about to
30 ;; execute the results irrespective of the warnings). We might want
31 ;; to be careful about not muffling warnings arising from inner
32 ;; evaluations/compilations, though [e.g. the ignored variable in
33 ;; (DEFUN FOO (X) 1)]. -- CSR, 2003-05-13
35 ;; As of 1.0.21.6 we muffle compiler notes lexically here, which seems
37 (let* (;; why PROGN? So that attempts to eval free declarations
38 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
39 (lambda `(named-lambda (eval ,(sb!c::source-form-context *eval-source-context*)) ()
40 (declare (muffle-conditions compiler-note))
42 (fun (sb!c:compile-in-lexenv nil lambda lexenv)))
45 ;;; Handle PROGN and implicit PROGN.
46 (defun simple-eval-progn-body (progn-body lexenv)
47 (unless (list-with-length-p progn-body)
48 (let ((*print-circle* t))
49 (error 'simple-program-error
51 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
52 :format-arguments (list progn-body))))
54 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
55 ;; need to take care to return all the values of the final EVAL.
56 ;; * It's left as an exercise to the reader to verify that this
57 ;; gives the right result when PROGN-BODY is NIL, because
58 ;; (FIRST NIL) = (REST NIL) = NIL.
59 (do* ((i progn-body rest-i)
60 (rest-i (rest i) (rest i)))
62 (if rest-i ; if not last element of list
63 (simple-eval-in-lexenv (first i) lexenv)
64 (return (simple-eval-in-lexenv (first i) lexenv)))))
66 (defun simple-eval-locally (exp lexenv &key vars)
67 (multiple-value-bind (body decls)
68 (parse-body (rest exp) :doc-string-allowed nil)
70 ;; KLUDGE: Uh, yeah. I'm not anticipating
71 ;; winning any prizes for this code, which was
72 ;; written on a "let's get it to work" basis.
73 ;; These seem to be the variables that need
74 ;; bindings for PROCESS-DECLS to work
75 ;; (*FREE-FUNS* and *FREE-VARS* so that
76 ;; references to free functions and variables
77 ;; in the declarations can be noted;
78 ;; *UNDEFINED-WARNINGS* so that warnings about
79 ;; undefined things can be accumulated [and
80 ;; then thrown away, as it happens]). -- CSR,
82 (let* ((sb!c:*lexenv* lexenv)
83 (sb!c::*free-funs* (make-hash-table :test 'equal))
84 (sb!c::*free-vars* (make-hash-table :test 'eq))
85 (sb!c::*undefined-warnings* nil))
86 ;; FIXME: VALUES declaration
87 (sb!c::process-decls decls
92 (simple-eval-progn-body body lexenv))))
96 ;;;; Analogous to COMPILER-ERROR, but simpler.
98 (define-condition eval-error (encapsulated-condition)
100 (:report (lambda (condition stream)
101 (print-object (encapsulated-condition condition) stream))))
103 (defun eval-error (condition)
104 (signal 'eval-error :condition condition)
105 (bug "Unhandled EVAL-ERROR"))
107 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
108 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
109 (defun simple-eval-in-lexenv (original-exp lexenv)
110 (declare (optimize (safety 1)))
111 ;; (aver (lexenv-simple-p lexenv))
114 ((sb!c:compiler-error
116 (if (boundp 'sb!c::*compiler-error-bailout*)
117 ;; if we're in the compiler, delegate either to a higher
118 ;; authority or, if that's us, back down to the
119 ;; outermost compiler handler...
123 ;; ... if we're not in the compiler, better signal the
124 ;; error straight away.
125 (invoke-restart 'sb!c::signal-error)))))
126 (let ((exp (macroexpand original-exp lexenv)))
127 (handler-bind ((eval-error
129 (error 'interpreted-program-error
130 :condition (encapsulated-condition condition)
134 (ecase (info :variable :kind exp)
135 ((:special :global :constant :unknown)
137 ;; FIXME: This special case here is a symptom of non-ANSI
138 ;; weirdness in SBCL's ALIEN implementation, which could
139 ;; cause problems for e.g. code walkers. It'd probably be
140 ;; good to ANSIfy it by making alien variable accessors
141 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
142 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
143 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
144 ;; be retained for compatibility, it can be implemented
145 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
148 (%simple-eval original-exp lexenv))))
150 (let ((name (first exp))
151 (n-args (1- (length exp))))
155 (error "wrong number of args to FUNCTION:~% ~S" exp))
156 (let ((name (second exp)))
157 (if (and (legal-fun-name-p name)
158 (not (consp (let ((sb!c:*lexenv* lexenv))
159 (sb!c:lexenv-find name funs)))))
160 (%coerce-name-to-fun name)
161 ;; FIXME: This is a bit wasteful: it would be nice to call
162 ;; COMPILE-IN-LEXENV with the lambda-form directly, but
163 ;; getting consistent source context and muffling compiler notes
164 ;; is easier this way.
165 (%simple-eval original-exp lexenv))))
168 (error "wrong number of args to QUOTE:~% ~S" exp))
171 (unless (evenp n-args)
172 (error "odd number of args to SETQ:~% ~S" exp))
173 (unless (zerop n-args)
174 (do ((name (cdr exp) (cddr name)))
176 (do ((args (cdr exp) (cddr args)))
178 ;; We duplicate the call to SET so that the
179 ;; correct value gets returned.
181 (simple-eval-in-lexenv (second args) lexenv)))
183 (simple-eval-in-lexenv (second args) lexenv))))
184 (let ((symbol (first name)))
185 (case (info :variable :kind symbol)
187 (t (return (%simple-eval original-exp lexenv))))
188 (unless (type= (info :variable :type symbol)
190 ;; let the compiler deal with type checking
191 (return (%simple-eval original-exp lexenv)))))))
193 (simple-eval-progn-body (rest exp) lexenv))
195 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
196 ;; instead of PROGRAM-ERROR when there's something wrong
197 ;; with the syntax here (e.g. missing SITUATIONS). This
198 ;; could be fixed by hand-crafting clauses to catch and
199 ;; report each possibility, but it would probably be
200 ;; cleaner to write a new macro
201 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
202 ;; DESTRUCTURING-BIND and promotes any mismatch to
203 ;; PROGRAM-ERROR, then to use it here and in (probably
204 ;; dozens of) other places where the same problem
206 (destructuring-bind (eval-when situations &rest body) exp
207 (declare (ignore eval-when))
208 (multiple-value-bind (ct lt e)
209 (sb!c:parse-eval-when-situations situations)
210 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
211 ;; the situation :EXECUTE (or EVAL) controls whether
212 ;; evaluation occurs for other EVAL-WHEN forms; that
213 ;; is, those that are not top level forms, or those
214 ;; in code processed by EVAL or COMPILE. If the
215 ;; :EXECUTE situation is specified in such a form,
216 ;; then the body forms are processed as an implicit
217 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
218 (declare (ignore ct lt))
220 (simple-eval-progn-body body lexenv)))))
222 (simple-eval-locally exp lexenv))
224 (destructuring-bind (definitions &rest body)
227 (let ((sb!c:*lexenv* lexenv))
228 (sb!c::funcall-in-macrolet-lexenv
231 (declare (ignore funs))
234 (simple-eval-locally `(locally ,@body) lexenv))))
236 (destructuring-bind (definitions &rest body) (rest exp)
237 (multiple-value-bind (lexenv vars)
238 (let ((sb!c:*lexenv* lexenv))
239 (sb!c::funcall-in-symbol-macrolet-lexenv
242 (values sb!c:*lexenv* vars))
244 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
246 (destructuring-bind (test then &optional else) (rest exp)
247 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
252 (destructuring-bind (definitions &rest body) (rest exp)
253 (if (null definitions)
254 (simple-eval-locally `(locally ,@body) lexenv)
255 (%simple-eval exp lexenv))))
257 (if (and (symbolp name)
258 (eq (info :function :kind name) :function))
260 (dolist (arg (rest exp))
261 (args (eval-in-lexenv arg lexenv)))
262 (apply (symbol-function name) (args)))
263 (%simple-eval exp lexenv))))))
267 (defun eval-in-lexenv (exp lexenv)
269 (if (eq *evaluator-mode* :compile)
270 (simple-eval-in-lexenv exp lexenv)
271 (sb!eval:eval-in-native-environment exp lexenv))
273 (simple-eval-in-lexenv exp lexenv))
275 (defun eval (original-exp)
277 "Evaluate the argument in a null lexical environment, returning the
279 (let ((*eval-source-context* original-exp))
280 (eval-in-lexenv original-exp (make-null-lexenv))))
283 ;;; miscellaneous full function definitions of things which are
284 ;;; ordinarily handled magically by the compiler
286 (defun apply (function arg &rest arguments)
288 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
289 the manner of LIST*. That is, a list is made of the values of all but the
290 last argument, appended to the value of the last argument, which must be a
292 (cond ((atom arguments)
293 (apply function arg))
294 ((atom (cdr arguments))
295 (apply function (cons arg (car arguments))))
296 (t (do* ((a1 arguments a2)
297 (a2 (cdr arguments) (cdr a2)))
300 (apply function (cons arg arguments)))))))
302 (defun funcall (function &rest arguments)
304 "Call FUNCTION with the given ARGUMENTS."
305 (apply function arguments))
307 (defun values (&rest values)
309 "Return all arguments, in order, as values."
310 (declare (truly-dynamic-extent values))
311 (values-list values))
313 (defun values-list (list)
315 "Return all of the elements of LIST, in order, as values."