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