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 ;;; general case of EVAL (except in that it can't handle toplevel
15 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
16 (defun %simple-eval (expr lexenv)
17 ;; FIXME: It might be nice to quieten the toplevel by muffling
18 ;; warnings generated by this compilation (since we're about to
19 ;; execute the results irrespective of the warnings). We might want
20 ;; to be careful about not muffling warnings arising from inner
21 ;; evaluations/compilations, though [e.g. the ignored variable in
22 ;; (DEFUN FOO (X) 1)]. -- CSR, 2003-05-13
23 (let* (;; why PROGN? So that attempts to eval free declarations
24 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
25 (lambda `(lambda () (progn ,expr)))
26 (fun (sb!c:compile-in-lexenv nil lambda lexenv)))
29 ;;; Handle PROGN and implicit PROGN.
30 (defun simple-eval-progn-body (progn-body lexenv)
31 (unless (list-with-length-p progn-body)
32 (let ((*print-circle* t))
33 (error 'simple-program-error
35 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
36 :format-arguments (list progn-body))))
38 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
39 ;; need to take care to return all the values of the final EVAL.
40 ;; * It's left as an exercise to the reader to verify that this
41 ;; gives the right result when PROGN-BODY is NIL, because
42 ;; (FIRST NIL) = (REST NIL) = NIL.
43 (do* ((i progn-body rest-i)
44 (rest-i (rest i) (rest i)))
46 (if rest-i ; if not last element of list
47 (simple-eval-in-lexenv (first i) lexenv)
48 (return (simple-eval-in-lexenv (first i) lexenv)))))
50 (defun simple-eval-locally (exp lexenv &key vars)
51 (multiple-value-bind (body decls)
52 (parse-body (rest exp) :doc-string-allowed nil)
54 ;; KLUDGE: Uh, yeah. I'm not anticipating
55 ;; winning any prizes for this code, which was
56 ;; written on a "let's get it to work" basis.
57 ;; These seem to be the variables that need
58 ;; bindings for PROCESS-DECLS to work
59 ;; (*FREE-FUNS* and *FREE-VARS* so that
60 ;; references to free functions and variables
61 ;; in the declarations can be noted;
62 ;; *UNDEFINED-WARNINGS* so that warnings about
63 ;; undefined things can be accumulated [and
64 ;; then thrown away, as it happens]). -- CSR,
66 (let* ((sb!c:*lexenv* lexenv)
67 (sb!c::*free-funs* (make-hash-table :test 'equal))
68 (sb!c::*free-vars* (make-hash-table :test 'eq))
69 (sb!c::*undefined-warnings* nil))
70 ;; FIXME: VALUES declaration
71 (sb!c::process-decls decls
76 (simple-eval-progn-body body lexenv))))
80 ;;;; Analogous to COMPILER-ERROR, but simpler.
82 (define-condition eval-error (encapsulated-condition)
84 (:report (lambda (condition stream)
85 (print-object (encapsulated-condition condition) stream))))
87 (defun eval-error (condition)
88 (signal 'eval-error :condition condition)
89 (bug "Unhandled EVAL-ERROR"))
91 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
92 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
93 (defun simple-eval-in-lexenv (original-exp lexenv)
94 (declare (optimize (safety 1)))
95 ;; (aver (lexenv-simple-p lexenv))
99 (if (boundp 'sb!c::*compiler-error-bailout*)
100 ;; if we're in the compiler, delegate either to a higher
101 ;; authority or, if that's us, back down to the
102 ;; outermost compiler handler...
106 ;; ... if we're not in the compiler, better signal the
107 ;; error straight away.
108 (invoke-restart 'sb!c::signal-error)))))
109 (let ((exp (macroexpand original-exp lexenv)))
110 (handler-bind ((eval-error
112 (error 'interpreted-program-error
113 :condition (encapsulated-condition condition)
117 (ecase (info :variable :kind exp)
119 (values (info :variable :constant-value exp)))
122 ;; FIXME: This special case here is a symptom of non-ANSI
123 ;; weirdness in SBCL's ALIEN implementation, which could
124 ;; cause problems for e.g. code walkers. It'd probably be
125 ;; good to ANSIfy it by making alien variable accessors
126 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
127 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
128 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
129 ;; be retained for compatibility, it can be implemented
130 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
133 (%simple-eval original-exp lexenv))))
135 (let ((name (first exp))
136 (n-args (1- (length exp))))
140 (error "wrong number of args to FUNCTION:~% ~S" exp))
141 (let ((name (second exp)))
142 (if (and (legal-fun-name-p name)
143 (not (consp (let ((sb!c:*lexenv* lexenv))
144 (sb!c:lexenv-find name funs)))))
145 (%coerce-name-to-fun name)
146 (%simple-eval original-exp lexenv))))
149 (error "wrong number of args to QUOTE:~% ~S" exp))
152 (unless (evenp n-args)
153 (error "odd number of args to SETQ:~% ~S" exp))
154 (unless (zerop n-args)
155 (do ((name (cdr exp) (cddr name)))
157 (do ((args (cdr exp) (cddr args)))
159 ;; We duplicate the call to SET so that the
160 ;; correct value gets returned.
162 (simple-eval-in-lexenv (second args) lexenv)))
164 (simple-eval-in-lexenv (second args) lexenv))))
165 (let ((symbol (first name)))
166 (case (info :variable :kind symbol)
168 (t (return (%simple-eval original-exp lexenv))))
169 (unless (type= (info :variable :type symbol)
171 ;; let the compiler deal with type checking
172 (return (%simple-eval original-exp lexenv)))))))
174 (simple-eval-progn-body (rest exp) lexenv))
176 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
177 ;; instead of PROGRAM-ERROR when there's something wrong
178 ;; with the syntax here (e.g. missing SITUATIONS). This
179 ;; could be fixed by hand-crafting clauses to catch and
180 ;; report each possibility, but it would probably be
181 ;; cleaner to write a new macro
182 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
183 ;; DESTRUCTURING-BIND and promotes any mismatch to
184 ;; PROGRAM-ERROR, then to use it here and in (probably
185 ;; dozens of) other places where the same problem
187 (destructuring-bind (eval-when situations &rest body) exp
188 (declare (ignore eval-when))
189 (multiple-value-bind (ct lt e)
190 (sb!c:parse-eval-when-situations situations)
191 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
192 ;; the situation :EXECUTE (or EVAL) controls whether
193 ;; evaluation occurs for other EVAL-WHEN forms; that
194 ;; is, those that are not top level forms, or those
195 ;; in code processed by EVAL or COMPILE. If the
196 ;; :EXECUTE situation is specified in such a form,
197 ;; then the body forms are processed as an implicit
198 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
199 (declare (ignore ct lt))
201 (simple-eval-progn-body body lexenv)))))
203 (simple-eval-locally exp lexenv))
205 (destructuring-bind (definitions &rest body)
208 (let ((sb!c:*lexenv* lexenv))
209 (sb!c::funcall-in-macrolet-lexenv
212 (declare (ignore funs))
215 (simple-eval-locally `(locally ,@body) lexenv))))
217 (destructuring-bind (definitions &rest body) (rest exp)
218 (multiple-value-bind (lexenv vars)
219 (let ((sb!c:*lexenv* lexenv))
220 (sb!c::funcall-in-symbol-macrolet-lexenv
223 (values sb!c:*lexenv* vars))
225 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
227 (destructuring-bind (test then &optional else) (rest exp)
228 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
233 (destructuring-bind (definitions &rest body) (rest exp)
234 (if (null definitions)
235 (simple-eval-locally `(locally ,@body) lexenv)
236 (%simple-eval exp lexenv))))
238 (if (and (symbolp name)
239 (eq (info :function :kind name) :function))
241 (dolist (arg (rest exp))
242 (args (eval-in-lexenv arg lexenv)))
243 (apply (symbol-function name) (args)))
244 (%simple-eval exp lexenv))))))
248 (defun eval-in-lexenv (exp lexenv)
250 (if (eq *evaluator-mode* :compile)
251 (simple-eval-in-lexenv exp lexenv)
252 (sb!eval:eval-in-native-environment exp lexenv))
254 (simple-eval-in-lexenv exp lexenv))
256 (defun eval (original-exp)
258 "Evaluate the argument in a null lexical environment, returning the
260 (eval-in-lexenv original-exp (make-null-lexenv)))
263 ;;; miscellaneous full function definitions of things which are
264 ;;; ordinarily handled magically by the compiler
266 (defun apply (function arg &rest arguments)
268 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
269 the manner of LIST*. That is, a list is made of the values of all but the
270 last argument, appended to the value of the last argument, which must be a
272 (cond ((atom arguments)
273 (apply function arg))
274 ((atom (cdr arguments))
275 (apply function (cons arg (car arguments))))
276 (t (do* ((a1 arguments a2)
277 (a2 (cdr arguments) (cdr a2)))
280 (apply function (cons arg arguments)))))))
282 (defun funcall (function &rest arguments)
284 "Call FUNCTION with the given ARGUMENTS."
285 (apply function arguments))
287 (defun values (&rest values)
289 "Return all arguments, in order, as values."
290 (declare (dynamic-extent values))
291 (values-list values))
293 (defun values-list (list)
295 "Return all of the elements of LIST, in order, as values."