Verified Commit 1bd184cc authored by Vladimir Sedach's avatar Vladimir Sedach
Browse files

Added DEFPARAMETER special form

Thanks to Olaf Ruppert for the suggestion.
parent 432572a0
...@@ -1176,6 +1176,7 @@ default: ...@@ -1176,6 +1176,7 @@ default:
<li>(<code id="let">LET</code> ({<var>var</var> | (<var>var</var> <var>value</var>)}*) <var>body</var>)</li> <li>(<code id="let">LET</code> ({<var>var</var> | (<var>var</var> <var>value</var>)}*) <var>body</var>)</li>
<li>(<code id="let*">LET*</code> ({<var>var</var> | (<var>var</var> <var>value</var>)}*) <var>body</var>)</li> <li>(<code id="let*">LET*</code> ({<var>var</var> | (<var>var</var> <var>value</var>)}*) <var>body</var>)</li>
<li>(<code id="defvar">DEFVAR</code> <var>var</var> {<var>value</var>}?)</li> <li>(<code id="defvar">DEFVAR</code> <var>var</var> {<var>value</var>}?)</li>
<li>(<code id="defvar">DEFPARAMETER</code> <var>var</var> {<var>value</var>}?)</li>
<li>(<code id="var">VAR</code> <var>var</var> {<var>value</var>}?)</li> <li>(<code id="var">VAR</code> <var>var</var> {<var>value</var>}?)</li>
</ul> </ul>
......
...@@ -626,7 +626,18 @@ Parenscript now implements implicit return, update your code! Things like (lambd ...@@ -626,7 +626,18 @@ Parenscript now implements implicit return, update your code! Things like (lambd
documentation) documentation)
;; this must be used as a top-level form, otherwise the resulting ;; this must be used as a top-level form, otherwise the resulting
;; behavior will be undefined. ;; behavior will be undefined.
(declare (ignore documentation)) (declare (ignore documentation)) ; TODO: print docstring
(pushnew name *special-variables*)
(ps-compile (if value-provided?
`(when (undefined ,name) (var ,name ,value))
(list 'var name))))
(define-statement-operator defparameter
(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)) ; TODO: print docstring
(pushnew name *special-variables*) (pushnew name *special-variables*)
(ps-compile `(var ,name ,@(when value-provided? (list value))))) (ps-compile `(var ,name ,@(when value-provided? (list value)))))
......
...@@ -464,7 +464,7 @@ return _js2.style.left = _js1; ...@@ -464,7 +464,7 @@ return _js2.style.left = _js1;
(test-ps-js variable-declaration-1 (test-ps-js variable-declaration-1
(defvar *a* (array 1 2 3)) (defvar *a* (array 1 2 3))
"var A = [1, 2, 3];") "if ('undefined' === typeof A) { var A = [1, 2, 3]; };")
(test-ps-js variable-declaration-2 (test-ps-js variable-declaration-2
(progn (defvar *a* 4) (progn (defvar *a* 4)
...@@ -473,6 +473,32 @@ return _js2.style.left = _js1; ...@@ -473,6 +473,32 @@ return _js2.style.left = _js1;
(let* ((y (+ x 1)) (let* ((y (+ x 1))
(x (+ x y))) (x (+ x y)))
(+ *a* x y)))) (+ *a* x y))))
"if ('undefined' === typeof A) { var A = 4; };
(function () {
var x = 1;
var A_TMPSTACK1;
try {
A_TMPSTACK1 = A;
A = 2;
var y = x + 1;
var x2 = x + y;
return A + x2 + y;
} finally {
A = A_TMPSTACK1;
};
})();")
(test-ps-js variable-declaration-3
(defparameter A 987)
"var A = 987;")
(test-ps-js variable-declaration-4
(progn (defparameter *a* 4)
(let ((x 1)
(*a* 2))
(let* ((y (+ x 1))
(x (+ x y)))
(+ *a* x y))))
"var A = 4; "var A = 4;
(function () { (function () {
var x = 1; var x = 1;
...@@ -488,6 +514,10 @@ try { ...@@ -488,6 +514,10 @@ try {
}; };
})();") })();")
(test-ps-js variable-declaration-5
(defvar BAZ)
"var BAZ;")
(test-ps-js iteration-constructs-1 (test-ps-js iteration-constructs-1
(do* ((a) b (c (array "a" "b" "c" "d" "e")) (do* ((a) b (c (array "a" "b" "c" "d" "e"))
(d 0 (1+ d)) (d 0 (1+ d))
...@@ -1571,7 +1601,7 @@ __setf_someThing('foo', 1, 2);") ...@@ -1571,7 +1601,7 @@ __setf_someThing('foo', 1, 2);")
})();") })();")
(test-ps-js special-var2 (test-ps-js special-var2
(progn (defvar *foo*) (progn (defparameter *foo*)
(let* ((*baz* 3) (let* ((*baz* 3)
(*foo* 2)) (*foo* 2))
(* *foo* 2 *baz*))) (* *foo* 2 *baz*)))
...@@ -3519,7 +3549,7 @@ while (foo()) { ...@@ -3519,7 +3549,7 @@ while (foo()) {
(block nil (block nil
((lambda () (return 6))) ((lambda () (return 6)))
(+ 1 2))))) (+ 1 2)))))
"var foo = (function () { "if ('undefined' === typeof foo) { var foo = (function () {
try { try {
(function () { (function () {
throw { '__ps_block_tag' : 'nilBlock', '__ps_value1' : 6 }; throw { '__ps_block_tag' : 'nilBlock', '__ps_value1' : 6 };
...@@ -3533,14 +3563,15 @@ while (foo()) { ...@@ -3533,14 +3563,15 @@ while (foo()) {
throw _ps_err1; throw _ps_err1;
}; };
}; };
})();") })(); };")
(test-ps-js block-dynamic-return1-redundant (test-ps-js block-dynamic-return1-redundant
(defvar foo ((lambda () (defparameter foo
(block nil ((lambda ()
((lambda () (return 6))) (block nil
(+ 1 2)) ((lambda () (return 6)))
(+ 4 5)))) (+ 1 2))
(+ 4 5))))
;;; FIXME. Not wrong, but redundant ;;; FIXME. Not wrong, but redundant
"var foo = (function () { "var foo = (function () {
nilBlock: { nilBlock: {
...@@ -3960,7 +3991,7 @@ return function (x) { ...@@ -3960,7 +3991,7 @@ return function (x) {
})() });") })() });")
(test-ps-js toplevel-local-scope1 (test-ps-js toplevel-local-scope1
(defvar foo (create "fn" (let ((x 5)) (lambda () x)))) (defparameter foo (create "fn" (let ((x 5)) (lambda () x))))
"var foo = { 'fn' : (function () { "var foo = { 'fn' : (function () {
var x = 5; var x = 5;
return function () { return function () {
...@@ -4007,7 +4038,7 @@ return function (x) { ...@@ -4007,7 +4038,7 @@ return function (x) {
function bar() { function bar() {
return foo + 1; return foo + 1;
}; };
var baz = 2;") if ('undefined' === typeof baz) { var baz = 2; };")
(test-ps-js js-ir-package-unique-symbols (test-ps-js js-ir-package-unique-symbols
(loop :for i :from 0 :below 5 :do (loop :for i :from 0 :below 5 :do
...@@ -4172,7 +4203,7 @@ bar();") ...@@ -4172,7 +4203,7 @@ bar();")
(defvar bar (1+ foo))) (defvar bar (1+ foo)))
bar) bar)
"var foo = 0; "var foo = 0;
var bar = foo + 1; if ('undefined' === typeof bar) { var bar = foo + 1; };
bar;") bar;")
(test-ps-js setf-side-effects (test-ps-js setf-side-effects
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment