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