0.7.12.47:
[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 (legal-fun-name-p name)
83                        (not (consp (let ((sb!c:*lexenv* lexenv))
84                                      (sb!c:lexenv-find name funs)))))
85                   (fdefinition name)
86                   (%eval original-exp lexenv))))
87            ((quote)
88             (unless (= n-args 1)
89               (error "wrong number of args to QUOTE:~% ~S" exp))
90             (second exp))
91            (setq
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)))
96                   ((null name)
97                    (do ((args (cdr exp) (cddr args)))
98                        ((null (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..
109                     (:special)
110                     (t (return (%eval original-exp lexenv))))))))
111            ((progn)
112             (eval-progn-body (rest exp) lexenv))
113            ((eval-when)
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))
137                 (when e
138                   (eval-progn-body body lexenv)))))
139            ((locally)
140             (multiple-value-bind (body decls) (parse-body (rest exp) nil)
141               (let ((lexenv
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
158                                             nil nil
159                                             (sb!c::make-continuation)
160                                             lexenv))))
161                 (eval-progn-body body lexenv))))
162            ((macrolet)
163             (destructuring-bind (definitions &rest body)
164                 (rest exp)
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))
170               (let ((lexenv
171                      (sb!c::make-lexenv
172                       :default lexenv
173                       :funs (mapcar
174                              (sb!c::macrolet-definitionize-fun
175                               :eval
176                               ;; I'm not sure that this is the correct
177                               ;; LEXENV to be compiling local macros
178                               ;; in...
179                               lexenv)
180                              definitions))))
181                 (eval-in-lexenv `(locally ,@body) lexenv))))
182            ((symbol-macrolet)
183             (destructuring-bind (definitions &rest body)
184                 (rest exp)
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))
190               (let ((lexenv
191                      (sb!c::make-lexenv
192                       :default lexenv
193                       :vars (mapcar
194                              (sb!c::symbol-macrolet-definitionize-fun
195                               :eval)
196                              definitions))))
197                 (eval-in-lexenv `(locally ,@body) lexenv))))
198            (t
199             (if (and (symbolp name)
200                      (eq (info :function :kind name) :function))
201                 (collect ((args))
202                          (dolist (arg (rest exp))
203                            (args (eval-in-lexenv arg lexenv)))
204                          (apply (symbol-function name) (args)))
205                 (%eval exp lexenv))))))
206       (t
207        exp))))
208 \f
209 ;;; miscellaneous full function definitions of things which are
210 ;;; ordinarily handled magically by the compiler
211
212 (defun apply (function arg &rest arguments)
213   #!+sb-doc
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
217   list."
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)))
224                 ((atom (cdr a2))
225                  (rplacd a1 (car a2))
226                  (apply function (cons arg arguments)))))))
227
228 (defun funcall (function &rest arguments)
229   #!+sb-doc
230   "Call FUNCTION with the given ARGUMENTS."
231   (apply function arguments))
232
233 (defun values (&rest values)
234   #!+sb-doc
235   "Return all arguments, in order, as values."
236   (values-list values))
237
238 (defun values-list (list)
239   #!+sb-doc
240   "Return all of the elements of LIST, in order, as values."
241   (values-list list))