Verified Commit 7d17a98c authored by Vladimir Sedach's avatar Vladimir Sedach
Browse files

Undid 88c61599

No longer going to parenthesize lambdas and objects at toplevel.
The REPL use case (the original motivation for parenthesizing at
toplevel[1]) is a not very compelling special case. Apparently,
MongoDB has some brain damage with parsing parenthesized expressions,
which is a more compelling use case, and a reason not to parenthesize.

Thanks to Lukas Phaf for the bug report.

[1] https://mailman.common-lisp.net/pipermail/parenscript-devel/2011-November/000838.html
parent 1c2d80ef
...@@ -55,8 +55,6 @@ vice-versa.") ...@@ -55,8 +55,6 @@ vice-versa.")
(defvar *psw-stream*) (defvar *psw-stream*)
(defvar %printer-toplevel?)
(defun parenscript-print (form immediate?) (defun parenscript-print (form immediate?)
(declare (special immediate?)) (declare (special immediate?))
(let ((*indent-level* 0) (let ((*indent-level* 0)
...@@ -64,8 +62,7 @@ vice-versa.") ...@@ -64,8 +62,7 @@ vice-versa.")
(*psw-stream* (if immediate? (*psw-stream* (if immediate?
*psw-stream* *psw-stream*
(make-string-output-stream))) (make-string-output-stream)))
(%psw-accumulator ()) (%psw-accumulator ()))
(%printer-toplevel? t))
(declare (special %psw-accumulator)) (declare (special %psw-accumulator))
(with-standard-io-syntax (with-standard-io-syntax
(if (and (listp form) (eq 'ps-js:block (car form))) ; ignore top-level block (if (and (listp form) (eq 'ps-js:block (car form))) ; ignore top-level block
...@@ -101,10 +98,6 @@ vice-versa.") ...@@ -101,10 +98,6 @@ vice-versa.")
(defgeneric ps-print (form)) (defgeneric ps-print (form))
(defgeneric ps-print% (js-primitive args)) (defgeneric ps-print% (js-primitive args))
(defmethod ps-print :after (form)
(declare (ignore form))
(setf %printer-toplevel? nil))
(defmacro defprinter (js-primitive args &body body) (defmacro defprinter (js-primitive args &body body)
(if (listp js-primitive) (if (listp js-primitive)
(cons 'progn (mapcar (lambda (p) (cons 'progn (mapcar (lambda (p)
...@@ -218,15 +211,9 @@ vice-versa.") ...@@ -218,15 +211,9 @@ vice-versa.")
ps-js:funcall ps-js:aref ps-js:getprop))) ;; these aren't really associative, but RPN ps-js:funcall ps-js:aref ps-js:getprop))) ;; these aren't really associative, but RPN
(defun parenthesize-print (x) (defun parenthesize-print (x)
(psw #\() (if (functionp x) (funcall x) (ps-print x)) (psw #\))) (psw #\() (ps-print x) (psw #\)))
(defun parenthesize-at-toplevel (x)
(if %printer-toplevel?
(parenthesize-print x)
(funcall x)))
(defun print-op-argument (op argument) (defun print-op-argument (op argument)
(setf %printer-toplevel? nil)
(let ((arg-op (when (listp argument) (car argument)))) (let ((arg-op (when (listp argument) (car argument))))
(if (or (< (precedence op) (precedence arg-op)) (if (or (< (precedence op) (precedence arg-op))
(and (= (precedence op) (precedence arg-op)) (and (= (precedence op) (precedence arg-op))
...@@ -293,8 +280,7 @@ vice-versa.") ...@@ -293,8 +280,7 @@ vice-versa.")
"}") "}")
(defprinter ps-js:lambda (args body-block) (defprinter ps-js:lambda (args body-block)
(parenthesize-at-toplevel (print-fun-def nil args body-block))
(lambda () (print-fun-def nil args body-block))))
(defprinter ps-js:defun (name args docstring body-block) (defprinter ps-js:defun (name args docstring body-block)
(when docstring (print-comment docstring)) (when docstring (print-comment docstring))
...@@ -310,29 +296,27 @@ vice-versa.") ...@@ -310,29 +296,27 @@ vice-versa.")
(ps-print body))) (ps-print body)))
(defprinter ps-js:object (&rest slot-defs) (defprinter ps-js:object (&rest slot-defs)
(parenthesize-at-toplevel (psw "{ ")
(lambda () (let ((indent? (< 2 (length slot-defs)))
(psw "{ ") (indent *column*))
(let ((indent? (< 2 (length slot-defs))) (loop for ((slot-name . slot-value) . remaining) on slot-defs do
(indent *column*)) (if (consp slot-name)
(loop for ((slot-name . slot-value) . remaining) on slot-defs do (apply #'print-fun-def slot-name slot-value)
(if (consp slot-name) (progn
(apply #'print-fun-def slot-name slot-value) (ps-print slot-name) (psw " : ")
(progn (if (and (consp slot-value)
(ps-print slot-name) (psw " : ") (eq 'ps-js:|,| (car slot-value)))
(if (and (consp slot-value) (parenthesize-print slot-value)
(eq 'ps-js:|,| (car slot-value))) (ps-print slot-value))))
(parenthesize-print slot-value) (when remaining
(ps-print slot-value)))) (psw ",")
(when remaining (if indent?
(psw ",") (newline-and-indent indent)
(if indent? (psw #\Space))))
(newline-and-indent indent) (if indent?
(psw #\Space)))) (newline-and-indent (- indent 2))
(if indent? (psw #\Space)))
(newline-and-indent (- indent 2)) (psw "}"))
(psw #\Space)))
(psw "}"))))
(defprinter ps-js:getprop (obj slot) (defprinter ps-js:getprop (obj slot)
(print-op-argument op obj)"."(psw (symbol-to-js-string slot))) (print-op-argument op obj)"."(psw (symbol-to-js-string slot)))
......
...@@ -149,15 +149,15 @@ ...@@ -149,15 +149,15 @@
(test-ps-js object-literals-1 (test-ps-js object-literals-1
(create foo "bar" :blorg 1) (create foo "bar" :blorg 1)
"({ foo : 'bar', 'blorg' : 1 });") "{ foo : 'bar', 'blorg' : 1 };")
(test-ps-js object-literals-2 (test-ps-js object-literals-2
(create foo "hihi" (create foo "hihi"
blorg (array 1 2 3) blorg (array 1 2 3)
another-object (create :schtrunz 1)) another-object (create :schtrunz 1))
"({ foo : 'hihi', "{ foo : 'hihi',
blorg : [1, 2, 3], blorg : [1, 2, 3],
anotherObject : { 'schtrunz' : 1 } });") anotherObject : { 'schtrunz' : 1 } };")
(test-ps-js object-literals-3 (test-ps-js object-literals-3
(getprop an-object 'foo) (getprop an-object 'foo)
...@@ -174,14 +174,14 @@ ...@@ -174,14 +174,14 @@
(test-ps-js with-slots-single-eval (test-ps-js with-slots-single-eval
(lambda () (with-slots (a b) (foo) (+ a b))) (lambda () (with-slots (a b) (foo) (+ a b)))
"(function () { "function () {
var object1 = foo(); var object1 = foo();
return object1.a + object1.b; return object1.a + object1.b;
});") };")
(test-ps-js object-literal-quoted-symbols (test-ps-js object-literal-quoted-symbols
(create 'test "bang" 'symbol-saved-my-life "parenscript") (create 'test "bang" 'symbol-saved-my-life "parenscript")
"({ 'test' : 'bang', 'symbolSavedMyLife' : 'parenscript' });") "{ 'test' : 'bang', 'symbolSavedMyLife' : 'parenscript' };")
(test-ps-js object-literal-property-accessors (test-ps-js object-literal-property-accessors
(defun foo () (defun foo ()
...@@ -240,9 +240,9 @@ ...@@ -240,9 +240,9 @@
(test-ps-js literal-symbols-4 (test-ps-js literal-symbols-4
(lambda () nil) (lambda () nil)
"(function () { "function () {
return null; return null;
});") };")
(test-ps-js literal-symbols-5 (test-ps-js literal-symbols-5
undefined undefined
...@@ -334,9 +334,9 @@ blafoo(i);") ...@@ -334,9 +334,9 @@ blafoo(i);")
(test-ps-js lambda-definition-2 (test-ps-js lambda-definition-2
(lambda (a b) (+ a b)) (lambda (a b) (+ a b))
"(function (a, b) { "function (a, b) {
return a + b; return a + b;
});") };")
(test-ps-js assignment-1 (test-ps-js assignment-1
(setf a 1) (setf a 1)
...@@ -1178,13 +1178,13 @@ __setf_someThing('foo', 1, 2);") ...@@ -1178,13 +1178,13 @@ __setf_someThing('foo', 1, 2);")
(test-ps-js arglist-optional4 (test-ps-js arglist-optional4
(lambda (&optional (x 0 supplied?)) (lambda (&optional (x 0 supplied?))
x) x)
"(function (x) { "function (x) {
var suppliedwhat = x !== undefined; var suppliedwhat = x !== undefined;
if (!suppliedwhat) { if (!suppliedwhat) {
x = 0; x = 0;
}; };
return x; return x;
});") };")
(test-ps-js return-nothing (test-ps-js return-nothing
(defun foo () (return-from foo)) (defun foo () (return-from foo))
...@@ -1233,11 +1233,11 @@ __setf_someThing('foo', 1, 2);") ...@@ -1233,11 +1233,11 @@ __setf_someThing('foo', 1, 2);")
(test-ps-js obj-literal-numbers (test-ps-js obj-literal-numbers
(create 1 "foo") (create 1 "foo")
"({ 1 : 'foo' });") "{ 1 : 'foo' };")
(test-ps-js obj-literal-strings (test-ps-js obj-literal-strings
(create "foo" 2) (create "foo" 2)
"({ 'foo' : 2 });") "{ 'foo' : 2 };")
(test-ps-js getprop-string (test-ps-js getprop-string
(getprop foo "bar") (getprop foo "bar")
...@@ -1261,11 +1261,11 @@ __setf_someThing('foo', 1, 2);") ...@@ -1261,11 +1261,11 @@ __setf_someThing('foo', 1, 2);")
(test-ps-js create-blank (test-ps-js create-blank
(create) (create)
"({ });") "{ };")
(test-ps-js blank-object-literal (test-ps-js blank-object-literal
{} {}
"({ });") "{ };")
(test-ps-js array-literal1 (test-ps-js array-literal1
[] []
...@@ -1366,7 +1366,7 @@ __setf_someThing('foo', 1, 2);") ...@@ -1366,7 +1366,7 @@ __setf_someThing('foo', 1, 2);")
(test-ps-js arglist-keyword-supplied (test-ps-js arglist-keyword-supplied
(lambda (&key (foo 1 supplied?)) (lambda (&key (foo 1 supplied?))
foo) foo)
"(function () { "function () {
var _js2 = arguments.length; var _js2 = arguments.length;
for (var n1 = 0; n1 < _js2; n1 += 2) { for (var n1 = 0; n1 < _js2; n1 += 2) {
switch (arguments[n1]) { switch (arguments[n1]) {
...@@ -1378,7 +1378,7 @@ __setf_someThing('foo', 1, 2);") ...@@ -1378,7 +1378,7 @@ __setf_someThing('foo', 1, 2);")
var suppliedwhat; var suppliedwhat;
var foo = 'undefined' === typeof foo ? 1 : foo; var foo = 'undefined' === typeof foo ? 1 : foo;
return foo; return foo;
});") };")
(test-ps-js keyword-funcall1 (test-ps-js keyword-funcall1
(func :baz 1) (func :baz 1)
...@@ -2003,10 +2003,10 @@ return x1 + y; ...@@ -2003,10 +2003,10 @@ return x1 + y;
(lambda () (lambda ()
(let (x) (let (x)
(+ x x))) (+ x x)))
"(function () { "function () {
var x = null; var x = null;
return x + x; return x + x;
});") };")
(test-ps-js let*1 (test-ps-js let*1
(let* ((x 1)) (let* ((x 1))
...@@ -2117,10 +2117,10 @@ return x + x; ...@@ -2117,10 +2117,10 @@ return x + x;
(lambda (x) (lambda (x)
(let ((x (+ 1 x))) (let ((x (+ 1 x)))
x)) x))
"(function (x) { "function (x) {
var x1 = 1 + x; var x1 = 1 + x;
return x1; return x1;
});") };")
(test-ps-js symbol-macro-array (test-ps-js symbol-macro-array
(symbol-macrolet ((x 1)) (symbol-macrolet ((x 1))
...@@ -2130,12 +2130,12 @@ return x + x; ...@@ -2130,12 +2130,12 @@ return x + x;
(test-ps-js symbol-macro-obj (test-ps-js symbol-macro-obj
(symbol-macrolet ((x (+ 1 2))) (symbol-macrolet ((x (+ 1 2)))
(create x 1)) (create x 1))
"({ x : 1 });") "{ x : 1 };")
(test-ps-js symbol-macro-obj1 (test-ps-js symbol-macro-obj1
(symbol-macrolet ((x (+ 1 2))) (symbol-macrolet ((x (+ 1 2)))
(ps:create x x)) (ps:create x x))
"({ x : 1 + 2 });") "{ x : 1 + 2 };")
(test-ps-js symbol-macro-getprop1 (test-ps-js symbol-macro-getprop1
(symbol-macrolet ((x (+ 1 2))) (symbol-macrolet ((x (+ 1 2)))
...@@ -2283,7 +2283,7 @@ return ++x1; ...@@ -2283,7 +2283,7 @@ return ++x1;
(test-ps-js create-reserved-word (test-ps-js create-reserved-word
(create :default 1) (create :default 1)
"({ 'default' : 1 });") "{ 'default' : 1 };")
(test-ps-js getprop-reserved-word (test-ps-js getprop-reserved-word
(getprop foo :default) (getprop foo :default)
...@@ -2342,10 +2342,10 @@ return ++x1; ...@@ -2342,10 +2342,10 @@ return ++x1;
(test-ps-js nary-comparison1 (test-ps-js nary-comparison1
(lambda () (< 1 2 3)) (lambda () (< 1 2 3))
"(function () { "function () {
var _cmp1; var _cmp1;
return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3); return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
});") };")
(test-ps-js chain-getprop1 (test-ps-js chain-getprop1
(chain ($ "foo") (bar x z) frob (baz 5)) (chain ($ "foo") (bar x z) frob (baz 5))
...@@ -2516,10 +2516,10 @@ try { ...@@ -2516,10 +2516,10 @@ try {
(test-ps-js values0 (test-ps-js values0
(lambda () (values)) (lambda () (values))
"(function () { "function () {
__PS_MV_REG = {}; __PS_MV_REG = {};
return null; return null;
});") };")
(test-ps-js values1 (test-ps-js values1
(values x) (values x)
...@@ -2527,17 +2527,17 @@ try { ...@@ -2527,17 +2527,17 @@ try {
(test-ps-js values2 (test-ps-js values2
(lambda () (values x y)) (lambda () (values x y))
"(function () { "function () {
__PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y] }; __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y] };
return x; return x;
});") };")
(test-ps-js values3 (test-ps-js values3
(lambda () (values x y z)) (lambda () (values x y z))
"(function () { "function () {
__PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y, z] }; __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y, z] };
return x; return x;
});") };")
(test-ps-js values-return (test-ps-js values-return
(defun foo (x y) (defun foo (x y)
...@@ -2624,7 +2624,7 @@ if (foowhat(x)) { ...@@ -2624,7 +2624,7 @@ if (foowhat(x)) {
(if a (if a
(loop for y in x do (foo y)) (loop for y in x do (foo y))
c)) c))
"(function () { "function () {
if (a) { if (a) {
var _js4 = x.length; var _js4 = x.length;
for (var _js3 = 0; _js3 < _js4; _js3 += 1) { for (var _js3 = 0; _js3 < _js4; _js3 += 1) {
...@@ -2634,7 +2634,7 @@ if (foowhat(x)) { ...@@ -2634,7 +2634,7 @@ if (foowhat(x)) {
} else { } else {
return c; return c;
}; };
});") };")
(test-ps-js lambda-loop-if-return1 (test-ps-js lambda-loop-if-return1
(defun baz () (defun baz ()
...@@ -2707,21 +2707,21 @@ case 2: ...@@ -2707,21 +2707,21 @@ case 2:
`(@ ,el offset-left))) `(@ ,el offset-left)))
(setf (left x) 10) (setf (left x) 10)
(left x))) (left x)))
"(function () { "function () {
var _js2 = x; var _js2 = x;
var _js1 = 10; var _js1 = 10;
_js2.style.left = _js1; _js2.style.left = _js1;
return x.offsetLeft; return x.offsetLeft;
});") };")
(test-ps-js for-return (test-ps-js for-return
(lambda () (dolist (arg args) (foo arg))) (lambda () (dolist (arg args) (foo arg)))
"(function () { "function () {
for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) { for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
arg = args[_js_idx1]; arg = args[_js_idx1];
foo(arg); foo(arg);
}; };
});") };")
(test-ps-js try-catch-return (test-ps-js try-catch-return
(defun foo () (defun foo ()
...@@ -2796,16 +2796,16 @@ __setf_foo(5, x, 1, 2, 3, 4);") ...@@ -2796,16 +2796,16 @@ __setf_foo(5, x, 1, 2, 3, 4);")
(test-ps-js implicit-return-null (test-ps-js implicit-return-null
(lambda () (lambda ()
) )
"(function () { "function () {
return null; return null;
});") };")
(test-ps-js implicit-return-null (test-ps-js implicit-return-null
(lambda () (lambda ()
nil) nil)
"(function () { "function () {
return null; return null;
});") };")
(test-ps-js return-conditional-nested (test-ps-js return-conditional-nested
(defun blep (ss x y) (defun blep (ss x y)
...@@ -2882,7 +2882,7 @@ try { ...@@ -2882,7 +2882,7 @@ try {
(declare (special *foo*)) (declare (special *foo*))
(let ((*foo* 1)) (let ((*foo* 1))
(1+ *foo*))) (1+ *foo*)))
"(function () { "function () {
var FOO_TMPSTACK1; var FOO_TMPSTACK1;
try { try {
FOO_TMPSTACK1 = FOO; FOO_TMPSTACK1 = FOO;
...@@ -2891,7 +2891,7 @@ try { ...@@ -2891,7 +2891,7 @@ try {
} finally { } finally {
FOO = FOO_TMPSTACK1; FOO = FOO_TMPSTACK1;
}; };
});") };")
(test-ps-js declare-special-let (test-ps-js declare-special-let
(let ((*foo* 123)) (let ((*foo* 123))
...@@ -3149,7 +3149,7 @@ a === b;") ...@@ -3149,7 +3149,7 @@ a === b;")
(return-from scope)) (return-from scope))
(blee)) (blee))
(+ 1 2)) (+ 1 2))
"(function () { "function () {
scope: { scope: {
foo(); foo();
if (bar()) { if (bar()) {
...@@ -3158,7 +3158,7 @@ a === b;") ...@@ -3158,7 +3158,7 @@ a === b;")
blee(); blee();
}; };
return 1 + 2; return 1 + 2;
});") };")
(test-ps-js block-return-from2 (test-ps-js block-return-from2
(lambda () (lambda ()
...@@ -3168,14 +3168,14 @@ a === b;") ...@@ -3168,14 +3168,14 @@ a === b;")
(when (bar) (when (bar)
(return-from scope 6)) (return-from scope 6))
(blee))) (blee)))
"(function () { "function () {
bar(5); bar(5);
foo(); foo();
if (bar()) { if (bar()) {
return 6; return 6;
}; };
return blee(); return blee();
});") };")
(test-ps-js let-funcall (test-ps-js let-funcall
(let ((x foo)) (let ((x foo))
...@@ -3219,11 +3219,11 @@ return x(); ...@@ -3219,11 +3219,11 @@ return x();
(test-ps-js lambda-apply (test-ps-js lambda-apply
(lambda (x) (lambda (x)
(apply (lambda (y) (bar (1+ y))) x)) (apply (lambda (y) (bar (1+ y))) x))