From ac14e3993f5b288f8e98a1b0f5af4a06ec2fb02e Mon Sep 17 00:00:00 2001 From: Vladimir Sedach Date: Sat, 24 Dec 2011 12:26:42 -0500 Subject: [PATCH] Fixed up bug where defuns were getting wrapped in lambdas because toplevel form checking weren't getting macroexpanded. --- src/macros.lisp | 9 ------ src/special-operators.lisp | 17 ++++++++-- t/output-tests.lisp | 81 +++++++++++++++++++++++++++------------------- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/src/macros.lisp b/src/macros.lisp index 4141f9f..11a3b5f 100644 --- a/src/macros.lisp +++ b/src/macros.lisp @@ -435,15 +435,6 @@ lambda-list::= ;;; misc -(defpsmacro defvar (name &optional - (value (values) value-provided?) - documentation) - ;; this must be used as a top-level form, otherwise the resulting - ;; behavior will be undefined. - (declare (ignore documentation)) - (pushnew name *special-variables*) - `(var ,name ,@(when value-provided? (list value)))) - (defpsmacro let* (bindings &body body) (if bindings `(let (,(car bindings)) diff --git a/src/special-operators.lisp b/src/special-operators.lisp index 8617b2d..59419f9 100644 --- a/src/special-operators.lisp +++ b/src/special-operators.lisp @@ -361,6 +361,15 @@ Parenscript now implements implicit return, update your code! Things like (lambd (third rhs))) (list 'ps-js:= lhs rhs)))))) +(define-statement-operator defvar (name &optional + (value (values) value-provided?) + documentation) + ;; this must be used as a top-level form, otherwise the resulting + ;; behavior will be undefined. + (declare (ignore documentation)) + (pushnew name *special-variables*) + (ps-compile `(var ,name ,@(when value-provided? (list value))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; binding @@ -451,9 +460,13 @@ Parenscript now implements implicit return, update your code! Things like (lambd dynamic-bindings))))) renamed-body)))) (ps-compile (cond (in-function-scope? let-body) + ;; HACK ((find-if (lambda (x) - (member x '(defun defvar defparameter))) - (flatten let-body)) + (member x '(defun% defvar))) + (flatten + (loop for x in body collecting + (or (ignore-errors (ps-macroexpand x)) + x)))) let-body) (t (with-lambda-scope let-body)))))))) diff --git a/t/output-tests.lisp b/t/output-tests.lisp index fd633f3..e985665 100644 --- a/t/output-tests.lisp +++ b/t/output-tests.lisp @@ -264,10 +264,10 @@ x = a + b + c;") "(function () { var a = 1; var b = 2; -var _js1 = b; -var _js2 = a; -a = _js1; -return b = _js2; +var _js3 = b; +var _js4 = a; +a = _js3; +return b = _js4; })();") (test-ps-js assignment-6 @@ -460,8 +460,8 @@ return alert('Summation to 10 is ' + (function () { ((@ document write) (+ "c: " c "
")))) "(function () { var l = [1, 2, 4, 8, 16, 32]; -for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) { - c = l[_js_idx1]; +for (var c = null, _js_idx2 = 0; _js_idx2 < l.length; _js_idx2 += 1) { + c = l[_js_idx2]; document.write('c: ' + c + '
'); }; })();") @@ -3470,36 +3470,49 @@ return loopResultVar3; }; };") -;;; broken +(test-ps-js toplevel-defun-macroexpand + (progn (defmacro defun-js (name lambda-list &body body) + `(defun ,name ,lambda-list ,@body)) -(test-ps-js let-defun-toplevel - (progn (let ((foo 0)) - (defun bar () foo)) - (bar)) - "var bar_foo1 = 0; + (let ((foo 0)) + (defun-js bar () (1+ foo)) + (defvar baz 2))) + "var foo = 0; function bar() { - return bar_foo1; + return foo + 1; }; -bar();") +var baz = 2;") -(test-ps-js let-defvar-toplevel - (progn (let ((foo 0)) - (defvar bar (1+ foo))) - bar) - "var bar_foo1 = 0; -var bar = bar_foo1 + 1; -bar;") +;;; broken -(test-ps-js setf-side-effects - (progn - (let ((x 10)) - (defun side-effect() - (setf x 4) - 3) - (setf x (+ 2 (side-effect) x 5)))) - "var sideEffect_x1 = 10; -function sideEffect() { - sideEffect_x1 = 4; - return 3; -}; -sideEffect_x1 = 2 + sideEffect() + x + 5;") +;; (test-ps-js let-defun-toplevel +;; (progn (let ((foo 0)) +;; (defun bar () foo)) +;; (bar)) +;; "var bar_foo1 = 0; +;; function bar() { +;; return bar_foo1; +;; }; +;; bar();") + +;; (test-ps-js let-defvar-toplevel +;; (progn (let ((foo 0)) +;; (defvar bar (1+ foo))) +;; bar) +;; "var bar_foo1 = 0; +;; var bar = bar_foo1 + 1; +;; bar;") + +;; (test-ps-js setf-side-effects +;; (progn +;; (let ((x 10)) +;; (defun side-effect() +;; (setf x 4) +;; 3) +;; (setf x (+ 2 (side-effect) x 5)))) +;; "var sideEffect_x1 = 10; +;; function sideEffect() { +;; sideEffect_x1 = 4; +;; return 3; +;; }; +;; sideEffect_x1 = 2 + sideEffect() + x + 5;") -- 2.11.4.GIT