summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-08-03 23:35:59 -0700
committerOri Bernstein <ori@eigenstate.org>2018-08-03 23:36:54 -0700
commit80ad746d8032274e2e6b40d3e4d801aa2085082d (patch)
tree198beefbb3e252d4fabc4cd66f7b5623897111d9
parent17b5405f1035985d60539f7d777b77cf56e7231a (diff)
downloadmc-80ad746d8032274e2e6b40d3e4d801aa2085082d.tar.gz
Record the closure into all nested funcs.
Funcs can go inside funcs.
-rw-r--r--parse/stab.c19
-rw-r--r--test/nestclosure.myr11
-rw-r--r--test/tests1
3 files changed, 26 insertions, 5 deletions
diff --git a/parse/stab.c b/parse/stab.c
index 0319d13..41dac79 100644
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -215,6 +215,16 @@ getclosure(Stab *st, size_t *n)
return vals;
}
+static void
+envclose(Stab *start, Stab *found, Stab *fn, Node *s)
+{
+ Stab *st;
+
+ for (st = start; st != found; st = st->super)
+ if (st->env && !s->decl.isglobl && !s->decl.isgeneric)
+ htput(st->env, s->decl.name, s);
+}
+
/*
* Searches for declarations from current
* scope, and all enclosing scopes. Doe
@@ -226,18 +236,17 @@ getclosure(Stab *st, size_t *n)
* in the scope's closure.
*/
Node *
-getdcl(Stab *st, Node *n)
+getdcl(Stab *start, Node *n)
{
+ Stab *st, *fn;
Node *s;
- Stab *fn;
fn = NULL;
+ st = start;
do {
s = htget(st->dcl, n);
if (s) {
- /* record that this is in the closure of this scope */
- if (fn && !s->decl.isglobl && !s->decl.isgeneric)
- htput(fn->env, s->decl.name, s);
+ envclose(start, st, fn, s);
return s;
}
if (!fn && st->env)
diff --git a/test/nestclosure.myr b/test/nestclosure.myr
new file mode 100644
index 0000000..05a55a7
--- /dev/null
+++ b/test/nestclosure.myr
@@ -0,0 +1,11 @@
+use std
+
+const main = {
+ var a = 1
+ var f = std.fndup({
+ {
+ std.put("{}\n", a)
+ }()
+ })
+ f()
+}
diff --git a/test/tests b/test/tests
index 2291b7e..4ac4fb0 100644
--- a/test/tests
+++ b/test/tests
@@ -64,6 +64,7 @@ B callbig E 42
B nestfn E 42
B foldidx P 123,456
B closure P 111555333666
+B nestclosure P 1
B closurerec E 21
B fncast P ok
B loop P 0123401236789