Verified Commit e6489e09 authored by Vladimir Sedach's avatar Vladimir Sedach
Browse files

Hack replacement of loop lexical scope capturing WITH with LAMBDA

parent b734520e
......@@ -2,10 +2,11 @@
;;; Copyright 2005-2006 Edward Marco Baringer
;;; Copyright 2006 Attila Lendvai
;;; Copyright 2006 Luca Capello
;;; Copyright 2007-2012 Vladimir Sedach
;;; Copyright 2007-2012, 2018 Vladimir Sedach
;;; Copyright 2008 Travis Cross
;;; Copyright 2009-2010 Red Daly
;;; Copyright 2009-2010 Daniel Gackle
;;; Copyright 2012, 2015 Boris Smilga
;;; SPDX-License-Identifier: BSD-3-Clause
......
......@@ -640,20 +640,15 @@ Parenscript now implements implicit return, update your code! Things like (lambd
init-forms))
(defun compile-loop-body (loop-vars body)
(let*
((in-loop-scope? t)
;; provides lexical bindings for all free variables using WITH
(in-function-scope? t)
(let (compiled-body loop-closures?)
(let* ((in-loop-scope? t)
(*loop-scope-lexicals* ())
(*loop-scope-lexicals-captured* ())
(*ps-gensym-counter* *ps-gensym-counter*)
(compiled-body (compile-statement `(progn ,@body))))
(aif (remove-duplicates *loop-scope-lexicals-captured*)
`(ps-js:block
(ps-js:with
,(compile-expression
`(create ,@(mapcan (lambda (x) (list x nil)) it)))
,compiled-body))
(*ps-gensym-counter* *ps-gensym-counter*))
(setf compiled-body (compile-statement `(progn ,@body))
loop-closures? *loop-scope-lexicals-captured*))
(if loop-closures?
(compile-statement `(progn ((lambda () ,@body))))
compiled-body)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
......
......@@ -849,6 +849,23 @@
(loop for x across x sum (funcall x)))
45)
(test-js-eval loop-variable-capture4
(let ((x (make-array 10 :initial-element (lambda () 0))))
(dotimes (i 10)
(let ((i i))
(setf (aref x i) (lambda () i))
(when (= i 5) (return))))
(loop for x across x sum (funcall x)))
15)
(test-js-eval loop-variable-capture5
(let ((A (make-array 10)))
(dotimes (i 10)
(flet ((foo (x) (+ i x)))
(setf (aref A i) (lambda () (foo i)))))
(loop for x across A sum (funcall x)))
200)
(test-js-eval nested-let
(let ((x (let ((y 94))
y)))
......
......@@ -3363,12 +3363,12 @@ for (var i = 0; i < 10; i += 1) {
(lambda () (+ i x))))
"(function () {
for (var i = 0; i < 10; i += 1) {
with ({ x : null }) {
(function () {
var x = i + 1;
function () {
return function () {
return i + x;
};
};
})();
};
})();")
......@@ -3378,14 +3378,14 @@ for (var i = 0; i < 10; i += 1) {
(lambda () (foo i))))
"(function () {
for (var i = 0; i < 10; i += 1) {
with ({ foo : null }) {
(function () {
var foo = function (x) {
return i + x;
};
function () {
return function () {
return foo(i);
};
};
})();
};
})();")
......@@ -3394,12 +3394,12 @@ for (var i = 0; i < 10; i += 1) {
(let ((abc (bar)))
(lambda () (+ 1 abc))))
"while (foo()) {
with ({ abc : null }) {
(function () {
var abc = bar();
function () {
return function () {
return 1 + abc;
};
};
})();
};")
(test-ps-js dotted-list-form
......
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