b81bd7c9df69c232e87515ecbdd3ec20bc8b8c9f
[sbcl.git] / src / code / eval.lisp
1 ;;;; EVAL and friends
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!IMPL")
13
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-")
24                                      `(lambda ()
25                                        ,expr)
26                                      lexenv)))
27     (funcall fun)))
28
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
34              :format-control
35              "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
36              :format-arguments (list progn-body))))
37   ;; Note:
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)))
45       (nil)
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)))))
49
50 (defun eval (original-exp)
51   #!+sb-doc
52   "Evaluate the argument in a null lexical environment, returning the
53   result or results."
54   (eval-in-lexenv original-exp (make-null-lexenv)))
55
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))
61   (handler-bind
62       ((sb!c:compiler-error
63         (lambda (c)
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...
68               (progn
69                 (signal c)
70                 nil)
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)))
75       (typecase exp
76         (symbol
77          (ecase (info :variable :kind exp)
78            (:constant
79             (values (info :variable :constant-value exp)))
80            ((:special :global)
81             (symbol-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
91            ;; happy.
92            (:alien
93             (%eval original-exp lexenv))))
94         (list
95          (let ((name (first exp))
96                (n-args (1- (length exp))))
97            (case name
98              ((function)
99               (unless (= n-args 1)
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)))))
105                     (fdefinition name)
106                     (%eval original-exp lexenv))))
107              ((quote)
108               (unless (= n-args 1)
109                 (error "wrong number of args to QUOTE:~% ~S" exp))
110               (second exp))
111              (setq
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)))
116                     ((null name)
117                      (do ((args (cdr exp) (cddr args)))
118                          ((null (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..
130                       (:special)
131                       (t (return (%eval original-exp lexenv))))))))
132              ((progn)
133               (eval-progn-body (rest exp) lexenv))
134              ((eval-when)
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
145               ;; arises.
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))
159                   (when e
160                     (eval-progn-body body lexenv)))))
161              ((locally)
162               (multiple-value-bind (body decls) (parse-body (rest exp) nil)
163                 (let ((lexenv
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,
175                        ;; 2002-10-24
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
181                                               nil nil
182                                               (sb!c::make-continuation)
183                                               lexenv))))
184                   (eval-progn-body body lexenv))))
185              ((macrolet)
186               (destructuring-bind (definitions &rest body)
187                   (rest exp)
188                 ;; FIXME: shared code with
189                 ;; FUNCALL-IN-FOOMACROLET-LEXENV
190                 (declare (type list definitions))
191                 (unless (= (length definitions)
192                            (length (remove-duplicates definitions
193                                                       :key #'first)))
194                   (style-warn "duplicate definitions in ~S" definitions))
195                 (let ((lexenv
196                        (sb!c::make-lexenv
197                         :default lexenv
198                         :funs (mapcar
199                                (sb!c::macrolet-definitionize-fun
200                                 :eval
201                                 ;; I'm not sure that this is the
202                                 ;; correct LEXENV to be compiling
203                                 ;; local macros in...
204                                 lexenv)
205                                definitions))))
206                   (eval-in-lexenv `(locally ,@body) lexenv))))
207              ((symbol-macrolet)
208               (destructuring-bind (definitions &rest body)
209                   (rest exp)
210                 ;; FIXME: shared code with
211                 ;; FUNCALL-IN-FOOMACROLET-LEXENV
212                 (declare (type list definitions))
213                 (unless (= (length definitions)
214                            (length (remove-duplicates definitions
215                                                       :key #'first)))
216                   (style-warn "duplicate definitions in ~S" definitions))
217                 (let ((lexenv
218                        (sb!c::make-lexenv
219                         :default lexenv
220                         :vars (mapcar
221                                (sb!c::symbol-macrolet-definitionize-fun
222                                 :eval)
223                                definitions))))
224                   (eval-in-lexenv `(locally ,@body) lexenv))))
225              (t
226               (if (and (symbolp name)
227                        (eq (info :function :kind name) :function))
228                   (collect ((args))
229                            (dolist (arg (rest exp))
230                              (args (eval-in-lexenv arg lexenv)))
231                            (apply (symbol-function name) (args)))
232                   (%eval exp lexenv))))))
233         (t
234          exp)))))
235 \f
236 ;;; miscellaneous full function definitions of things which are
237 ;;; ordinarily handled magically by the compiler
238
239 (defun apply (function arg &rest arguments)
240   #!+sb-doc
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
244   list."
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)))
251                 ((atom (cdr a2))
252                  (rplacd a1 (car a2))
253                  (apply function (cons arg arguments)))))))
254
255 (defun funcall (function &rest arguments)
256   #!+sb-doc
257   "Call FUNCTION with the given ARGUMENTS."
258   (apply function arguments))
259
260 (defun values (&rest values)
261   #!+sb-doc
262   "Return all arguments, in order, as values."
263   (values-list values))
264
265 (defun values-list (list)
266   #!+sb-doc
267   "Return all of the elements of LIST, in order, as values."
268   (values-list list))