From fa3b84125e3a277ad77ca8bf70352582036fb2a8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20V=C3=A1zquez?= Date: Fri, 5 Jul 2013 03:27:50 +0200 Subject: [PATCH] Non-nested switch cases in codegen --- src/compiler-codegen.lisp | 22 ++++++++++++---------- src/compiler.lisp | 15 ++++++++------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/compiler-codegen.lisp b/src/compiler-codegen.lisp index 480a856..83cb4b9 100644 --- a/src/compiler-codegen.lisp +++ b/src/compiler-codegen.lisp @@ -429,16 +429,18 @@ (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) diff --git a/src/compiler.lisp b/src/compiler.lisp index fd7e1e0..bfa9abb 100644 --- a/src/compiler.lisp +++ b/src/compiler.lisp @@ -280,13 +280,14 @@ ,@(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))) -- 1.7.10.4