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 %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 ((fun (sb!c:compile-in-lexenv (gensym "EVAL-TMPFUN-")
29 ;;; Handle PROGN and implicit PROGN.
30 (defun 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 (eval-in-lexenv (first i) lexenv)
48 (return (eval-in-lexenv (first i) lexenv)))))
50 (defun eval (original-exp)
52 "Evaluate the argument in a null lexical environment, returning the
54 (eval-in-lexenv original-exp (make-null-lexenv)))
56 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
57 ;;; magical cases, and call %EVAL for the rest.
58 (defun eval-in-lexenv (original-exp lexenv)
59 (declare (optimize (safety 1)))
60 ;; (aver (lexenv-simple-p lexenv))
64 (if (boundp 'sb!c::*compiler-error-bailout*)
65 ;; if we're in the compiler, delegate either to a higher
66 ;; authority or, if that's us, back down to the
67 ;; outermost compiler handler...
71 ;; ... if we're not in the compiler, better signal a
72 ;; program error straight away.
73 (invoke-restart 'sb!c::signal-program-error)))))
74 (let ((exp (macroexpand original-exp lexenv)))
77 (ecase (info :variable :kind exp)
79 (values (info :variable :constant-value exp)))
82 ;; FIXME: This special case here is a symptom of non-ANSI
83 ;; weirdness in SBCL's ALIEN implementation, which could
84 ;; cause problems for e.g. code walkers. It'd probably be
85 ;; good to ANSIfy it by making alien variable accessors
86 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
87 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
88 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
89 ;; be retained for compatibility, it can be implemented
90 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
93 (%eval original-exp lexenv))))
95 (let ((name (first exp))
96 (n-args (1- (length exp))))
100 (error "wrong number of args to FUNCTION:~% ~S" exp))
101 (let ((name (second exp)))
102 (if (and (legal-fun-name-p name)
103 (not (consp (let ((sb!c:*lexenv* lexenv))
104 (sb!c:lexenv-find name funs)))))
106 (%eval original-exp lexenv))))
109 (error "wrong number of args to QUOTE:~% ~S" exp))
112 (unless (evenp n-args)
113 (error "odd number of args to SETQ:~% ~S" exp))
114 (unless (zerop n-args)
115 (do ((name (cdr exp) (cddr name)))
117 (do ((args (cdr exp) (cddr args)))
119 ;; We duplicate the call to SET so that the
120 ;; correct value gets returned.
121 (set (first args) (eval (second args))))
122 (set (first args) (eval (second args)))))
123 (let ((symbol (first name)))
124 (case (info :variable :kind symbol)
125 ;; FIXME: I took out the *TOPLEVEL-AUTO-DECLARE*
126 ;; test here, and removed the
127 ;; *TOPLEVEL-AUTO-DECLARE* variable; the code
128 ;; should now act as though that variable is
129 ;; NIL. This should be tested..
131 (t (return (%eval original-exp lexenv))))))))
133 (eval-progn-body (rest exp) lexenv))
135 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
136 ;; instead of PROGRAM-ERROR when there's something wrong
137 ;; with the syntax here (e.g. missing SITUATIONS). This
138 ;; could be fixed by hand-crafting clauses to catch and
139 ;; report each possibility, but it would probably be
140 ;; cleaner to write a new macro
141 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
142 ;; DESTRUCTURING-BIND and promotes any mismatch to
143 ;; PROGRAM-ERROR, then to use it here and in (probably
144 ;; dozens of) other places where the same problem
146 (destructuring-bind (eval-when situations &rest body) exp
147 (declare (ignore eval-when))
148 (multiple-value-bind (ct lt e)
149 (sb!c:parse-eval-when-situations situations)
150 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
151 ;; the situation :EXECUTE (or EVAL) controls whether
152 ;; evaluation occurs for other EVAL-WHEN forms; that
153 ;; is, those that are not top level forms, or those
154 ;; in code processed by EVAL or COMPILE. If the
155 ;; :EXECUTE situation is specified in such a form,
156 ;; then the body forms are processed as an implicit
157 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
158 (declare (ignore ct lt))
160 (eval-progn-body body lexenv)))))
162 (multiple-value-bind (body decls) (parse-body (rest exp) nil)
164 ;; KLUDGE: Uh, yeah. I'm not anticipating
165 ;; winning any prizes for this code, which was
166 ;; written on a "let's get it to work" basis.
167 ;; These seem to be the variables that need
168 ;; bindings for PROCESS-DECLS to work
169 ;; (*FREE-FUNS* and *FREE-VARS* so that
170 ;; references to free functions and variables
171 ;; in the declarations can be noted;
172 ;; *UNDEFINED-WARNINGS* so that warnings about
173 ;; undefined things can be accumulated [and
174 ;; then thrown away, as it happens]). -- CSR,
176 (let ((sb!c:*lexenv* lexenv)
177 (sb!c::*free-funs* (make-hash-table :test 'equal))
178 (sb!c::*free-vars* (make-hash-table :test 'eq))
179 (sb!c::*undefined-warnings* nil))
180 (sb!c::process-decls decls
182 (sb!c::make-continuation)
184 (eval-progn-body body lexenv))))
186 (destructuring-bind (definitions &rest body)
188 ;; FIXME: shared code with
189 ;; FUNCALL-IN-FOOMACROLET-LEXENV
190 (declare (type list definitions))
191 (unless (= (length definitions)
192 (length (remove-duplicates definitions
194 (style-warn "duplicate definitions in ~S" definitions))
199 (sb!c::macrolet-definitionize-fun
201 ;; I'm not sure that this is the
202 ;; correct LEXENV to be compiling
203 ;; local macros in...
206 (eval-in-lexenv `(locally ,@body) lexenv))))
208 (destructuring-bind (definitions &rest body)
210 ;; FIXME: shared code with
211 ;; FUNCALL-IN-FOOMACROLET-LEXENV
212 (declare (type list definitions))
213 (unless (= (length definitions)
214 (length (remove-duplicates definitions
216 (style-warn "duplicate definitions in ~S" definitions))
221 (sb!c::symbol-macrolet-definitionize-fun
224 (eval-in-lexenv `(locally ,@body) lexenv))))
226 (if (and (symbolp name)
227 (eq (info :function :kind name) :function))
229 (dolist (arg (rest exp))
230 (args (eval-in-lexenv arg lexenv)))
231 (apply (symbol-function name) (args)))
232 (%eval exp lexenv))))))
236 ;;; miscellaneous full function definitions of things which are
237 ;;; ordinarily handled magically by the compiler
239 (defun apply (function arg &rest arguments)
241 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
242 the manner of LIST*. That is, a list is made of the values of all but the
243 last argument, appended to the value of the last argument, which must be a
245 (cond ((atom arguments)
246 (apply function arg))
247 ((atom (cdr arguments))
248 (apply function (cons arg (car arguments))))
249 (t (do* ((a1 arguments a2)
250 (a2 (cdr arguments) (cdr a2)))
253 (apply function (cons arg arguments)))))))
255 (defun funcall (function &rest arguments)
257 "Call FUNCTION with the given ARGUMENTS."
258 (apply function arguments))
260 (defun values (&rest values)
262 "Return all arguments, in order, as values."
263 (values-list values))
265 (defun values-list (list)
267 "Return all of the elements of LIST, in order, as values."