23af78981e491eb7a0e62be7342ac216176917f4
[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!BYTECODE")
13
14 ;;; This needs to be initialized in the cold load, since the top-level
15 ;;; catcher will always restore the initial value.
16 (defvar *eval-stack-top* 0)
17
18 ;;; general case of EVAL (except in that it can't handle toplevel
19 ;;; EVAL-WHEN magic properly): Delegate to the byte compiler.
20 (defun %eval (expr)
21   (funcall (compile (gensym "EVAL-TMPFUN-")
22                     `(lambda ()
23
24                        ;; SPEED=0,DEBUG=1 => byte-compile
25                        (declare (optimize (speed 0) (debug 1))) 
26
27                        ;; Other than that, basically we care about
28                        ;; compilation speed, compilation speed, and
29                        ;; compilation speed. (There are cases where
30                        ;; the user wants something else, but we don't
31                        ;; know enough to guess that; and if he is
32                        ;; unhappy about our guessed emphasis, he
33                        ;; should explicitly compile his code, with
34                        ;; explicit declarations to tell us what to
35                        ;; emphasize.)
36                        (declare (optimize (space 1) (safety 1)))
37                        (declare (optimize (compilation-speed 3)))
38
39                        ,expr))))
40
41 ;;; Pick off a few easy cases, and the various top-level EVAL-WHEN
42 ;;; magical cases, and call %EVAL for the rest. 
43 (defun eval (original-exp)
44   #!+sb-doc
45   "Evaluate the argument in a null lexical environment, returning the
46   result or results."
47   (declare (optimize (safety 1)))
48   (let ((exp (macroexpand original-exp)))
49     (typecase exp
50       (symbol
51        (ecase (info :variable :kind exp)
52          (:constant
53           (values (info :variable :constant-value exp)))
54          ((:special :global)
55           (symbol-value exp))
56          ;; FIXME: This special case here is a symptom of non-ANSI
57          ;; weirdness in SBCL's ALIEN implementation, which could
58          ;; cause problems for e.g. code walkers. It'd probably be
59          ;; good to ANSIfy it by making alien variable accessors into
60          ;; ordinary forms, e.g. (SB-UNIX:ENV) and (SETF SB-UNIX:ENV),
61          ;; instead of magical symbols, e.g. plain SB-UNIX:ENV. Then
62          ;; if the old magical-symbol syntax is to be retained for
63          ;; compatibility, it can be implemented with
64          ;; DEFINE-SYMBOL-MACRO, keeping the code walkers happy.
65          (:alien
66           (%eval original-exp))))
67       (list
68        (let ((name (first exp))
69              (args (1- (length exp))))
70          (case name
71            (function
72             (unless (= args 1)
73               (error "wrong number of args to FUNCTION:~% ~S" exp))
74             (let ((name (second exp)))
75               (if (or (atom name)
76                       (and (consp name)
77                            (eq (car name) 'setf)))
78                   (fdefinition name)
79                   (%eval original-exp))))
80            (quote
81             (unless (= args 1)
82               (error "wrong number of args to QUOTE:~% ~S" exp))
83             (second exp))
84            (setq
85             (unless (evenp args)
86               (error "odd number of args to SETQ:~% ~S" exp))
87             (unless (zerop args)
88               (do ((name (cdr exp) (cddr name)))
89                   ((null name)
90                    (do ((args (cdr exp) (cddr args)))
91                        ((null (cddr args))
92                         ;; We duplicate the call to SET so that the
93                         ;; correct value gets returned.
94                         (set (first args) (eval (second args))))
95                      (set (first args) (eval (second args)))))
96                 (let ((symbol (first name)))
97                   (case (info :variable :kind symbol)
98                     ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
99                     ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
100                     ;; variable; the code should now act as though that
101                     ;; variable is NIL. This should be tested..
102                     (:special)
103                     (t (return (%eval original-exp))))))))
104            ((progn)
105             (when (> args 0)
106               (dolist (x (butlast (rest exp)) (eval (car (last exp))))
107                 (eval x))))
108            ((eval-when)
109             (if (and (> args 0)
110                      (or (member 'eval (second exp))
111                          (member :execute (second exp))))
112                 (when (> args 1)
113                   (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
114                     (eval x)))
115                 (%eval original-exp)))
116            (t
117             (if (and (symbolp name)
118                      (eq (info :function :kind name) :function))
119                 (collect ((args))
120                   (dolist (arg (rest exp))
121                     (args (eval arg)))
122                   (apply (symbol-function name) (args)))
123                 (%eval original-exp))))))
124       (t
125        exp))))
126
127 ;;; Given a function, return three values:
128 ;;; 1] A lambda expression that could be used to define the function,
129 ;;;    or NIL if the definition isn't available.
130 ;;; 2] NIL if the function was definitely defined in a null lexical
131 ;;;    environment, and T otherwise.
132 ;;; 3] Some object that \"names\" the function. Although this is
133 ;;;    allowed to be any object, CMU CL always returns a valid
134 ;;;    function name or a string.
135 ;;;
136 ;;; If interpreted, use the interpreter interface. Otherwise, see
137 ;;; whether it was compiled with COMPILE. If that fails, check for an
138 ;;; inline expansion.
139 (defun function-lambda-expression (fun)
140   (declare (type function fun))
141   (let* ((fun (%function-self fun))
142          (name (%function-name fun))
143          (code (sb!di::function-code-header fun))
144          (info (sb!kernel:%code-debug-info code)))
145     (if info
146         (let ((source (first (sb!c::compiled-debug-info-source info))))
147           (cond ((and (eq (sb!c::debug-source-from source) :lisp)
148                       (eq (sb!c::debug-source-info source) fun))
149                  (values (second (svref (sb!c::debug-source-name source) 0))
150                          nil name))
151                 ((stringp name)
152                  (values nil t name))
153                 (t
154                  (let ((exp (info :function :inline-expansion name)))
155                    (if exp
156                        (values exp nil name)
157                        (values nil t name))))))
158         (values nil t name))))
159 \f
160 ;;; miscellaneous full function definitions of things which are
161 ;;; ordinarily handled magically by the compiler
162
163 (defun apply (function arg &rest arguments)
164   #!+sb-doc
165   "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
166   the manner of LIST*. That is, a list is made of the values of all but the
167   last argument, appended to the value of the last argument, which must be a
168   list."
169   (cond ((atom arguments)
170          (apply function arg))
171         ((atom (cdr arguments))
172          (apply function (cons arg (car arguments))))
173         (t (do* ((a1 arguments a2)
174                  (a2 (cdr arguments) (cdr a2)))
175                 ((atom (cdr a2))
176                  (rplacd a1 (car a2))
177                  (apply function (cons arg arguments)))))))
178
179 (defun funcall (function &rest arguments)
180   #!+sb-doc
181   "Call FUNCTION with the given ARGUMENTS."
182   (apply function arguments))
183
184 (defun values (&rest values)
185   #!+sb-doc
186   "Return all arguments, in order, as values."
187   (values-list values))
188
189 (defun values-list (list)
190   #!+sb-doc
191   "Return all of the elements of LIST, in order, as values."
192   (values-list list))