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 (funcall (sb!c:compile-in-lexenv
18 (gensym "EVAL-TMPFUN-")
23 ;;; Handle PROGN and implicit PROGN.
24 (defun eval-progn-body (progn-body lexenv)
25 (unless (list-with-length-p progn-body)
26 (let ((*print-circle* t))
27 (error 'simple-program-error
29 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
30 :format-arguments (list progn-body))))
32 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
33 ;; need to take care to return all the values of the final EVAL.
34 ;; * It's left as an exercise to the reader to verify that this
35 ;; gives the right result when PROGN-BODY is NIL, because
36 ;; (FIRST NIL) = (REST NIL) = NIL.
37 (do* ((i progn-body rest-i)
38 (rest-i (rest i) (rest i)))
40 (if rest-i ; if not last element of list
41 (eval-in-lexenv (first i) lexenv)
42 (return (eval-in-lexenv (first i) lexenv)))))
44 (defun eval (original-exp)
46 "Evaluate the argument in a null lexical environment, returning the
48 (eval-in-lexenv original-exp (make-null-lexenv)))
50 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
51 ;;; magical cases, and call %EVAL for the rest.
52 (defun eval-in-lexenv (original-exp lexenv)
53 (declare (optimize (safety 1)))
54 ;; (aver (lexenv-simple-p lexenv))
55 (let ((exp (macroexpand original-exp lexenv)))
58 (ecase (info :variable :kind exp)
60 (values (info :variable :constant-value exp)))
63 ;; FIXME: This special case here is a symptom of non-ANSI
64 ;; weirdness in SBCL's ALIEN implementation, which could
65 ;; cause problems for e.g. code walkers. It'd probably be
66 ;; good to ANSIfy it by making alien variable accessors into
67 ;; ordinary forms, e.g. (SB-UNIX:ENV) and (SETF SB-UNIX:ENV),
68 ;; instead of magical symbols, e.g. plain SB-UNIX:ENV. Then
69 ;; if the old magical-symbol syntax is to be retained for
70 ;; compatibility, it can be implemented with
71 ;; DEFINE-SYMBOL-MACRO, keeping the code walkers happy.
73 (%eval original-exp lexenv))))
75 (let ((name (first exp))
76 (n-args (1- (length exp))))
80 (error "wrong number of args to FUNCTION:~% ~S" exp))
81 (let ((name (second exp)))
82 (if (and (or (atom name)
84 (eq (car name) 'setf)))
85 (not (consp (let ((sb!c:*lexenv* lexenv))
86 (sb!c:lexenv-find name funs)))))
88 (%eval original-exp lexenv))))
91 (error "wrong number of args to QUOTE:~% ~S" exp))
94 (unless (evenp n-args)
95 (error "odd number of args to SETQ:~% ~S" exp))
96 (unless (zerop n-args)
97 (do ((name (cdr exp) (cddr name)))
99 (do ((args (cdr exp) (cddr args)))
101 ;; We duplicate the call to SET so that the
102 ;; correct value gets returned.
103 (set (first args) (eval (second args))))
104 (set (first args) (eval (second args)))))
105 (let ((symbol (first name)))
106 (case (info :variable :kind symbol)
107 ;; FIXME: I took out the *TOPLEVEL-AUTO-DECLARE*
108 ;; test here, and removed the *TOPLEVEL-AUTO-DECLARE*
109 ;; variable; the code should now act as though that
110 ;; variable is NIL. This should be tested..
112 (t (return (%eval original-exp lexenv))))))))
114 (eval-progn-body (rest exp) lexenv))
116 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
117 ;; instead of PROGRAM-ERROR when there's something wrong
118 ;; with the syntax here (e.g. missing SITUATIONS). This
119 ;; could be fixed by hand-crafting clauses to catch and
120 ;; report each possibility, but it would probably be
121 ;; cleaner to write a new macro
122 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
123 ;; DESTRUCTURING-BIND and promotes any mismatch to
124 ;; PROGRAM-ERROR, then to use it here and in (probably
125 ;; dozens of) other places where the same problem arises.
126 (destructuring-bind (eval-when situations &rest body) exp
127 (declare (ignore eval-when))
128 (multiple-value-bind (ct lt e)
129 (sb!c:parse-eval-when-situations situations)
130 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
131 ;; the situation :EXECUTE (or EVAL) controls whether
132 ;; evaluation occurs for other EVAL-WHEN forms; that
133 ;; is, those that are not top level forms, or those in
134 ;; code processed by EVAL or COMPILE. If the :EXECUTE
135 ;; situation is specified in such a form, then the
136 ;; body forms are processed as an implicit PROGN;
137 ;; otherwise, the EVAL-WHEN form returns NIL.
138 (declare (ignore ct lt))
140 (eval-progn-body body lexenv)))))
142 (multiple-value-bind (body decls) (parse-body (rest exp) nil)
144 ;; KLUDGE: Uh, yeah. I'm not anticipating
145 ;; winning any prizes for this code, which was
146 ;; written on a "let's get it to work" basis.
147 ;; These seem to be the variables that need
148 ;; bindings for PROCESS-DECLS to work
149 ;; (*FREE-FUNS* and *FREE-VARS* so that
150 ;; references to free functions and variables in
151 ;; the declarations can be noted;
152 ;; *UNDEFINED-WARNINGS* so that warnings about
153 ;; undefined things can be accumulated [and then
154 ;; thrown away, as it happens]). -- CSR, 2002-10-24
155 (let ((sb!c:*lexenv* lexenv)
156 (sb!c::*free-funs* (make-hash-table :test 'equal))
157 (sb!c::*free-vars* (make-hash-table :test 'eq))
158 (sb!c::*undefined-warnings* nil))
159 (sb!c::process-decls decls
161 (sb!c::make-continuation)
163 (eval-progn-body body lexenv))))
165 (destructuring-bind (definitions &rest body)
167 ;; FIXME: shared code with FUNCALL-IN-FOOMACROLET-LEXENV
168 (declare (type list definitions))
169 (unless (= (length definitions)
170 (length (remove-duplicates definitions :key #'first)))
171 (style-warn "duplicate definitions in ~S" definitions))
176 (sb!c::macrolet-definitionize-fun
178 ;; I'm not sure that this is the correct
179 ;; LEXENV to be compiling local macros
183 (eval-in-lexenv `(locally ,@body) lexenv))))
185 (destructuring-bind (definitions &rest body)
187 ;; FIXME: shared code with FUNCALL-IN-FOOMACROLET-LEXENV
188 (declare (type list definitions))
189 (unless (= (length definitions)
190 (length (remove-duplicates definitions :key #'first)))
191 (style-warn "duplicate definitions in ~S" definitions))
196 (sb!c::symbol-macrolet-definitionize-fun
199 (eval-in-lexenv `(locally ,@body) lexenv))))
201 (if (and (symbolp name)
202 (eq (info :function :kind name) :function))
204 (dolist (arg (rest exp))
205 (args (eval-in-lexenv arg lexenv)))
206 (apply (symbol-function name) (args)))
207 (%eval exp lexenv))))))
211 ;;; miscellaneous full function definitions of things which are
212 ;;; ordinarily handled magically by the compiler
214 (defun apply (function arg &rest arguments)
216 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
217 the manner of LIST*. That is, a list is made of the values of all but the
218 last argument, appended to the value of the last argument, which must be a
220 (cond ((atom arguments)
221 (apply function arg))
222 ((atom (cdr arguments))
223 (apply function (cons arg (car arguments))))
224 (t (do* ((a1 arguments a2)
225 (a2 (cdr arguments) (cdr a2)))
228 (apply function (cons arg arguments)))))))
230 (defun funcall (function &rest arguments)
232 "Call FUNCTION with the given ARGUMENTS."
233 (apply function arguments))
235 (defun values (&rest values)
237 "Return all arguments, in order, as values."
238 (values-list values))
240 (defun values-list (list)
242 "Return all of the elements of LIST, in order, as values."