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-")
28 ;;; Handle PROGN and implicit PROGN.
29 (defun eval-progn-body (progn-body lexenv)
30 (unless (list-with-length-p progn-body)
31 (let ((*print-circle* t))
32 (error 'simple-program-error
34 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
35 :format-arguments (list progn-body))))
37 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
38 ;; need to take care to return all the values of the final EVAL.
39 ;; * It's left as an exercise to the reader to verify that this
40 ;; gives the right result when PROGN-BODY is NIL, because
41 ;; (FIRST NIL) = (REST NIL) = NIL.
42 (do* ((i progn-body rest-i)
43 (rest-i (rest i) (rest i)))
45 (if rest-i ; if not last element of list
46 (eval-in-lexenv (first i) lexenv)
47 (return (eval-in-lexenv (first i) lexenv)))))
49 (defun eval-locally (exp lexenv &optional vars)
50 (multiple-value-bind (body decls) (parse-body (rest exp) nil)
52 ;; KLUDGE: Uh, yeah. I'm not anticipating
53 ;; winning any prizes for this code, which was
54 ;; written on a "let's get it to work" basis.
55 ;; These seem to be the variables that need
56 ;; bindings for PROCESS-DECLS to work
57 ;; (*FREE-FUNS* and *FREE-VARS* so that
58 ;; references to free functions and variables
59 ;; in the declarations can be noted;
60 ;; *UNDEFINED-WARNINGS* so that warnings about
61 ;; undefined things can be accumulated [and
62 ;; then thrown away, as it happens]). -- CSR,
64 (let ((sb!c:*lexenv* lexenv)
65 (sb!c::*free-funs* (make-hash-table :test 'equal))
66 (sb!c::*free-vars* (make-hash-table :test 'eq))
67 (sb!c::*undefined-warnings* nil))
68 (sb!c::process-decls decls
71 (sb!c::make-continuation)
73 (eval-progn-body body lexenv))))
75 (defun eval (original-exp)
77 "Evaluate the argument in a null lexical environment, returning the
79 (eval-in-lexenv original-exp (make-null-lexenv)))
81 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
82 ;;; magical cases, and call %EVAL for the rest.
83 (defun eval-in-lexenv (original-exp lexenv)
84 (declare (optimize (safety 1)))
85 ;; (aver (lexenv-simple-p lexenv))
89 (if (boundp 'sb!c::*compiler-error-bailout*)
90 ;; if we're in the compiler, delegate either to a higher
91 ;; authority or, if that's us, back down to the
92 ;; outermost compiler handler...
96 ;; ... if we're not in the compiler, better signal a
97 ;; program error straight away.
98 (invoke-restart 'sb!c::signal-program-error)))))
99 (let ((exp (macroexpand original-exp lexenv)))
102 (ecase (info :variable :kind exp)
104 (values (info :variable :constant-value exp)))
107 ;; FIXME: This special case here is a symptom of non-ANSI
108 ;; weirdness in SBCL's ALIEN implementation, which could
109 ;; cause problems for e.g. code walkers. It'd probably be
110 ;; good to ANSIfy it by making alien variable accessors
111 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
112 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
113 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
114 ;; be retained for compatibility, it can be implemented
115 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
118 (%eval original-exp lexenv))))
120 (let ((name (first exp))
121 (n-args (1- (length exp))))
125 (error "wrong number of args to FUNCTION:~% ~S" exp))
126 (let ((name (second exp)))
127 (if (and (legal-fun-name-p name)
128 (not (consp (let ((sb!c:*lexenv* lexenv))
129 (sb!c:lexenv-find name funs)))))
131 (%eval original-exp lexenv))))
134 (error "wrong number of args to QUOTE:~% ~S" exp))
137 (unless (evenp n-args)
138 (error "odd number of args to SETQ:~% ~S" exp))
139 (unless (zerop n-args)
140 (do ((name (cdr exp) (cddr name)))
142 (do ((args (cdr exp) (cddr args)))
144 ;; We duplicate the call to SET so that the
145 ;; correct value gets returned.
146 (set (first args) (eval (second args))))
147 (set (first args) (eval (second args)))))
148 (let ((symbol (first name)))
149 (case (info :variable :kind symbol)
151 (t (return (%eval original-exp lexenv))))
152 (unless (type= (info :variable :type symbol)
154 ;; let the compiler deal with type checking
155 (return (%eval original-exp lexenv)))))))
157 (eval-progn-body (rest exp) lexenv))
159 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
160 ;; instead of PROGRAM-ERROR when there's something wrong
161 ;; with the syntax here (e.g. missing SITUATIONS). This
162 ;; could be fixed by hand-crafting clauses to catch and
163 ;; report each possibility, but it would probably be
164 ;; cleaner to write a new macro
165 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
166 ;; DESTRUCTURING-BIND and promotes any mismatch to
167 ;; PROGRAM-ERROR, then to use it here and in (probably
168 ;; dozens of) other places where the same problem
170 (destructuring-bind (eval-when situations &rest body) exp
171 (declare (ignore eval-when))
172 (multiple-value-bind (ct lt e)
173 (sb!c:parse-eval-when-situations situations)
174 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
175 ;; the situation :EXECUTE (or EVAL) controls whether
176 ;; evaluation occurs for other EVAL-WHEN forms; that
177 ;; is, those that are not top level forms, or those
178 ;; in code processed by EVAL or COMPILE. If the
179 ;; :EXECUTE situation is specified in such a form,
180 ;; then the body forms are processed as an implicit
181 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
182 (declare (ignore ct lt))
184 (eval-progn-body body lexenv)))))
186 (eval-locally exp lexenv))
188 (destructuring-bind (definitions &rest body)
191 (let ((sb!c:*lexenv* lexenv))
192 (sb!c::funcall-in-macrolet-lexenv
195 (declare (ignore funs))
198 (eval-locally `(locally ,@body) lexenv))))
200 (destructuring-bind (definitions &rest body)
202 (multiple-value-bind (lexenv vars)
203 (let ((sb!c:*lexenv* lexenv))
204 (sb!c::funcall-in-symbol-macrolet-lexenv
207 (values sb!c:*lexenv* vars))
209 (eval-locally `(locally ,@body) lexenv vars))))
211 (if (and (symbolp name)
212 (eq (info :function :kind name) :function))
214 (dolist (arg (rest exp))
215 (args (eval-in-lexenv arg lexenv)))
216 (apply (symbol-function name) (args)))
217 (%eval exp lexenv))))))
221 ;;; miscellaneous full function definitions of things which are
222 ;;; ordinarily handled magically by the compiler
224 (defun apply (function arg &rest arguments)
226 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
227 the manner of LIST*. That is, a list is made of the values of all but the
228 last argument, appended to the value of the last argument, which must be a
230 (cond ((atom arguments)
231 (apply function arg))
232 ((atom (cdr arguments))
233 (apply function (cons arg (car arguments))))
234 (t (do* ((a1 arguments a2)
235 (a2 (cdr arguments) (cdr a2)))
238 (apply function (cons arg arguments)))))))
240 (defun funcall (function &rest arguments)
242 "Call FUNCTION with the given ARGUMENTS."
243 (apply function arguments))
245 (defun values (&rest values)
247 "Return all arguments, in order, as values."
248 (values-list values))
250 (defun values-list (list)
252 "Return all of the elements of LIST, in order, as values."