abd46dd0f6a95cf8d511b080a0f2049fd86cbc94
[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
21                ;; The user can reasonably expect that the
22                ;; interpreter will be safe.
23                (declare (optimize (safety 3)))
24
25                ;; It's also good if the interpreter doesn't
26                ;; spend too long thinking about each input
27                ;; form, since if the user'd wanted the
28                ;; tradeoff to favor quality of compiled code
29                ;; over compilation speed, he'd've explicitly
30                ;; asked for compilation.
31                (declare (optimize (compilation-speed 2)))
32
33                ;; Other properties are relatively unimportant.
34                (declare (optimize (speed 1) (debug 1) (space 1)))
35
36                ,expr)
37             lexenv)))
38
39 ;;; Handle PROGN and implicit PROGN.
40 (defun eval-progn-body (progn-body lexenv)
41   (unless (list-with-length-p progn-body)
42     (let ((*print-circle* t))
43       (error 'simple-program-error
44              :format-control
45              "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
46              :format-arguments (list progn-body))))
47   ;; Note:
48   ;;   * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
49   ;;     need to take care to return all the values of the final EVAL.
50   ;;   * It's left as an exercise to the reader to verify that this
51   ;;     gives the right result when PROGN-BODY is NIL, because
52   ;;     (FIRST NIL) = (REST NIL) = NIL.
53   (do* ((i progn-body rest-i)
54         (rest-i (rest i) (rest i)))
55       (nil)
56     (if rest-i ; if not last element of list
57         (eval-in-lexenv (first i) lexenv)
58         (return (eval-in-lexenv (first i) lexenv)))))
59
60 (defun eval (original-exp)
61   #!+sb-doc
62   "Evaluate the argument in a null lexical environment, returning the
63   result or results."
64   (eval-in-lexenv original-exp (make-null-lexenv)))
65
66 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
67 ;;; magical cases, and call %EVAL for the rest.
68 (defun eval-in-lexenv (original-exp lexenv)
69   (declare (optimize (safety 1)))
70   ;; (aver (lexenv-simple-p lexenv))
71   (let ((exp (macroexpand original-exp lexenv)))
72     (typecase exp
73       (symbol
74        (ecase (info :variable :kind exp)
75          (:constant
76           (values (info :variable :constant-value exp)))
77          ((:special :global)
78           (symbol-value exp))
79          ;; FIXME: This special case here is a symptom of non-ANSI
80          ;; weirdness in SBCL's ALIEN implementation, which could
81          ;; cause problems for e.g. code walkers. It'd probably be
82          ;; good to ANSIfy it by making alien variable accessors into
83          ;; ordinary forms, e.g. (SB-UNIX:ENV) and (SETF SB-UNIX:ENV),
84          ;; instead of magical symbols, e.g. plain SB-UNIX:ENV. Then
85          ;; if the old magical-symbol syntax is to be retained for
86          ;; compatibility, it can be implemented with
87          ;; DEFINE-SYMBOL-MACRO, keeping the code walkers happy.
88          (:alien
89           (%eval original-exp lexenv))))
90       (list
91        (let ((name (first exp))
92              (n-args (1- (length exp))))
93          (case name
94            (function
95             (unless (= n-args 1)
96               (error "wrong number of args to FUNCTION:~% ~S" exp))
97             (let ((name (second exp)))
98               (if (and (or (atom name)
99                            (and (consp name)
100                                 (eq (car name) 'setf)))
101                        (not (consp (let ((sb!c:*lexenv* lexenv))
102                                      (sb!c:lexenv-find name funs)))))
103                   (fdefinition name)
104                   (%eval original-exp lexenv))))
105            (quote
106             (unless (= n-args 1)
107               (error "wrong number of args to QUOTE:~% ~S" exp))
108             (second exp))
109            (setq
110             (unless (evenp n-args)
111               (error "odd number of args to SETQ:~% ~S" exp))
112             (unless (zerop n-args)
113               (do ((name (cdr exp) (cddr name)))
114                   ((null name)
115                    (do ((args (cdr exp) (cddr args)))
116                        ((null (cddr args))
117                         ;; We duplicate the call to SET so that the
118                         ;; correct value gets returned.
119                         (set (first args) (eval (second args))))
120                      (set (first args) (eval (second args)))))
121                 (let ((symbol (first name)))
122                   (case (info :variable :kind symbol)
123                     ;; FIXME: I took out the *TOPLEVEL-AUTO-DECLARE*
124                     ;; test here, and removed the *TOPLEVEL-AUTO-DECLARE*
125                     ;; variable; the code should now act as though that
126                     ;; variable is NIL. This should be tested..
127                     (:special)
128                     (t (return (%eval original-exp lexenv))))))))
129            ((progn)
130             (eval-progn-body (rest exp) lexenv))
131            ((eval-when)
132             ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
133             ;; instead of PROGRAM-ERROR when there's something wrong
134             ;; with the syntax here (e.g. missing SITUATIONS). This
135             ;; could be fixed by hand-crafting clauses to catch and
136             ;; report each possibility, but it would probably be
137             ;; cleaner to write a new macro
138             ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
139             ;; DESTRUCTURING-BIND and promotes any mismatch to
140             ;; PROGRAM-ERROR, then to use it here and in (probably
141             ;; dozens of) other places where the same problem arises.
142             (destructuring-bind (eval-when situations &rest body) exp
143               (declare (ignore eval-when))
144               (multiple-value-bind (ct lt e)
145                   (sb!c:parse-eval-when-situations situations)
146                 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
147                 ;; the situation :EXECUTE (or EVAL) controls whether
148                 ;; evaluation occurs for other EVAL-WHEN forms; that
149                 ;; is, those that are not top level forms, or those in
150                 ;; code processed by EVAL or COMPILE. If the :EXECUTE
151                 ;; situation is specified in such a form, then the
152                 ;; body forms are processed as an implicit PROGN;
153                 ;; otherwise, the EVAL-WHEN form returns NIL.
154                 (declare (ignore ct lt))
155                 (when e
156                   (eval-progn-body body lexenv)))))
157            (t
158             (if (and (symbolp name)
159                      (eq (info :function :kind name) :function))
160                 (collect ((args))
161                          (dolist (arg (rest exp))
162                            (args (eval arg)))
163                          (apply (symbol-function name) (args)))
164                 (%eval original-exp lexenv))))))
165       (t
166        exp))))
167 \f
168 ;;; miscellaneous full function definitions of things which are
169 ;;; ordinarily handled magically by the compiler
170
171 (defun apply (function arg &rest arguments)
172   #!+sb-doc
173   "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
174   the manner of LIST*. That is, a list is made of the values of all but the
175   last argument, appended to the value of the last argument, which must be a
176   list."
177   (cond ((atom arguments)
178          (apply function arg))
179         ((atom (cdr arguments))
180          (apply function (cons arg (car arguments))))
181         (t (do* ((a1 arguments a2)
182                  (a2 (cdr arguments) (cdr a2)))
183                 ((atom (cdr a2))
184                  (rplacd a1 (car a2))
185                  (apply function (cons arg arguments)))))))
186
187 (defun funcall (function &rest arguments)
188   #!+sb-doc
189   "Call FUNCTION with the given ARGUMENTS."
190   (apply function arguments))
191
192 (defun values (&rest values)
193   #!+sb-doc
194   "Return all arguments, in order, as values."
195   (values-list values))
196
197 (defun values-list (list)
198   #!+sb-doc
199   "Return all of the elements of LIST, in order, as values."
200   (values-list list))