shithub: mc

Download patch

ref: 6f62b7bb0abfcf72d515f476d20c414d4b82f1d6
parent: ee96b8a5cafc4811c1d4e253f843ac06426a6e5d
author: Ori Bernstein <[email protected]>
date: Mon Jul 9 15:58:49 EDT 2012

Start work towards closures.

    - Add test for a closure
    - Don't capture globals: they don't need it.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -176,6 +176,7 @@
 toplev
         : decl
             {lappend(&file->file.stmts, &file->file.nstmts, $1);
+             $1->decl.isglobl = 1;
              putdcl(file->file.globls, $1);}
         | use
             {lappend(&file->file.uses, &file->file.nuses, $1);}
@@ -224,7 +225,7 @@
 
 pkgitem : decl 
             {putdcl(file->file.exports, $1);
-            if ($1->decl.init)
+             if ($1->decl.init)
                  lappend(&file->file.stmts, &file->file.nstmts, $1);}
         | tydef {puttype(file->file.exports, mkname($1.line, $1.name), $1.type);}
         | visdef {die("Unimplemented visdef");}
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -209,9 +209,10 @@
 
         struct {
             long  did;
-            int   isconst;
-            int   isgeneric;
-            int   isextern;
+            char  isglobl;
+            char  isconst;
+            char  isgeneric;
+            char  isextern;
             Node *name;
             Type *type;
             Node *init;
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -78,7 +78,7 @@
             /* record that this is in the closure of this scope */
             if (!st->closure)
                 st->closure = mkht(namehash, nameeq);
-            if (st != orig)
+            if (st != orig && !n->decl.isglobl)
                 htput(st->closure, s->decl.name, s);
             return s;
         }
--- /dev/null
+++ b/test/closure.myr
@@ -1,0 +1,7 @@
+const main = {
+	var a = 42
+	var f = {b
+		-> a + b
+	}
+	-> f(13)
+}
--- a/test/tests
+++ b/test/tests
@@ -32,6 +32,7 @@
 B voidcall	E	12
 B callbig	E	42
 B nestfn	E	42
+B closure	E	55
 B loop		E	45
 B fib		E	21
 B float		E	1