1 ;;; Naive Javascript unparser
3 ;;; This code generator takes as input a S-expression representation
4 ;;; of the Javascript AST and generates Javascript code without
5 ;;; redundant syntax constructions like extra parenthesis.
7 ;;; It is intended to be used with the new compiler. However, it is
8 ;;; quite independent so it has been integrated early in JSCL.
10 (defun ensure-list (x)
15 (defun concat (&rest strs)
16 (apply #'concatenate 'string strs))
18 (defmacro while (condition &body body)
23 (defvar *js-output* t)
25 ;;; Two seperate functions are needed for escaping strings:
26 ;;; One for producing JavaScript string literals (which are singly or
28 ;;; And one for producing Lisp strings (which are only doubly quoted)
30 ;;; The same function would suffice for both, but for javascript string
31 ;;; literals it is neater to use either depending on the context, e.g:
34 ;;; which avoids having to escape quotes where possible
35 (defun js-escape-string (string)
37 (size (length string))
38 (seen-single-quote nil)
39 (seen-double-quote nil))
40 (flet ((%js-escape-string (string escape-single-quote-p)
44 (let ((ch (char string index)))
46 (setq output (concat output "\\")))
47 (when (and escape-single-quote-p (char= ch #\'))
48 (setq output (concat output "\\")))
49 (when (char= ch #\newline)
50 (setq output (concat output "\\"))
52 (setq output (concat output (string ch))))
55 ;; First, scan the string for single/double quotes
57 (let ((ch (char string index)))
59 (setq seen-single-quote t))
61 (setq seen-double-quote t)))
63 ;; Then pick the appropriate way to escape the quotes
65 ((not seen-single-quote)
66 (concat "'" (%js-escape-string string nil) "'"))
67 ((not seen-double-quote)
68 (concat "\"" (%js-escape-string string nil) "\""))
69 (t (concat "'" (%js-escape-string string t) "'"))))))
72 (defun js-format (fmt &rest args)
73 (apply #'format *js-output* fmt args))
75 (defun valid-js-identifier (string-designator)
76 (let ((string (typecase string-designator
77 (symbol (string-downcase (symbol-name string-designator)))
78 (string string-designator)
80 (return-from valid-js-identifier (values nil nil))))))
81 (flet ((constitutentp (ch)
82 (or (alphanumericp ch) (member ch '(#\$ #\_)))))
83 (if (and (every #'constitutentp string)
84 (if (plusp (length string))
85 (not (digit-char-p (char string 0)))
87 (values (format nil "~a" string) t)
90 (defun js-identifier (string-designator)
91 (multiple-value-bind (string valid)
92 (valid-js-identifier string-designator)
94 (error "~S is not a valid Javascript identifier." string))
95 (js-format "~a" string)))
97 (defun js-primary-expr (form)
101 (js-format "~a" form)
102 (js-expr `(- ,(abs form)))))
104 (js-format "~a" (js-escape-string form)))
107 (true (js-format "true"))
108 (false (js-format "false"))
109 (null (js-format "null"))
110 (this (js-format "this"))
112 (js-identifier form))))
114 (error "Unknown Javascript syntax ~S." form))))
116 (defun js-vector-initializer (vector)
117 (let ((size (length vector)))
119 (dotimes (i (1- size))
120 (let ((elt (aref vector i)))
121 (unless (eq elt 'null)
125 (js-expr (aref vector (1- size))))
128 (defun js-object-initializer (plist)
130 (do* ((tail plist (cddr tail)))
132 (let ((key (car tail))
134 (multiple-value-bind (identifier identifier-p) (valid-js-identifier key)
135 (declare (ignore identifier))
138 (js-expr (string key))))
141 (unless (null (cddr tail))
145 (defun js-function (arguments &rest body)
146 (js-format "function(")
148 (js-identifier (car arguments))
149 (dolist (arg (cdr arguments))
151 (js-identifier arg)))
153 (js-stmt `(group ,@body)))
155 (defun check-lvalue (x)
156 (unless (or (symbolp x)
157 (nth-value 1 (valid-js-identifier x))
159 (member (car x) '(get =))))
160 (error "Bad Javascript lvalue ~S" x)))
162 ;;; Process the Javascript AST to reduce some syntax sugar.
163 (defun js-expand-expr (form)
167 (case (length (cdr form))
168 (1 `(unary+ ,(cadr form)))
169 (t (reduce (lambda (x y) `(+ ,x ,y)) (cdr form)))))
171 (case (length (cdr form))
172 (1 `(unary- ,(cadr form)))
173 (t (reduce (lambda (x y) `(- ,x ,y)) (cdr form)))))
175 (reduce (lambda (x y) `(comma ,x ,y)) (cdr form) :from-end t))
179 ;; Initialized to any value larger than any operator precedence
180 (defvar *js-operator-precedence* 1000)
181 (defvar *js-operator-associativity* 'left)
182 (defvar *js-operand-order* 'left)
184 ;; Format an expression optionally wrapped with parenthesis if the
185 ;; precedence rules require it.
186 (defmacro with-operator ((precedence associativity) &body body)
187 (let ((g!parens (gensym))
188 (g!precedence (gensym)))
189 `(let* ((,g!precedence ,precedence)
192 ((> ,g!precedence *js-operator-precedence*))
193 ((< ,g!precedence *js-operator-precedence*) nil)
194 ;; Same precedence. Let us consider associativity.
196 (not (eq *js-operand-order* *js-operator-associativity*)))))
197 (*js-operator-precedence* ,g!precedence)
198 (*js-operator-associativity* ,associativity)
199 (*js-operand-order* 'left))
200 (when ,g!parens (js-format "("))
202 (when ,g!parens (js-format ")")))))
204 (defun js-operator (string)
205 (js-format "~a" string)
206 (setq *js-operand-order* 'right))
208 (defun js-operator-expression (op args)
209 (let ((op1 (car args))
217 (with-operator (13 'left)
218 (js-expr (cadr args))
219 (dolist (operand (cddr args))
220 (let ((*js-output* t))
222 (js-expr operand)))))
226 (multiple-value-bind (identifier identifierp)
227 (valid-js-identifier (car args))
228 (multiple-value-bind (accessor accessorp)
229 (valid-js-identifier (cadr args))
231 ((and identifierp accessorp)
232 (js-identifier identifier)
234 (js-identifier accessor))
238 (js-expr (cadr args))
242 (js-object-initializer args))
243 ;; Function expressions
246 (apply #'js-function args)
249 (flet ((%unary-op (operator string precedence associativity post lvalue)
250 (when (eq op operator)
251 (with-operator (precedence associativity)
252 (when lvalue (check-lvalue op1))
256 (js-operator string))
260 (return-from js-operator-expression)))
261 (%binary-op (operator string precedence associativity lvalue)
262 (when (eq op operator)
263 (when lvalue (check-lvalue op1))
264 (with-operator (precedence associativity)
268 (return-from js-operator-expression))))
270 (macrolet ((unary-op (operator string precedence associativity &key post lvalue)
271 `(%unary-op ',operator ',string ',precedence ',associativity ',post ',lvalue))
272 (binary-op (operator string precedence associativity &key lvalue)
273 `(%binary-op ',operator ',string ',precedence ',associativity ',lvalue)))
275 (unary-op pre++ "++" 1 right :lvalue t)
276 (unary-op pre-- "--" 1 right :lvalue t)
277 (unary-op post++ "++" 1 right :lvalue t :post t)
278 (unary-op post-- "--" 1 right :lvalue t :post t)
279 (unary-op not "!" 1 right)
280 (unary-op bit-not "~" 1 right)
281 ;; Note that the leading space is necessary because it
282 ;; could break with post++, for example. TODO: Avoid
283 ;; leading space when it's possible.
284 (unary-op unary+ " +" 1 right)
285 (unary-op unary- " -" 1 right)
286 (unary-op delete "delete " 1 right)
287 (unary-op void "void " 1 right)
288 (unary-op typeof "typeof " 1 right)
289 (unary-op new "new " 1 right)
291 (binary-op * "*" 2 left)
292 (binary-op / "/" 2 left)
293 (binary-op mod "%" 2 left)
294 (binary-op % "%" 2 left)
295 (binary-op + "+" 3 left)
296 (binary-op - "-" 3 left)
297 (binary-op << "<<" 4 left)
298 (binary-op >> "<<" 4 left)
299 (binary-op >>> ">>>" 4 left)
300 (binary-op <= "<=" 5 left)
301 (binary-op < "<" 5 left)
302 (binary-op > ">" 5 left)
303 (binary-op >= ">=" 5 left)
304 (binary-op instanceof " instanceof " 5 left)
305 (binary-op in " in " 5 left)
306 (binary-op == "==" 6 left)
307 (binary-op != "!=" 6 left)
308 (binary-op === "===" 6 left)
309 (binary-op !== "!==" 6 left)
310 (binary-op bit-and "&" 7 left)
311 (binary-op bit-xor "^" 8 left)
312 (binary-op bit-or "|" 9 left)
313 (binary-op and "&&" 10 left)
314 (binary-op or "||" 11 left)
315 (binary-op = "=" 13 right :lvalue t)
316 (binary-op += "+=" 13 right :lvalue t)
317 (binary-op incf "+=" 13 right :lvalue t)
318 (binary-op -= "-=" 13 right :lvalue t)
319 (binary-op decf "-=" 13 right :lvalue t)
320 (binary-op *= "*=" 13 right :lvalue t)
321 (binary-op /= "*=" 13 right :lvalue t)
322 (binary-op bit-xor= "^=" 13 right :lvalue t)
323 (binary-op bit-and= "&=" 13 right :lvalue t)
324 (binary-op bit-or= "|=" 13 right :lvalue t)
325 (binary-op <<= "<<=" 13 right :lvalue t)
326 (binary-op >>= ">>=" 13 right :lvalue t)
327 (binary-op >>>= ">>>=" 13 right :lvalue t)
329 (binary-op comma "," 13 right)
330 (binary-op progn "," 13 right)
332 (when (member op '(? if))
333 (with-operator (12 'right)
334 (js-expr (first args))
336 (js-expr (second args))
338 (js-expr (third args)))
339 (return-from js-operator-expression))
341 (error "Unknown operator `~S'" op)))))))
343 (defun js-expr (form)
344 (let ((form (js-expand-expr form)))
346 ((or (symbolp form) (numberp form) (stringp form))
347 (js-primary-expr form))
349 (js-vector-initializer form))
351 (js-operator-expression (car form) (cdr form))))))
353 (defun js-stmt (form)
360 (destructuring-bind (label &body body) (cdr form)
361 (js-identifier label)
363 (js-stmt `(progn ,@body))))
365 (destructuring-bind (label) (cdr form)
367 (js-identifier label)
370 (destructuring-bind (value) (cdr form)
371 (js-format "return ")
375 (flet ((js-var (spec)
376 (destructuring-bind (variable &optional initial)
378 (js-identifier variable)
381 (js-expr initial)))))
382 (destructuring-bind (var &rest vars) (cdr form)
383 (let ((*js-operator-precedence* 12))
391 (destructuring-bind (condition true &optional false) (cdr form)
401 (mapc #'js-stmt (cdr form))
408 (js-stmt (cadr form)))
410 (js-stmt `(group ,@(cdr form))))))
412 (destructuring-bind (condition &body body) (cdr form)
413 (js-format "while (")
416 (js-stmt `(group ,@body))))
421 (defun js (&rest stmts)
422 (mapc #'js-stmt stmts)