From 008edd3627c0cc149280b9143a18bb7cea3c0918 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20V=C3=A1zquez?= Date: Sun, 30 Jun 2013 17:29:00 +0200 Subject: [PATCH] Optional arguments --- src/compiler.lisp | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/compiler.lisp b/src/compiler.lisp index c02d326..0829144 100644 --- a/src/compiler.lisp +++ b/src/compiler.lisp @@ -276,26 +276,17 @@ (n-required-arguments (length (ll-required-arguments ll))) (n-optional-arguments (length optional-arguments))) (when optional-arguments - `(code "switch(nargs){" - ,(let ((cases nil) - (idx 0)) - (progn - (while (< idx n-optional-arguments) - (let ((arg (nth idx optional-arguments))) - (push `(code "case " ,(+ idx n-required-arguments) ":" - (code ,(translate-variable (car arg)) - "=" - ,(ls-compile (cadr arg)) ";") - ,(when (third arg) - `(code ,(translate-variable (third arg)) - "=" - ,(ls-compile nil) - ";"))) - cases) - (incf idx))) - (push `(code "default: break;") cases) - `(code ,@(reverse cases)))) - "}")))) + `(switch |nargs| + ,@(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)))))))) (defun compile-lambda-rest (ll) (let ((n-required-arguments (length (ll-required-arguments ll))) @@ -426,8 +417,7 @@ ,(lambda-check-argument-count n-required-arguments n-optional-arguments (or rest-argument keyword-arguments)) - (code - ,(compile-lambda-optional ll)) + ,(compile-lambda-optional ll) ,(compile-lambda-rest ll) (code ,(compile-lambda-parse-keywords ll)) -- 1.7.10.4