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
32 (let* (;; why PROGN? So that attempts to eval free declarations
33 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
34 (lambda `(lambda () (progn ,expr)))
35 (fun (sb!c:compile-in-lexenv nil lambda lexenv)))
38 ;;; Handle PROGN and implicit PROGN.
39 (defun simple-eval-progn-body (progn-body lexenv)
40 (unless (list-with-length-p progn-body)
41 (let ((*print-circle* t))
42 (error 'simple-program-error
44 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
45 :format-arguments (list progn-body))))
47 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
48 ;; need to take care to return all the values of the final EVAL.
49 ;; * It's left as an exercise to the reader to verify that this
50 ;; gives the right result when PROGN-BODY is NIL, because
51 ;; (FIRST NIL) = (REST NIL) = NIL.
52 (do* ((i progn-body rest-i)
53 (rest-i (rest i) (rest i)))
55 (if rest-i ; if not last element of list
56 (simple-eval-in-lexenv (first i) lexenv)
57 (return (simple-eval-in-lexenv (first i) lexenv)))))
59 (defun simple-eval-locally (exp lexenv &key vars)
60 (multiple-value-bind (body decls)
61 (parse-body (rest exp) :doc-string-allowed nil)
63 ;; KLUDGE: Uh, yeah. I'm not anticipating
64 ;; winning any prizes for this code, which was
65 ;; written on a "let's get it to work" basis.
66 ;; These seem to be the variables that need
67 ;; bindings for PROCESS-DECLS to work
68 ;; (*FREE-FUNS* and *FREE-VARS* so that
69 ;; references to free functions and variables
70 ;; in the declarations can be noted;
71 ;; *UNDEFINED-WARNINGS* so that warnings about
72 ;; undefined things can be accumulated [and
73 ;; then thrown away, as it happens]). -- CSR,
75 (let* ((sb!c:*lexenv* lexenv)
76 (sb!c::*free-funs* (make-hash-table :test 'equal))
77 (sb!c::*free-vars* (make-hash-table :test 'eq))
78 (sb!c::*undefined-warnings* nil))
79 ;; FIXME: VALUES declaration
80 (sb!c::process-decls decls
85 (simple-eval-progn-body body lexenv))))
89 ;;;; Analogous to COMPILER-ERROR, but simpler.
91 (define-condition eval-error (encapsulated-condition)
93 (:report (lambda (condition stream)
94 (print-object (encapsulated-condition condition) stream))))
96 (defun eval-error (condition)
97 (signal 'eval-error :condition condition)
98 (bug "Unhandled EVAL-ERROR"))
100 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
101 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
102 (defun simple-eval-in-lexenv (original-exp lexenv)
103 (declare (optimize (safety 1)))
104 ;; (aver (lexenv-simple-p lexenv))
107 ((sb!c:compiler-error
109 (if (boundp 'sb!c::*compiler-error-bailout*)
110 ;; if we're in the compiler, delegate either to a higher
111 ;; authority or, if that's us, back down to the
112 ;; outermost compiler handler...
116 ;; ... if we're not in the compiler, better signal the
117 ;; error straight away.
118 (invoke-restart 'sb!c::signal-error)))))
119 (let ((exp (macroexpand original-exp lexenv)))
120 (handler-bind ((eval-error
122 (error 'interpreted-program-error
123 :condition (encapsulated-condition condition)
127 (ecase (info :variable :kind exp)
128 ((:special :global :constant)
130 ;; FIXME: This special case here is a symptom of non-ANSI
131 ;; weirdness in SBCL's ALIEN implementation, which could
132 ;; cause problems for e.g. code walkers. It'd probably be
133 ;; good to ANSIfy it by making alien variable accessors
134 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
135 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
136 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
137 ;; be retained for compatibility, it can be implemented
138 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
141 (%simple-eval original-exp lexenv))))
143 (let ((name (first exp))
144 (n-args (1- (length exp))))
148 (error "wrong number of args to FUNCTION:~% ~S" exp))
149 (let ((name (second exp)))
150 (if (and (legal-fun-name-p name)
151 (not (consp (let ((sb!c:*lexenv* lexenv))
152 (sb!c:lexenv-find name funs)))))
153 (%coerce-name-to-fun name)
154 (%simple-eval original-exp lexenv))))
157 (error "wrong number of args to QUOTE:~% ~S" exp))
160 (unless (evenp n-args)
161 (error "odd number of args to SETQ:~% ~S" exp))
162 (unless (zerop n-args)
163 (do ((name (cdr exp) (cddr name)))
165 (do ((args (cdr exp) (cddr args)))
167 ;; We duplicate the call to SET so that the
168 ;; correct value gets returned.
170 (simple-eval-in-lexenv (second args) lexenv)))
172 (simple-eval-in-lexenv (second args) lexenv))))
173 (let ((symbol (first name)))
174 (case (info :variable :kind symbol)
176 (t (return (%simple-eval original-exp lexenv))))
177 (unless (type= (info :variable :type symbol)
179 ;; let the compiler deal with type checking
180 (return (%simple-eval original-exp lexenv)))))))
182 (simple-eval-progn-body (rest exp) lexenv))
184 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
185 ;; instead of PROGRAM-ERROR when there's something wrong
186 ;; with the syntax here (e.g. missing SITUATIONS). This
187 ;; could be fixed by hand-crafting clauses to catch and
188 ;; report each possibility, but it would probably be
189 ;; cleaner to write a new macro
190 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
191 ;; DESTRUCTURING-BIND and promotes any mismatch to
192 ;; PROGRAM-ERROR, then to use it here and in (probably
193 ;; dozens of) other places where the same problem
195 (destructuring-bind (eval-when situations &rest body) exp
196 (declare (ignore eval-when))
197 (multiple-value-bind (ct lt e)
198 (sb!c:parse-eval-when-situations situations)
199 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
200 ;; the situation :EXECUTE (or EVAL) controls whether
201 ;; evaluation occurs for other EVAL-WHEN forms; that
202 ;; is, those that are not top level forms, or those
203 ;; in code processed by EVAL or COMPILE. If the
204 ;; :EXECUTE situation is specified in such a form,
205 ;; then the body forms are processed as an implicit
206 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
207 (declare (ignore ct lt))
209 (simple-eval-progn-body body lexenv)))))
211 (simple-eval-locally exp lexenv))
213 (destructuring-bind (definitions &rest body)
216 (let ((sb!c:*lexenv* lexenv))
217 (sb!c::funcall-in-macrolet-lexenv
220 (declare (ignore funs))
223 (simple-eval-locally `(locally ,@body) lexenv))))
225 (destructuring-bind (definitions &rest body) (rest exp)
226 (multiple-value-bind (lexenv vars)
227 (let ((sb!c:*lexenv* lexenv))
228 (sb!c::funcall-in-symbol-macrolet-lexenv
231 (values sb!c:*lexenv* vars))
233 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
235 (destructuring-bind (test then &optional else) (rest exp)
236 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
241 (destructuring-bind (definitions &rest body) (rest exp)
242 (if (null definitions)
243 (simple-eval-locally `(locally ,@body) lexenv)
244 (%simple-eval exp lexenv))))
246 (if (and (symbolp name)
247 (eq (info :function :kind name) :function))
249 (dolist (arg (rest exp))
250 (args (eval-in-lexenv arg lexenv)))
251 (apply (symbol-function name) (args)))
252 (%simple-eval exp lexenv))))))
256 (defun eval-in-lexenv (exp lexenv)
258 (if (eq *evaluator-mode* :compile)
259 (simple-eval-in-lexenv exp lexenv)
260 (sb!eval:eval-in-native-environment exp lexenv))
262 (simple-eval-in-lexenv exp lexenv))
264 (defun eval (original-exp)
266 "Evaluate the argument in a null lexical environment, returning the
268 (eval-in-lexenv original-exp (make-null-lexenv)))
271 ;;; miscellaneous full function definitions of things which are
272 ;;; ordinarily handled magically by the compiler
274 (defun apply (function arg &rest arguments)
276 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
277 the manner of LIST*. That is, a list is made of the values of all but the
278 last argument, appended to the value of the last argument, which must be a
280 (cond ((atom arguments)
281 (apply function arg))
282 ((atom (cdr arguments))
283 (apply function (cons arg (car arguments))))
284 (t (do* ((a1 arguments a2)
285 (a2 (cdr arguments) (cdr a2)))
288 (apply function (cons arg arguments)))))))
290 (defun funcall (function &rest arguments)
292 "Call FUNCTION with the given ARGUMENTS."
293 (apply function arguments))
295 (defun values (&rest values)
297 "Return all arguments, in order, as values."
298 (declare (truly-dynamic-extent values))
299 (values-list values))
301 (defun values-list (list)
303 "Return all of the elements of LIST, in order, as values."