SWITCH support
authorDavid Vázquez <davazp@gmail.com>
Sun, 30 Jun 2013 15:23:42 +0000 (17:23 +0200)
committerDavid Vázquez <davazp@gmail.com>
Sun, 30 Jun 2013 15:23:42 +0000 (17:23 +0200)
src/compiler-codegen.lisp

index 4552430..2c9e1e8 100644 (file)
               (js-format ":")
               (js-stmt `(progn ,@body))))
            (break
-            (destructuring-bind (label) (cdr form)
-              (js-format "break ")
-              (js-identifier label)
+            (destructuring-bind (&optional label) (cdr form)
+              (js-format "break")
+              (when label
+                (js-format " ")
+                (js-identifier label))
               (js-format ";")))
            (return
              (destructuring-bind (value) (cdr form)
                  (js-expr condition)
                  (js-format ")")
                  (js-stmt `(progn ,@body))))
+           (switch
+            (destructuring-bind (value &rest cases) (cdr form)
+              (js-format "switch(")
+              (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)))
+              (js-format "}")))
            (for
             (destructuring-bind ((start condition step) &body body) (cdr form)
               (js-format "for (")