From 67813c3c34958f6ab1fe1beb83266b8b87d01d9c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20V=C3=A1zquez?= Date: Wed, 19 Jun 2013 06:29:37 +0200 Subject: [PATCH] Consider associativity when unparse Javascript expressions --- experimental/codegen.lisp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/experimental/codegen.lisp b/experimental/codegen.lisp index b03fdc9..a54e479 100644 --- a/experimental/codegen.lisp +++ b/experimental/codegen.lisp @@ -169,12 +169,15 @@ (case (length (cdr form)) (1 `(unary- ,(cadr form))) (t (reduce (lambda (x y) `(- ,x ,y)) (cdr form))))) + ((progn comma) + (reduce (lambda (x y) `(comma ,x ,y)) (cdr form) :from-end t)) (t form)) form)) ;; Initialized to any value larger than any operator precedence (defvar *js-operator-precedence* 1000) (defvar *js-operator-associativity* 'left) +(defvar *js-operand-order* 'left) ;; Format an expression optionally wrapped with parenthesis if the ;; precedence rules require it. @@ -186,26 +189,24 @@ (cond ((> ,g!precedence *js-operator-precedence*)) ((< ,g!precedence *js-operator-precedence*) nil) + ;; Same precedence. Let us consider associativity. (t - t))) + (not (eq *js-operand-order* *js-operator-associativity*))))) (*js-operator-precedence* ,g!precedence) - (*js-operator-associativity* ,associativity)) + (*js-operator-associativity* ,associativity) + (*js-operand-order* 'left)) (when ,g!parens (js-format "(")) (progn ,@body) (when ,g!parens (js-format ")"))))) +(defun js-operator (string) + (js-format "~a" string) + (setq *js-operand-order* 'right)) + (defun js-operator-expression (op args) (let ((op1 (car args)) (op2 (cadr args))) (case op - ;; Comma (,) - ((progn comma) - (with-operator (14 'left) - (js-expr (car args)) - (dolist (operand (cdr args)) - (let ((*js-output* t)) - (js-format ",") - (js-expr operand))))) ;; Function call (call (js-expr (car args)) @@ -250,9 +251,9 @@ (cond (post (js-expr op1) - (js-format string)) + (js-operator string)) (t - (js-format string) + (js-operator string) (js-expr op1)))) (return-from js-operator-expression))) (%binary-op (operator string precedence associativity lvalue) @@ -260,7 +261,7 @@ (when lvalue (check-lvalue op1)) (with-operator (precedence associativity) (js-expr op1) - (js-format string) + (js-operator string) (js-expr op2)) (return-from js-operator-expression)))) @@ -319,15 +320,17 @@ (binary-op >>= ">>=" 13 right :lvalue t) (binary-op >>>= ">>>=" 13 right :lvalue t) + (binary-op comma "," 13 right) + (binary-op progn "," 13 right) + (when (member op '(? if)) (with-operator (12 'right) (js-expr (first args)) - (js-format "?") + (js-operator "?") (js-expr (second args)) (js-format ":") (js-expr (third args)))))))))) - (defun js-expr (form) (let ((form (js-expand-expr form))) (cond @@ -338,7 +341,6 @@ (t (js-operator-expression (car form) (cdr form)))))) - (defun js-stmt (form) (if (atom form) (progn -- 1.7.10.4