Fixed problems with expressions being compiled to statements inside expression progns...
[clinton/parenscript.git] / src / compiler.lisp
1 (in-package :parenscript)
2
3 (eval-when (:compile-toplevel :load-toplevel :execute)
4 (defvar *toplevel-special-forms* (make-hash-table :test #'equal)
5 "A hash-table containing functions that implement Parenscript special forms,
6 indexed by name (as symbols)")
7 (defun undefine-ps-special-form (name)
8 "Undefines the special form with the given name (name is a symbol)."
9 (remhash (lisp-symbol-to-ps-identifier name :special-form) *toplevel-special-forms*)))
10
11 (defmacro define-ps-special-form (name lambda-list &rest body)
12 "Define a special form NAME. The first argument given to the special
13 form is a keyword indicating whether the form is expected to produce
14 an :expression or a :statement. The resulting Parenscript language
15 types are appended to the ongoing javascript compilation."
16 (let ((arglist (gensym "ps-arglist-")))
17 `(setf (gethash (lisp-symbol-to-ps-identifier ',name :special-form) *toplevel-special-forms*)
18 (lambda (&rest ,arglist)
19 (destructuring-bind ,lambda-list
20 ,arglist
21 ,@body)))))
22
23 (defun get-ps-special-form (name)
24 "Returns the special form function corresponding to the given name."
25 (gethash (lisp-symbol-to-ps-identifier name :special-form) *toplevel-special-forms*))
26
27 (defvar *enclosing-lexical-block-declarations* ()
28 "This special variable is expected to be bound to a fresh list by
29 special forms that introduce a new JavaScript lexical block (currently
30 function definitions and lambdas). Enclosed special forms are expected
31 to push variable declarations onto the list when the variables
32 declaration cannot be made by the enclosed form (for example, a
33 (x,y,z) expression progn). It is then the responsibility of the
34 enclosing special form to introduce the variable bindings in its
35 lexical block.")
36
37 ;;; ParenScript form predicates
38 (defun ps-special-form-p (form)
39 (and (consp form)
40 (symbolp (car form))
41 (get-ps-special-form (car form))))
42
43 (defun op-form-p (form)
44 (and (listp form)
45 (not (ps-special-form-p form))
46 (not (null (op-precedence (first form))))))
47
48 (defun funcall-form-p (form)
49 (and (listp form)
50 (not (op-form-p form))
51 (not (ps-special-form-p form))))
52
53 (defun method-call-p (form)
54 (and (funcall-form-p form)
55 (symbolp (first form))
56 (eql (char (symbol-name (first form)) 0) #\.)))
57
58 ;;; macro expansion
59 (eval-when (:compile-toplevel :load-toplevel :execute)
60 (defun make-macro-env-dictionary ()
61 "Creates a standard macro dictionary."
62 (make-hash-table :test #'equal))
63 (defvar *script-macro-toplevel* (make-macro-env-dictionary)
64 "Toplevel macro environment dictionary. Key is the symbol of the
65 macro, value is (symbol-macro-p . expansion-function).")
66 (defvar *script-macro-env* (list *script-macro-toplevel*)
67 "Current macro environment.")
68
69 (defvar *script-setf-expanders* (make-macro-env-dictionary)
70 "Setf expander dictionary. Key is the symbol of the access
71 function of the place, value is an expansion function that takes the
72 arguments of the access functions as a first value and the form to be
73 stored as the second value.")
74
75 (defun get-macro-spec (name env-dict)
76 "Retrieves the macro spec of the given name with the given environment dictionary.
77 SPEC is of the form (symbol-macro-p . expansion-function)."
78 (gethash (lisp-symbol-to-ps-identifier name :macro) env-dict))
79 (defsetf get-macro-spec (name env-dict)
80 (spec)
81 `(setf (gethash (lisp-symbol-to-ps-identifier ,name :macro) ,env-dict) ,spec)))
82
83 (defun lookup-macro-spec (name &optional (environment *script-macro-env*))
84 "Looks up the macro spec associated with NAME in the given environment. A
85 macro spec is of the form (symbol-macro-p . function). Returns two values:
86 the SPEC and the parent macro environment.
87
88 NAME must be a symbol."
89 (when (symbolp name)
90 (do ((env environment (cdr env)))
91 ((null env) nil)
92 (let ((val (get-macro-spec name (car env))))
93 (when val
94 (return-from lookup-macro-spec
95 (values val (or (cdr env)
96 (list *script-macro-toplevel*)))))))))
97
98 (defun script-symbol-macro-p (name &optional (environment *script-macro-env*))
99 "True if there is a Parenscript symbol macro named by the symbol NAME."
100 (and (symbolp name) (car (lookup-macro-spec name environment))))
101
102 (defun script-macro-p (name &optional (environment *script-macro-env*))
103 "True if there is a Parenscript macro named by the symbol NAME."
104 (and (symbolp name)
105 (let ((macro-spec (lookup-macro-spec name environment)))
106 (and macro-spec (not (car macro-spec))))))
107
108 (defun lookup-macro-expansion-function (name &optional (environment *script-macro-env*))
109 "Lookup NAME in the given macro expansion environment (which
110 defaults to the current macro environment). Returns the expansion
111 function and the parent macro environment of the macro."
112 (multiple-value-bind (macro-spec parent-env)
113 (lookup-macro-spec name environment)
114 (values (cdr macro-spec) parent-env)))
115
116 (eval-when (:compile-toplevel :load-toplevel :execute)
117 (defun make-ps-macro-function (args body)
118 (let* ((whole-var (when (eql '&whole (first args)) (second args)))
119 (effective-lambda-list (if whole-var (cddr args) args))
120 (form-arg (or whole-var (gensym "ps-macro-form-arg-")))
121 (body (if (and (cdr body) (stringp (first body))) (rest body) body))) ;; drop docstring
122 (compile nil `(lambda (,form-arg)
123 (destructuring-bind ,effective-lambda-list
124 (cdr ,form-arg)
125 ,@body)))))
126
127 (defun define-script-macro% (name args body &key symbol-macro-p)
128 (undefine-ps-special-form name)
129 (setf (get-macro-spec name *script-macro-toplevel*)
130 (cons symbol-macro-p (make-ps-macro-function args body)))
131 nil))
132
133 (defmacro defpsmacro (name args &body body)
134 "Define a ParenScript macro, and store it in the toplevel ParenScript
135 macro environment."
136 `(define-script-macro% ',name ',args ',body :symbol-macro-p nil))
137
138 (defmacro define-script-symbol-macro (name &body body)
139 "Define a ParenScript symbol macro, and store it in the toplevel ParenScript
140 macro environment. BODY is a Lisp form that should return a ParenScript form."
141 `(define-script-macro% ',name () ',body :symbol-macro-p t))
142
143 (defun import-macros-from-lisp (&rest names)
144 "Import the named Lisp macros into the ParenScript macro
145 environment. When the imported macro is macroexpanded by ParenScript,
146 it is first fully macroexpanded in the Lisp macro environment, and
147 then that expansion is further expanded by ParenScript."
148 (dolist (name names)
149 (define-script-macro% name '(&rest args)
150 (list `(common-lisp:macroexpand `(,',name ,@args)))
151 :symbol-macro-p nil)))
152
153 (defmacro defmacro/ps (name args &body body)
154 "Define a Lisp macro and import it into the ParenScript macro environment."
155 `(progn (defmacro ,name ,args ,@body)
156 (ps:import-macros-from-lisp ',name)))
157
158 (defmacro defmacro+ps (name args &body body)
159 "Define a Lisp macro and a ParenScript macro in their respective
160 macro environments. This function should be used when you want to use
161 the same macro in both Lisp and ParenScript, but the 'macroexpand' of
162 that macro in Lisp makes the Lisp macro unsuitable to be imported into
163 the ParenScript macro environment."
164 `(progn (defmacro ,name ,args ,@body)
165 (defpsmacro ,name ,args ,@body)))
166
167 (defun ps-macroexpand (form)
168 "Recursively macroexpands ParenScript macros and symbol-macros in
169 the given ParenScript form. Returns two values: the expanded form, and
170 whether any expansion was performed on the form or not."
171 (if (consp form)
172 (let ((op (car form))
173 (args (cdr form)))
174 (cond ((equal op 'quote) (values (if (equalp '(nil) args) nil form) ;; leave quotes alone, unless it's a quoted nil
175 nil))
176 ((script-macro-p op) (values (ps-macroexpand (funcall (lookup-macro-expansion-function op) form)) t))
177 (t (values form nil))))
178 (cond ((script-symbol-macro-p form) (values (ps-macroexpand (funcall (lookup-macro-expansion-function form) (list form))) t))
179 (t (values form nil)))))
180
181 ;;;; compiler interface
182 (defgeneric compile-parenscript-form (form &key expecting)
183 (:documentation "Compiles a ParenScript form to the intermediate
184 ParenScript representation. :expecting determines whether the form is
185 compiled to an :expression (the default), a :statement, or a
186 :symbol."))
187
188 (defmethod compile-parenscript-form :around (form &key expecting)
189 (assert (if expecting (member expecting '(:expression :statement :symbol)) t))
190 (if (eql expecting :symbol)
191 (compile-to-symbol form)
192 (multiple-value-bind (expanded-form expanded-p)
193 (ps-macroexpand form)
194 (if expanded-p
195 (compile-parenscript-form expanded-form :expecting expecting)
196 (call-next-method)))))
197
198 (defun compile-to-symbol (form)
199 "Compiles the given Parenscript form and guarantees that the
200 resultant symbol has an associated script-package. Raises an error if
201 the form cannot be compiled to a symbol."
202 (let ((exp (compile-parenscript-form form)))
203 (when (or (eql (first exp) 'js-variable)
204 (eql (first exp) 'script-quote))
205 (setf exp (second exp)))
206 (assert (symbolp exp) ()
207 "~a is expected to be a symbol, but compiles to ~a (the ParenScript output for ~a alone is \"~a\"). This could be due to ~a being a special form." form exp form (ps* form) form)
208 exp))
209
210 (defmethod compile-parenscript-form (form &key expecting)
211 (declare (ignore expecting))
212 (error "The object ~S cannot be compiled by ParenScript." form))
213
214 (defmethod compile-parenscript-form ((form number) &key expecting)
215 (declare (ignore expecting))
216 form)
217
218 (defmethod compile-parenscript-form ((form string) &key expecting)
219 (declare (ignore expecting))
220 form)
221
222 (defmethod compile-parenscript-form ((form character) &key expecting)
223 (declare (ignore expecting))
224 (compile-parenscript-form (string form)))
225
226 (defmethod compile-parenscript-form ((symbol symbol) &key expecting)
227 (declare (ignore expecting))
228 ;; is this the correct behavior?
229 (let ((special-symbol (get-ps-special-form symbol)))
230 (cond (special-symbol (funcall special-symbol :symbol))
231 (t (list 'js-variable symbol)))))
232
233 (defun compile-function-argument-forms (arg-forms)
234 "Compiles a bunch of Parenscript forms from a funcall form to an effective set of
235 Javascript arguments. The only extra processing this does is makes :keyword arguments
236 into a single options argument via CREATE."
237 (flet ((keyword-arg (arg)
238 "If the given compiled expression is supposed to be a keyword argument, returns
239 the keyword for it."
240 (when (and (listp arg) (eql (first arg) 'script-quote)) (second arg))))
241 (let ((compiled-args (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :expression))
242 arg-forms)))
243 (do ((effective-expressions nil)
244 (expressions-subl compiled-args))
245 ((not expressions-subl) (reverse effective-expressions))
246 (let ((arg-expr (first expressions-subl)))
247 (if (keyword-arg arg-expr)
248 (progn (when (oddp (length expressions-subl))
249 (error "Odd number of keyword arguments: ~A." arg-forms))
250 (push (list 'js-object (loop for (name val) on expressions-subl by #'cddr
251 collect (list name val)))
252 effective-expressions)
253 (setf expressions-subl nil))
254 (progn (push arg-expr effective-expressions)
255 (setf expressions-subl (rest expressions-subl)))))))))
256
257 (defmethod compile-parenscript-form ((form cons) &key (expecting :statement))
258 (let* ((name (car form))
259 (args (cdr form)))
260 (cond ((eql name 'quote)
261 (assert (= 1 (length args)) () "Wrong number of arguments to quote: ~s" args)
262 (list 'script-quote (first args)))
263 ((ps-special-form-p form) (apply (get-ps-special-form name) (cons expecting args)))
264 ((op-form-p form)
265 (list 'operator
266 (script-convert-op-name (compile-parenscript-form (first form) :expecting :symbol))
267 (mapcar (lambda (form) (compile-parenscript-form form :expecting :expression)) (rest form))))
268 ((method-call-p form)
269 (list 'js-method-call
270 (compile-parenscript-form name :expecting :symbol)
271 (compile-parenscript-form (first args) :expecting :expression)
272 (compile-function-argument-forms (rest args))))
273 ((funcall-form-p form)
274 (list 'js-funcall
275 (compile-parenscript-form name :expecting :expression)
276 (compile-function-argument-forms args)))
277 (t (error "Cannot compile ~S to a ParenScript form." form)))))
278