0.8.0.19:
[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 () ,expr)
25                                      lexenv)))
26     (funcall fun)))
27
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
33              :format-control
34              "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
35              :format-arguments (list progn-body))))
36   ;; Note:
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)))
44       (nil)
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)))))
48
49 (defun eval (original-exp)
50   #!+sb-doc
51   "Evaluate the argument in a null lexical environment, returning the
52   result or results."
53   (eval-in-lexenv original-exp (make-null-lexenv)))
54
55 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
56 ;;; magical cases, and call %EVAL for the rest.
57 (defun eval-in-lexenv (original-exp lexenv)
58   (declare (optimize (safety 1)))
59   ;; (aver (lexenv-simple-p lexenv))
60   (handler-bind
61       ((sb!c:compiler-error
62         (lambda (c)
63           (if (boundp 'sb!c::*compiler-error-bailout*)
64               ;; if we're in the compiler, delegate either to a higher
65               ;; authority or, if that's us, back down to the
66               ;; outermost compiler handler...
67               (progn
68                 (signal c)
69                 nil)
70               ;; ... if we're not in the compiler, better signal a
71               ;; program error straight away.
72               (invoke-restart 'sb!c::signal-program-error)))))
73     (let ((exp (macroexpand original-exp lexenv)))
74       (typecase exp
75         (symbol
76          (ecase (info :variable :kind exp)
77            (:constant
78             (values (info :variable :constant-value exp)))
79            ((:special :global)
80             (symbol-value exp))
81            ;; FIXME: This special case here is a symptom of non-ANSI
82            ;; weirdness in SBCL's ALIEN implementation, which could
83            ;; cause problems for e.g. code walkers. It'd probably be
84            ;; good to ANSIfy it by making alien variable accessors
85            ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
86            ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
87            ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
88            ;; be retained for compatibility, it can be implemented
89            ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
90            ;; happy.
91            (:alien
92             (%eval original-exp lexenv))))
93         (list
94          (let ((name (first exp))
95                (n-args (1- (length exp))))
96            (case name
97              ((function)
98               (unless (= n-args 1)
99                 (error "wrong number of args to FUNCTION:~% ~S" exp))
100               (let ((name (second exp)))
101                 (if (and (legal-fun-name-p name)
102                          (not (consp (let ((sb!c:*lexenv* lexenv))
103                                        (sb!c:lexenv-find name funs)))))
104                     (fdefinition name)
105                     (%eval original-exp lexenv))))
106              ((quote)
107               (unless (= n-args 1)
108                 (error "wrong number of args to QUOTE:~% ~S" exp))
109               (second exp))
110              (setq
111               (unless (evenp n-args)
112                 (error "odd number of args to SETQ:~% ~S" exp))
113               (unless (zerop n-args)
114                 (do ((name (cdr exp) (cddr name)))
115                     ((null name)
116                      (do ((args (cdr exp) (cddr args)))
117                          ((null (cddr args))
118                           ;; We duplicate the call to SET so that the
119                           ;; correct value gets returned.
120                           (set (first args) (eval (second args))))
121                        (set (first args) (eval (second args)))))
122                   (let ((symbol (first name)))
123                     (case (info :variable :kind symbol)
124                       (:special)
125                       (t (return (%eval original-exp lexenv))))
126                     (unless (type= (info :variable :type symbol)
127                                    *universal-type*)
128                       ;; let the compiler deal with type checking
129                       (return (%eval original-exp lexenv)))))))
130              ((progn)
131               (eval-progn-body (rest exp) lexenv))
132              ((eval-when)
133               ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
134               ;; instead of PROGRAM-ERROR when there's something wrong
135               ;; with the syntax here (e.g. missing SITUATIONS). This
136               ;; could be fixed by hand-crafting clauses to catch and
137               ;; report each possibility, but it would probably be
138               ;; cleaner to write a new macro
139               ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
140               ;; DESTRUCTURING-BIND and promotes any mismatch to
141               ;; PROGRAM-ERROR, then to use it here and in (probably
142               ;; dozens of) other places where the same problem
143               ;; arises.
144               (destructuring-bind (eval-when situations &rest body) exp
145                 (declare (ignore eval-when))
146                 (multiple-value-bind (ct lt e)
147                     (sb!c:parse-eval-when-situations situations)
148                   ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
149                   ;; the situation :EXECUTE (or EVAL) controls whether
150                   ;; evaluation occurs for other EVAL-WHEN forms; that
151                   ;; is, those that are not top level forms, or those
152                   ;; in code processed by EVAL or COMPILE. If the
153                   ;; :EXECUTE situation is specified in such a form,
154                   ;; then the body forms are processed as an implicit
155                   ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
156                   (declare (ignore ct lt))
157                   (when e
158                     (eval-progn-body body lexenv)))))
159              ((locally)
160               (multiple-value-bind (body decls) (parse-body (rest exp) nil)
161                 (let ((lexenv
162                        ;; KLUDGE: Uh, yeah.  I'm not anticipating
163                        ;; winning any prizes for this code, which was
164                        ;; written on a "let's get it to work" basis.
165                        ;; These seem to be the variables that need
166                        ;; bindings for PROCESS-DECLS to work
167                        ;; (*FREE-FUNS* and *FREE-VARS* so that
168                        ;; references to free functions and variables
169                        ;; in the declarations can be noted;
170                        ;; *UNDEFINED-WARNINGS* so that warnings about
171                        ;; undefined things can be accumulated [and
172                        ;; then thrown away, as it happens]). -- CSR,
173                        ;; 2002-10-24
174                        (let ((sb!c:*lexenv* lexenv)
175                              (sb!c::*free-funs* (make-hash-table :test 'equal))
176                              (sb!c::*free-vars* (make-hash-table :test 'eq))
177                              (sb!c::*undefined-warnings* nil))
178                          (sb!c::process-decls decls
179                                               nil nil
180                                               (sb!c::make-continuation)
181                                               lexenv))))
182                   (eval-progn-body body lexenv))))
183              ((macrolet)
184               (destructuring-bind (definitions &rest body)
185                   (rest exp)
186                 ;; FIXME: shared code with
187                 ;; FUNCALL-IN-FOOMACROLET-LEXENV
188                 (declare (type list definitions))
189                 (unless (= (length definitions)
190                            (length (remove-duplicates definitions
191                                                       :key #'first)))
192                   (style-warn "duplicate definitions in ~S" definitions))
193                 (let ((lexenv
194                        (sb!c::make-lexenv
195                         :default lexenv
196                         :funs (mapcar
197                                (sb!c::macrolet-definitionize-fun
198                                 :eval
199                                 ;; I'm not sure that this is the
200                                 ;; correct LEXENV to be compiling
201                                 ;; local macros in...
202                                 lexenv)
203                                definitions))))
204                   (eval-in-lexenv `(locally ,@body) lexenv))))
205              ((symbol-macrolet)
206               (destructuring-bind (definitions &rest body)
207                   (rest exp)
208                 ;; FIXME: shared code with
209                 ;; FUNCALL-IN-FOOMACROLET-LEXENV
210                 (declare (type list definitions))
211                 (unless (= (length definitions)
212                            (length (remove-duplicates definitions
213                                                       :key #'first)))
214                   (style-warn "duplicate definitions in ~S" definitions))
215                 (let ((lexenv
216                        (sb!c::make-lexenv
217                         :default lexenv
218                         :vars (mapcar
219                                (sb!c::symbol-macrolet-definitionize-fun
220                                 :eval)
221                                definitions))))
222                   (eval-in-lexenv `(locally ,@body) lexenv))))
223              (t
224               (if (and (symbolp name)
225                        (eq (info :function :kind name) :function))
226                   (collect ((args))
227                            (dolist (arg (rest exp))
228                              (args (eval-in-lexenv arg lexenv)))
229                            (apply (symbol-function name) (args)))
230                   (%eval exp lexenv))))))
231         (t
232          exp)))))
233 \f
234 ;;; miscellaneous full function definitions of things which are
235 ;;; ordinarily handled magically by the compiler
236
237 (defun apply (function arg &rest arguments)
238   #!+sb-doc
239   "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
240   the manner of LIST*. That is, a list is made of the values of all but the
241   last argument, appended to the value of the last argument, which must be a
242   list."
243   (cond ((atom arguments)
244          (apply function arg))
245         ((atom (cdr arguments))
246          (apply function (cons arg (car arguments))))
247         (t (do* ((a1 arguments a2)
248                  (a2 (cdr arguments) (cdr a2)))
249                 ((atom (cdr a2))
250                  (rplacd a1 (car a2))
251                  (apply function (cons arg arguments)))))))
252
253 (defun funcall (function &rest arguments)
254   #!+sb-doc
255   "Call FUNCTION with the given ARGUMENTS."
256   (apply function arguments))
257
258 (defun values (&rest values)
259   #!+sb-doc
260   "Return all arguments, in order, as values."
261   (values-list values))
262
263 (defun values-list (list)
264   #!+sb-doc
265   "Return all of the elements of LIST, in order, as values."
266   (values-list list))