3 ;;;; This software is part of the SBCL system. See the README file for
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.
12 (in-package "SB!IMPL")
14 ;;; general case of EVAL (except in that it can't handle toplevel
15 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
17 (funcall (compile (gensym "EVAL-TMPFUN-")
20 ;; The user can reasonably expect that the
21 ;; interpreter will be safe.
22 (declare (optimize (safety 3)))
24 ;; It's also good if the interpreter doesn't
25 ;; spend too long thinking about each input
26 ;; form, since if the user'd wanted the
27 ;; tradeoff to favor quality of compiled code
28 ;; over compilation speed, he'd've explicitly
29 ;; asked for compilation.
30 (declare (optimize (compilation-speed 2)))
32 ;; Other properties are relatively unimportant.
33 (declare (optimize (speed 1) (debug 1) (space 1)))
37 ;;; Handle PROGN and implicit PROGN.
38 (defun eval-progn-body (progn-body)
39 (unless (list-with-length-p progn-body)
40 (let ((*print-circle* t))
41 (error 'simple-program-error
43 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
44 :format-arguments (list progn-body))))
46 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
47 ;; need to take care to return all the values of the final EVAL.
48 ;; * It's left as an exercise to the reader to verify that this
49 ;; gives the right result when PROGN-BODY is NIL, because
50 ;; (FIRST NIL) = (REST NIL) = NIL.
51 (do* ((i progn-body rest-i)
52 (rest-i (rest i) (rest i)))
54 (if rest-i ; if not last element of list
56 (return (eval (first i))))))
58 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
59 ;;; magical cases, and call %EVAL for the rest.
60 (defun eval (original-exp)
62 "Evaluate the argument in a null lexical environment, returning the
64 (declare (optimize (safety 1)))
65 (let ((exp (macroexpand original-exp)))
68 (ecase (info :variable :kind exp)
70 (values (info :variable :constant-value exp)))
73 ;; FIXME: This special case here is a symptom of non-ANSI
74 ;; weirdness in SBCL's ALIEN implementation, which could
75 ;; cause problems for e.g. code walkers. It'd probably be
76 ;; good to ANSIfy it by making alien variable accessors into
77 ;; ordinary forms, e.g. (SB-UNIX:ENV) and (SETF SB-UNIX:ENV),
78 ;; instead of magical symbols, e.g. plain SB-UNIX:ENV. Then
79 ;; if the old magical-symbol syntax is to be retained for
80 ;; compatibility, it can be implemented with
81 ;; DEFINE-SYMBOL-MACRO, keeping the code walkers happy.
83 (%eval original-exp))))
85 (let ((name (first exp))
86 (n-args (1- (length exp))))
90 (error "wrong number of args to FUNCTION:~% ~S" exp))
91 (let ((name (second exp)))
94 (eq (car name) 'setf)))
96 (%eval original-exp))))
99 (error "wrong number of args to QUOTE:~% ~S" exp))
102 (unless (evenp n-args)
103 (error "odd number of args to SETQ:~% ~S" exp))
104 (unless (zerop n-args)
105 (do ((name (cdr exp) (cddr name)))
107 (do ((args (cdr exp) (cddr args)))
109 ;; We duplicate the call to SET so that the
110 ;; correct value gets returned.
111 (set (first args) (eval (second args))))
112 (set (first args) (eval (second args)))))
113 (let ((symbol (first name)))
114 (case (info :variable :kind symbol)
115 ;; FIXME: I took out the *TOPLEVEL-AUTO-DECLARE*
116 ;; test here, and removed the *TOPLEVEL-AUTO-DECLARE*
117 ;; variable; the code should now act as though that
118 ;; variable is NIL. This should be tested..
120 (t (return (%eval original-exp))))))))
122 (eval-progn-body (rest exp)))
124 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
125 ;; instead of PROGRAM-ERROR when there's something wrong
126 ;; with the syntax here (e.g. missing SITUATIONS). This
127 ;; could be fixed by hand-crafting clauses to catch and
128 ;; report each possibility, but it would probably be
129 ;; cleaner to write a new macro
130 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
131 ;; DESTRUCTURING-BIND and promotes any mismatch to
132 ;; PROGRAM-ERROR, then to use it here and in (probably
133 ;; dozens of) other places where the same problem arises.
134 (destructuring-bind (eval-when situations &rest body) exp
135 (declare (ignore eval-when))
136 (multiple-value-bind (ct lt e)
137 (sb!c:parse-eval-when-situations situations)
138 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
139 ;; the situation :EXECUTE (or EVAL) controls whether
140 ;; evaluation occurs for other EVAL-WHEN forms; that
141 ;; is, those that are not top level forms, or those in
142 ;; code processed by EVAL or COMPILE. If the :EXECUTE
143 ;; situation is specified in such a form, then the
144 ;; body forms are processed as an implicit PROGN;
145 ;; otherwise, the EVAL-WHEN form returns NIL.
146 (declare (ignore ct lt))
148 (eval-progn-body body)))))
150 (if (and (symbolp name)
151 (eq (info :function :kind name) :function))
153 (dolist (arg (rest exp))
155 (apply (symbol-function name) (args)))
156 (%eval original-exp))))))
160 ;;; miscellaneous full function definitions of things which are
161 ;;; ordinarily handled magically by the compiler
163 (defun apply (function arg &rest arguments)
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
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)))
177 (apply function (cons arg arguments)))))))
179 (defun funcall (function &rest arguments)
181 "Call FUNCTION with the given ARGUMENTS."
182 (apply function arguments))
184 (defun values (&rest values)
186 "Return all arguments, in order, as values."
187 (values-list values))
189 (defun values-list (list)
191 "Return all of the elements of LIST, in order, as values."