shithub: mc

Download patch

ref: 04ebbd679e7eccb3ecd7104f651b9bbbdc396753
parent: c0af6b2f13dbc6b0a09986ad6ee4e2b8a06393c1
parent: c9f9102a9342d40d886ff7b2716aa0cd66974c64
author: Ori Bernstein <[email protected]>
date: Fri Jan 6 11:00:55 EST 2012

Merge branch 'master' of git+ssh://eigenstate.org/git/ori/mc2

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -197,7 +197,9 @@
         ;
 
 pkgitem : decl {putdcl(file->file.exports, $1->decl.sym);}
-        | tydef {puttype(file->file.exports, $1.name, $1.type);}
+        | tydef {puttype(file->file.exports, 
+                         mkname($1.line, $1.name),
+                         $1.type);}
         | visdef {die("Unimplemented visdef");}
         | TEndln
         ;
@@ -523,7 +525,7 @@
 blockbody
         : stmt
             {
-                $$ = mkblock(line, mkstab(curscope));
+                $$ = mkblock(line, mkstab(curstab()));
                 nlappend(&$$->block.stmts, &$$->block.nstmts, $1);
             }
         | blockbody stmt
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -12,6 +12,7 @@
 
 #include "parse.h"
 
+
 /* find the most accurate type mapping */
 static Type *tf(Type *t)
 {
@@ -165,6 +166,7 @@
 static void inferexpr(Node *n, Type *ret)
 {
     Node **args;
+    Sym *s;
     int nargs;
     Type *t;
     int i;
@@ -262,7 +264,11 @@
             settype(n, mkty(-1, Tyvoid));
             break;
         case Ovar:      /* a:@a -> @a */
-            settype(n, decltype(n));
+            s = getdcl(curstab(), n);
+            if (!s)
+                fatal(n->line, "Undeclared var");
+            else
+                settype(n, s->type);
             break;
         case Olit:      /* <lit>:@a::tyclass -> @a */
             settype(n, type(args[0]));
@@ -296,8 +302,10 @@
 
     switch (n->type) {
         case Nfile:
+            pushstab(n->file.globls);
             for (i = 0; i < n->file.nstmts; i++)
                 infernode(n->file.stmts[i]);
+            popstab();
             break;
         case Ndecl:
             inferdecl(n);
--- a/parse/node.c
+++ b/parse/node.c
@@ -103,7 +103,7 @@
     f->func.args = args;
     f->func.nargs = nargs;
     f->func.body = body;
-    f->func.scope = mkstab(curscope);
+    f->func.scope = mkstab(curstab());
 
     n = mknode(line, Nlit);
     n->lit.littype = Lfunc;
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -80,6 +80,7 @@
      * types and values are in separate namespaces. */
     Htab *ns;
     Htab *dcl;
+    Htab *closure; /* the syms we close over */
     Htab *ty;
 };
 
@@ -205,7 +206,6 @@
 extern Tok *curtok;     /* the last token we tokenized */
 extern int line;        /* the last line number we tokenized */
 extern Node *file;      /* the current file we're compiling */
-extern Stab *curscope;  /* the current scope to insert symbols into */
 extern Type **tytab;    /* type -> type map used by inference. size maintained by type creation code */
 extern int ntypes;
 extern Cstr **cstrtab;  /* int -> cstr map */
@@ -259,6 +259,10 @@
 Stab *getns(Stab *st, Node *n);
 Sym *getdcl(Stab *st, Node *n);
 Type *gettype(Stab *st, Node *n);
+
+Stab *curstab(void);
+void pushstab(Stab *st);
+void popstab(void);
 
 Sym *mksym(int line, Node *name, Type *ty);
 
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -19,6 +19,24 @@
     Type *type;
 };
 
+#define Maxstabdepth 128
+static Stab *stabstk[Maxstabdepth];
+static int stabstkoff;
+Stab *curstab()
+{
+    return stabstk[stabstkoff - 1];
+}
+
+void pushstab(Stab *st)
+{
+    stabstk[stabstkoff++] = st;
+}
+
+void popstab(void)
+{
+    stabstkoff--;
+}
+
 
 static char *name(Node *n)
 {