Non-nested switch cases in codegen
authorDavid Vázquez <davazp@gmail.com>
Fri, 5 Jul 2013 01:27:50 +0000 (03:27 +0200)
committerDavid Vázquez <davazp@gmail.com>
Fri, 5 Jul 2013 01:27:50 +0000 (03:27 +0200)
src/compiler-codegen.lisp
src/compiler.lisp

index 480a856..83cb4b9 100644 (file)
               (js-expr value)
               (js-format "){")
               (dolist (case cases)
-                (destructuring-bind (x &body body) case
-                  (if (eq x 'default)
-                      (js-format "default: ")
-                      (progn
-                        (unless (or (stringp x) (numberp x))
-                          (error "Non-constant switch case `~S'." (car cases)))
-                        (js-format "case ")
-                        (js-expr x)
-                        (js-format ":")))
-                  (mapc #'js-stmt body)))
+                (cond
+                  ((and (consp case) (eq (car case) 'case))
+                   (js-format "case ")
+                   (let ((value (cadr case)))
+                     (unless (or (stringp value) (integerp value))
+                       (error "Non-constant switch case `~S'." value))
+                     (js-expr value))
+                   (js-format ":"))
+                  ((eq case 'default)
+                   (js-format "default:"))
+                  (t
+                   (js-stmt case))))
               (js-format "}")))
            (for
             (destructuring-bind ((start condition step) &body body) (cdr form)
index fd7e1e0..bfa9abb 100644 (file)
                ,@(with-collect
                   (dotimes (idx n-optional-arguments)
                     (let ((arg (nth idx optional-arguments)))
-                      (collect `(,(+ idx n-required-arguments)
-                                  (= ,(make-symbol (translate-variable (car arg)))
-                                     ,(ls-compile (cadr arg)))
-                                  ,(when (third arg)
-                                         `(= ,(make-symbol (translate-variable (third arg)))
-                                             ,(ls-compile nil)))))))
-                  (collect `(default (break))))))))
+                      (collect `(case ,(+ idx n-required-arguments)))
+                      (collect `(= ,(make-symbol (translate-variable (car arg)))
+                                   ,(ls-compile (cadr arg))))
+                      (collect (when (third arg)
+                                 `(= ,(make-symbol (translate-variable (third arg)))
+                                     ,(ls-compile nil))))))
+                  (collect 'default)
+                  (collect '(break)))))))
 
 (defun compile-lambda-rest (ll)
   (let ((n-required-arguments (length (ll-required-arguments ll)))