From 8e756a42758fb7257fc36570628ebaa2911bb0f7 Mon Sep 17 00:00:00 2001 From: Vladimir Sedach Date: Fri, 10 Dec 2010 23:26:39 -0500 Subject: [PATCH] Changed heuristic for deciding to compile a conditional to a statement or expression to examine the hypothetical output instead of measuring s-exp depth. --- src/special-operators.lisp | 13 ++++++++----- t/output-tests.lisp | 32 ++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/special-operators.lisp b/src/special-operators.lisp index 7393e9a..bd31917 100644 --- a/src/special-operators.lisp +++ b/src/special-operators.lisp @@ -129,10 +129,11 @@ (*tags-that-return-throws-to* ())) `(ps-js:label ,name ,(wrap-block-for-dynamic-return name (compile-statement `(progn ,@body)))))) -(defun nesting-depth (form) - (if (consp form) - (max (1+ (nesting-depth (car form))) (nesting-depth (cdr form))) - 0)) +(defun try-expressionize-if? (form) + (< (count #\Newline (with-output-to-string (*psw-stream*) + (let ((*ps-print-pretty* t)) + (parenscript-print (compile-statement form) t)))) + (if (= (length form) 4) 5 4))) (define-statement-operator return-from (tag &optional result) (if (not tag) @@ -202,7 +203,9 @@ :format-control "Trying to RETURN a RETURN without a block tag specified. Perhaps you're still returning values from functions by hand? Parenscript now implements implicit return, update your code! Things like (lambda () (return x)) are not valid Common Lisp and may not be supported in future versions of Parenscript.")) form) (if - (aif (and (<= (nesting-depth form) 3) (handler-case (compile-expression form) (compile-expression-error () nil))) + (aif (and (try-expressionize-if? form) + (handler-case (compile-expression form) + (compile-expression-error () nil))) (return-from expressionize `(ps-js:return ,it)) `(if ,(second form) (return-from ,tag ,(third form)) diff --git a/t/output-tests.lisp b/t/output-tests.lisp index b43d1a5..3204c11 100644 --- a/t/output-tests.lisp +++ b/t/output-tests.lisp @@ -1088,9 +1088,7 @@ __setf_someThing('foo', 1, 2);") (setf (,macroname x) 123))))))) (normalize-js-code "function test1() { - if (data[x]) { - return data[x] = 123; - }; + return data[x] ? (data[x] = 123) : null; };")))) (test macro-environment2 @@ -1341,11 +1339,7 @@ bar(foo1(1));") (foo 3)))) "(function () { var foo = function (x) { - if (0 === x) { - return 0; - } else { - return x + foo(x - 1); - }; + return 0 === x ? 0 : x + foo(x - 1); }; return foo(3); })();") @@ -1423,7 +1417,11 @@ bar(foo(1));") (defun foo () (if x y (if z a b))) "function foo() { - return x ? y : (z ? a : b); + if (x) { + return y; + } else { + return z ? a : b; + }; };") (test-ps-js let1 @@ -2454,7 +2452,7 @@ foo = 3;") if (baz) { return 7; } else { - for (var _js1 = 0; _js1 < 100; _js1 += 1) { + for (var _js2 = 0; _js2 < 100; _js2 += 1) { bar(); }; return 42; @@ -2649,3 +2647,17 @@ foo = 3;") throw result; }; };") + +(test-ps-js expressify1 + (defun blah () + (when (some-condition) + (foo) + (bar) + (baz))) + "function blah() { + if (someCondition()) { + foo(); + bar(); + return baz(); + }; +};") -- 2.11.4.GIT