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 (legal-fun-name-p name)
83 (not (consp (let ((sb!c:*lexenv* lexenv))
84 (sb!c:lexenv-find name funs)))))
86 (%eval original-exp lexenv))))
89 (error "wrong number of args to QUOTE:~% ~S" exp))
92 (unless (evenp n-args)
93 (error "odd number of args to SETQ:~% ~S" exp))
94 (unless (zerop n-args)
95 (do ((name (cdr exp) (cddr name)))
97 (do ((args (cdr exp) (cddr args)))
99 ;; We duplicate the call to SET so that the
100 ;; correct value gets returned.
101 (set (first args) (eval (second args))))
102 (set (first args) (eval (second args)))))
103 (let ((symbol (first name)))
104 (case (info :variable :kind symbol)
105 ;; FIXME: I took out the *TOPLEVEL-AUTO-DECLARE*
106 ;; test here, and removed the *TOPLEVEL-AUTO-DECLARE*
107 ;; variable; the code should now act as though that
108 ;; variable is NIL. This should be tested..
110 (t (return (%eval original-exp lexenv))))))))
112 (eval-progn-body (rest exp) lexenv))
114 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
115 ;; instead of PROGRAM-ERROR when there's something wrong
116 ;; with the syntax here (e.g. missing SITUATIONS). This
117 ;; could be fixed by hand-crafting clauses to catch and
118 ;; report each possibility, but it would probably be
119 ;; cleaner to write a new macro
120 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
121 ;; DESTRUCTURING-BIND and promotes any mismatch to
122 ;; PROGRAM-ERROR, then to use it here and in (probably
123 ;; dozens of) other places where the same problem arises.
124 (destructuring-bind (eval-when situations &rest body) exp
125 (declare (ignore eval-when))
126 (multiple-value-bind (ct lt e)
127 (sb!c:parse-eval-when-situations situations)
128 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
129 ;; the situation :EXECUTE (or EVAL) controls whether
130 ;; evaluation occurs for other EVAL-WHEN forms; that
131 ;; is, those that are not top level forms, or those in
132 ;; code processed by EVAL or COMPILE. If the :EXECUTE
133 ;; situation is specified in such a form, then the
134 ;; body forms are processed as an implicit PROGN;
135 ;; otherwise, the EVAL-WHEN form returns NIL.
136 (declare (ignore ct lt))
138 (eval-progn-body body lexenv)))))
140 (multiple-value-bind (body decls) (parse-body (rest exp) nil)
142 ;; KLUDGE: Uh, yeah. I'm not anticipating
143 ;; winning any prizes for this code, which was
144 ;; written on a "let's get it to work" basis.
145 ;; These seem to be the variables that need
146 ;; bindings for PROCESS-DECLS to work
147 ;; (*FREE-FUNS* and *FREE-VARS* so that
148 ;; references to free functions and variables in
149 ;; the declarations can be noted;
150 ;; *UNDEFINED-WARNINGS* so that warnings about
151 ;; undefined things can be accumulated [and then
152 ;; thrown away, as it happens]). -- CSR, 2002-10-24
153 (let ((sb!c:*lexenv* lexenv)
154 (sb!c::*free-funs* (make-hash-table :test 'equal))
155 (sb!c::*free-vars* (make-hash-table :test 'eq))
156 (sb!c::*undefined-warnings* nil))
157 (sb!c::process-decls decls
159 (sb!c::make-continuation)
161 (eval-progn-body body lexenv))))
163 (destructuring-bind (definitions &rest body)
165 ;; FIXME: shared code with FUNCALL-IN-FOOMACROLET-LEXENV
166 (declare (type list definitions))
167 (unless (= (length definitions)
168 (length (remove-duplicates definitions :key #'first)))
169 (style-warn "duplicate definitions in ~S" definitions))
174 (sb!c::macrolet-definitionize-fun
176 ;; I'm not sure that this is the correct
177 ;; LEXENV to be compiling local macros
181 (eval-in-lexenv `(locally ,@body) lexenv))))
183 (destructuring-bind (definitions &rest body)
185 ;; FIXME: shared code with FUNCALL-IN-FOOMACROLET-LEXENV
186 (declare (type list definitions))
187 (unless (= (length definitions)
188 (length (remove-duplicates definitions :key #'first)))
189 (style-warn "duplicate definitions in ~S" definitions))
194 (sb!c::symbol-macrolet-definitionize-fun
197 (eval-in-lexenv `(locally ,@body) lexenv))))
199 (if (and (symbolp name)
200 (eq (info :function :kind name) :function))
202 (dolist (arg (rest exp))
203 (args (eval-in-lexenv arg lexenv)))
204 (apply (symbol-function name) (args)))
205 (%eval exp lexenv))))))
209 ;;; miscellaneous full function definitions of things which are
210 ;;; ordinarily handled magically by the compiler
212 (defun apply (function arg &rest arguments)
214 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
215 the manner of LIST*. That is, a list is made of the values of all but the
216 last argument, appended to the value of the last argument, which must be a
218 (cond ((atom arguments)
219 (apply function arg))
220 ((atom (cdr arguments))
221 (apply function (cons arg (car arguments))))
222 (t (do* ((a1 arguments a2)
223 (a2 (cdr arguments) (cdr a2)))
226 (apply function (cons arg arguments)))))))
228 (defun funcall (function &rest arguments)
230 "Call FUNCTION with the given ARGUMENTS."
231 (apply function arguments))
233 (defun values (&rest values)
235 "Return all arguments, in order, as values."
236 (values-list values))
238 (defun values-list (list)
240 "Return all of the elements of LIST, in order, as values."