1 ;;; compiler-codege.lisp --- Naive Javascript unparser
3 ;; copyright (C) 2013 David Vazquez
5 ;; JSCL is free software: you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation, either version 3 of the
8 ;; License, or (at your option) any later version.
10 ;; JSCL is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
18 ;;; This code generator takes as input a S-expression representation
19 ;;; of the Javascript AST and generates Javascript code without
20 ;;; redundant syntax constructions like extra parenthesis.
22 ;;; It is intended to be used with the new compiler. However, it is
23 ;;; quite independent so it has been integrated early in JSCL.
25 (defvar *js-output* t)
27 ;;; Two seperate functions are needed for escaping strings:
28 ;;; One for producing JavaScript string literals (which are singly or
30 ;;; And one for producing Lisp strings (which are only doubly quoted)
32 ;;; The same function would suffice for both, but for javascript string
33 ;;; literals it is neater to use either depending on the context, e.g:
36 ;;; which avoids having to escape quotes where possible
37 (defun js-escape-string (string)
39 (size (length string))
40 (seen-single-quote nil)
41 (seen-double-quote nil))
42 (flet ((%js-escape-string (string escape-single-quote-p)
46 (let ((ch (char string index)))
48 (setq output (concat output "\\")))
49 (when (and escape-single-quote-p (char= ch #\'))
50 (setq output (concat output "\\")))
51 (when (char= ch #\newline)
52 (setq output (concat output "\\"))
54 (setq output (concat output (string ch))))
57 ;; First, scan the string for single/double quotes
59 (let ((ch (char string index)))
61 (setq seen-single-quote t))
63 (setq seen-double-quote t)))
65 ;; Then pick the appropriate way to escape the quotes
67 ((not seen-single-quote)
68 (concat "'" (%js-escape-string string nil) "'"))
69 ((not seen-double-quote)
70 (concat "\"" (%js-escape-string string nil) "\""))
71 (t (concat "'" (%js-escape-string string t) "'"))))))
74 (defun js-format (fmt &rest args)
75 (apply #'format *js-output* fmt args))
77 (defun valid-js-identifier (string-designator)
78 (let ((string (typecase string-designator
79 (symbol (string-downcase (symbol-name string-designator)))
80 (string string-designator)
82 (return-from valid-js-identifier (values nil nil))))))
83 (flet ((constitutentp (ch)
84 (or (alphanumericp ch) (member ch '(#\$ #\_)))))
85 (if (and (every #'constitutentp string)
86 (if (plusp (length string))
87 (not (digit-char-p (char string 0)))
89 (values (format nil "~a" string) t)
92 (defun js-identifier (string-designator)
93 (multiple-value-bind (string valid)
94 (valid-js-identifier string-designator)
96 (error "~S is not a valid Javascript identifier." string))
97 (js-format "~a" string)))
99 (defun js-primary-expr (form)
103 (js-format "~a" form)
104 (js-expr `(- ,(abs form)))))
106 (js-format "~a" (js-escape-string form)))
109 (true (js-format "true"))
110 (false (js-format "false"))
111 (null (js-format "null"))
112 (this (js-format "this"))
114 (js-identifier form))))
116 (error "Unknown Javascript syntax ~S." form))))
118 (defun js-vector-initializer (vector)
119 (let ((size (length vector)))
121 (dotimes (i (1- size))
122 (let ((elt (aref vector i)))
123 (unless (eq elt 'null)
127 (js-expr (aref vector (1- size))))
130 (defun js-object-initializer (plist)
132 (do* ((tail plist (cddr tail)))
134 (let ((key (car tail))
136 (multiple-value-bind (identifier identifier-p) (valid-js-identifier key)
137 (declare (ignore identifier))
140 (js-expr (string key))))
143 (unless (null (cddr tail))
147 (defun js-function (arguments &rest body)
148 (js-format "function(")
150 (js-identifier (car arguments))
151 (dolist (arg (cdr arguments))
153 (js-identifier arg)))
155 (js-stmt `(group ,@body)))
157 (defun check-lvalue (x)
158 (unless (or (symbolp x)
159 (nth-value 1 (valid-js-identifier x))
161 (member (car x) '(get =))))
162 (error "Bad Javascript lvalue ~S" x)))
164 ;;; Process the Javascript AST to reduce some syntax sugar.
165 (defun js-expand-expr (form)
169 (case (length (cdr form))
170 (1 `(unary+ ,(cadr form)))
171 (t (reduce (lambda (x y) `(+ ,x ,y)) (cdr form)))))
173 (case (length (cdr form))
174 (1 `(unary- ,(cadr form)))
175 (t (reduce (lambda (x y) `(- ,x ,y)) (cdr form)))))
177 (reduce (lambda (x y) `(comma ,x ,y)) (cdr form) :from-end t))
181 ;; Initialized to any value larger than any operator precedence
182 (defvar *js-operator-precedence* 1000)
183 (defvar *js-operator-associativity* 'left)
184 (defvar *js-operand-order* 'left)
186 ;; Format an expression optionally wrapped with parenthesis if the
187 ;; precedence rules require it.
188 (defmacro with-operator ((precedence associativity) &body body)
189 (let ((g!parens (gensym))
190 (g!precedence (gensym)))
191 `(let* ((,g!precedence ,precedence)
194 ((> ,g!precedence *js-operator-precedence*))
195 ((< ,g!precedence *js-operator-precedence*) nil)
196 ;; Same precedence. Let us consider associativity.
198 (not (eq *js-operand-order* *js-operator-associativity*)))))
199 (*js-operator-precedence* ,g!precedence)
200 (*js-operator-associativity* ,associativity)
201 (*js-operand-order* 'left))
202 (when ,g!parens (js-format "("))
204 (when ,g!parens (js-format ")")))))
206 (defun js-operator (string)
207 (js-format "~a" string)
208 (setq *js-operand-order* 'right))
210 (defun js-operator-expression (op args)
211 (let ((op1 (car args))
219 (with-operator (13 'left)
220 (js-expr (cadr args))
221 (dolist (operand (cddr args))
222 (let ((*js-output* t))
224 (js-expr operand)))))
228 (multiple-value-bind (identifier identifierp)
229 (valid-js-identifier (car args))
230 (multiple-value-bind (accessor accessorp)
231 (valid-js-identifier (cadr args))
233 ((and identifierp accessorp)
234 (js-identifier identifier)
236 (js-identifier accessor))
240 (js-expr (cadr args))
244 (js-object-initializer args))
245 ;; Function expressions
248 (apply #'js-function args)
251 (flet ((%unary-op (operator string precedence associativity post lvalue)
252 (when (eq op operator)
253 (with-operator (precedence associativity)
254 (when lvalue (check-lvalue op1))
258 (js-operator string))
262 (return-from js-operator-expression)))
263 (%binary-op (operator string precedence associativity lvalue)
264 (when (eq op operator)
265 (when lvalue (check-lvalue op1))
266 (with-operator (precedence associativity)
270 (return-from js-operator-expression))))
272 (macrolet ((unary-op (operator string precedence associativity &key post lvalue)
273 `(%unary-op ',operator ',string ',precedence ',associativity ',post ',lvalue))
274 (binary-op (operator string precedence associativity &key lvalue)
275 `(%binary-op ',operator ',string ',precedence ',associativity ',lvalue)))
277 (unary-op pre++ "++" 1 right :lvalue t)
278 (unary-op pre-- "--" 1 right :lvalue t)
279 (unary-op post++ "++" 1 right :lvalue t :post t)
280 (unary-op post-- "--" 1 right :lvalue t :post t)
281 (unary-op not "!" 1 right)
282 (unary-op bit-not "~" 1 right)
283 ;; Note that the leading space is necessary because it
284 ;; could break with post++, for example. TODO: Avoid
285 ;; leading space when it's possible.
286 (unary-op unary+ " +" 1 right)
287 (unary-op unary- " -" 1 right)
288 (unary-op delete "delete " 1 right)
289 (unary-op void "void " 1 right)
290 (unary-op typeof "typeof " 1 right)
291 (unary-op new "new " 1 right)
293 (binary-op * "*" 2 left)
294 (binary-op / "/" 2 left)
295 (binary-op mod "%" 2 left)
296 (binary-op % "%" 2 left)
297 (binary-op + "+" 3 left)
298 (binary-op - "-" 3 left)
299 (binary-op << "<<" 4 left)
300 (binary-op >> "<<" 4 left)
301 (binary-op >>> ">>>" 4 left)
302 (binary-op <= "<=" 5 left)
303 (binary-op < "<" 5 left)
304 (binary-op > ">" 5 left)
305 (binary-op >= ">=" 5 left)
306 (binary-op instanceof " instanceof " 5 left)
307 (binary-op in " in " 5 left)
308 (binary-op == "==" 6 left)
309 (binary-op != "!=" 6 left)
310 (binary-op === "===" 6 left)
311 (binary-op !== "!==" 6 left)
312 (binary-op bit-and "&" 7 left)
313 (binary-op bit-xor "^" 8 left)
314 (binary-op bit-or "|" 9 left)
315 (binary-op and "&&" 10 left)
316 (binary-op or "||" 11 left)
317 (binary-op = "=" 13 right :lvalue t)
318 (binary-op += "+=" 13 right :lvalue t)
319 (binary-op incf "+=" 13 right :lvalue t)
320 (binary-op -= "-=" 13 right :lvalue t)
321 (binary-op decf "-=" 13 right :lvalue t)
322 (binary-op *= "*=" 13 right :lvalue t)
323 (binary-op /= "*=" 13 right :lvalue t)
324 (binary-op bit-xor= "^=" 13 right :lvalue t)
325 (binary-op bit-and= "&=" 13 right :lvalue t)
326 (binary-op bit-or= "|=" 13 right :lvalue t)
327 (binary-op <<= "<<=" 13 right :lvalue t)
328 (binary-op >>= ">>=" 13 right :lvalue t)
329 (binary-op >>>= ">>>=" 13 right :lvalue t)
331 (binary-op comma "," 13 right)
332 (binary-op progn "," 13 right)
334 (when (member op '(? if))
335 (with-operator (12 'right)
336 (js-expr (first args))
338 (js-expr (second args))
340 (js-expr (third args)))
341 (return-from js-operator-expression))
343 (error "Unknown operator `~S'" op)))))))
345 (defun js-expr (form)
346 (let ((form (js-expand-expr form)))
348 ((or (symbolp form) (numberp form) (stringp form))
349 (js-primary-expr form))
351 (js-vector-initializer form))
353 (js-operator-expression (car form) (cdr form))))))
355 (defun js-stmt (form)
362 (destructuring-bind (label &body body) (cdr form)
363 (js-identifier label)
365 (js-stmt `(progn ,@body))))
367 (destructuring-bind (label) (cdr form)
369 (js-identifier label)
372 (destructuring-bind (value) (cdr form)
373 (js-format "return ")
377 (flet ((js-var (spec)
378 (destructuring-bind (variable &optional initial)
380 (js-identifier variable)
383 (js-expr initial)))))
384 (destructuring-bind (var &rest vars) (cdr form)
385 (let ((*js-operator-precedence* 12))
393 (destructuring-bind (condition true &optional false) (cdr form)
403 (mapc #'js-stmt (cdr form))
410 (js-stmt (cadr form)))
412 (js-stmt `(group ,@(cdr form))))))
414 (destructuring-bind (condition &body body) (cdr form)
415 (js-format "while (")
418 (js-stmt `(group ,@body))))
423 (defun js (&rest stmts)
424 (mapc #'js-stmt stmts)