0.7.9.6:
[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   (funcall (sb!c:compile-in-lexenv
18             (gensym "EVAL-TMPFUN-")
19             `(lambda ()
20                ,expr)
21             lexenv)))
22
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
28              :format-control
29              "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
30              :format-arguments (list progn-body))))
31   ;; Note:
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)))
39       (nil)
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)))))
43
44 (defun eval (original-exp)
45   #!+sb-doc
46   "Evaluate the argument in a null lexical environment, returning the
47   result or results."
48   (eval-in-lexenv original-exp (make-null-lexenv)))
49
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)))
56     (typecase exp
57       (symbol
58        (ecase (info :variable :kind exp)
59          (:constant
60           (values (info :variable :constant-value exp)))
61          ((:special :global)
62           (symbol-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.
72          (:alien
73           (%eval original-exp lexenv))))
74       (list
75        (let ((name (first exp))
76              (n-args (1- (length exp))))
77          (case name
78            ((function)
79             (unless (= n-args 1)
80               (error "wrong number of args to FUNCTION:~% ~S" exp))
81             (let ((name (second exp)))
82               (if (and (or (atom name)
83                            (and (consp name)
84                                 (eq (car name) 'setf)))
85                        (not (consp (let ((sb!c:*lexenv* lexenv))
86                                      (sb!c:lexenv-find name funs)))))
87                   (fdefinition name)
88                   (%eval original-exp lexenv))))
89            ((quote)
90             (unless (= n-args 1)
91               (error "wrong number of args to QUOTE:~% ~S" exp))
92             (second exp))
93            (setq
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)))
98                   ((null name)
99                    (do ((args (cdr exp) (cddr args)))
100                        ((null (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..
111                     (:special)
112                     (t (return (%eval original-exp lexenv))))))))
113            ((progn)
114             (eval-progn-body (rest exp) lexenv))
115            ((eval-when)
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))
139                 (when e
140                   (eval-progn-body body lexenv)))))
141            ((locally)
142             (multiple-value-bind (body decls) (parse-body (rest exp) nil)
143               (let ((lexenv
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
160                                             nil nil
161                                             (sb!c::make-continuation)
162                                             lexenv))))
163                 (eval-progn-body body lexenv))))
164            ((macrolet)
165             (destructuring-bind (definitions &rest body)
166                 (rest exp)
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))
172               (let ((lexenv
173                      (sb!c::make-lexenv
174                       :default lexenv
175                       :funs (mapcar
176                              (sb!c::macrolet-definitionize-fun
177                               :eval
178                               ;; I'm not sure that this is the correct
179                               ;; LEXENV to be compiling local macros
180                               ;; in...
181                               lexenv)
182                              definitions))))
183                 (eval-in-lexenv `(locally ,@body) lexenv))))
184            ((symbol-macrolet)
185             (destructuring-bind (definitions &rest body)
186                 (rest exp)
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))
192               (let ((lexenv
193                      (sb!c::make-lexenv
194                       :default lexenv
195                       :vars (mapcar
196                              (sb!c::symbol-macrolet-definitionize-fun
197                               :eval)
198                              definitions))))
199                 (eval-in-lexenv `(locally ,@body) lexenv))))
200            (t
201             (if (and (symbolp name)
202                      (eq (info :function :kind name) :function))
203                 (collect ((args))
204                          (dolist (arg (rest exp))
205                            (args (eval-in-lexenv arg lexenv)))
206                          (apply (symbol-function name) (args)))
207                 (%eval original-exp lexenv))))))
208       (t
209        exp))))
210 \f
211 ;;; miscellaneous full function definitions of things which are
212 ;;; ordinarily handled magically by the compiler
213
214 (defun apply (function arg &rest arguments)
215   #!+sb-doc
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
219   list."
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)))
226                 ((atom (cdr a2))
227                  (rplacd a1 (car a2))
228                  (apply function (cons arg arguments)))))))
229
230 (defun funcall (function &rest arguments)
231   #!+sb-doc
232   "Call FUNCTION with the given ARGUMENTS."
233   (apply function arguments))
234
235 (defun values (&rest values)
236   #!+sb-doc
237   "Return all arguments, in order, as values."
238   (values-list values))
239
240 (defun values-list (list)
241   #!+sb-doc
242   "Return all of the elements of LIST, in order, as values."
243   (values-list list))