shithub: mc

Download patch

ref: 9954d66557d7cd1f604bf662547404ee534d8915
parent: 672e564bc299cc67682acec42c31cae4e308c6a8
author: Ori Bernstein <[email protected]>
date: Mon Jun 18 22:39:24 EDT 2012

A simple libray-based hello world demo compiles.

    It doesn't work, but it compiles.

--- a/8/reduce.c
+++ b/8/reduce.c
@@ -346,10 +346,10 @@
 
     n = mkname(lit->line, genlblstr(lbl, 128));
     t = mkdecl(lit->line, n, lit->expr.type);
-    r = mkexpr(lit->line, Ovar, t, NULL);
-    t->decl.init = lit;
+    r = mkexpr(lit->line, Ovar, n, NULL);
+    r->expr.type = lit->expr.type;
     r->expr.did = t->decl.did;
-    r->expr.type = t->expr.type;
+    t->decl.init = lit;
     htput(s->globls, t, strdup(lbl));
     lappend(&s->blobs, &s->nblobs, t);
     return r;
@@ -857,7 +857,7 @@
         }
     } else {
         if (dcl->decl.init && exprop(dcl->decl.init) == Olit)
-            lappend(blob, nblob, dcl);
+            lappend(&s.blobs, &s.nblobs, dcl);
         else
             die("We don't lower globls with nonlit inits yet...");
     }
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -206,10 +206,11 @@
         | pkgbody pkgitem
         ;
 
-pkgitem : decl {putdcl(file->file.exports, $1);}
-        | tydef {puttype(file->file.exports, 
-                         mkname($1.line, $1.name),
-                         $1.type);}
+pkgitem : decl 
+            {putdcl(file->file.exports, $1);
+            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");}
         | Tendln
         ;
@@ -281,6 +282,10 @@
              $$.nn = 0; lappend(&$$.nl, &$$.nn, $1);}
         | argdefs Tcomma declcore
             {lappend(&$$.nl, &$$.nn, $3);}
+        | /* empty */
+            {$$.line = line;
+             $$.nl = NULL;
+             $$.nn = 0;}
         ;
 
 structdef
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -717,8 +717,9 @@
     Stab *exports, *globls;
     size_t i, nk;
     void **k;
-    Node *n;
-    Type *ty, *t;
+    /* local, global version */
+    Node *nl, *ng;
+    Type *tl, *tg;
 
     exports = file->file.exports;
     globls = file->file.globls;
@@ -726,19 +727,20 @@
     pushstab(globls);
     k = htkeys(exports->ty, &nk);
     for (i = 0; i < nk; i++) {
-        ty = gettype(exports, k[i]);
-        n = k[i];
-        if (ty) {
-            if (!gettype(globls, n))
-                puttype(globls, n, ty);
+        tl = gettype(exports, k[i]);
+        nl = k[i];
+        if (tl) {
+            tg = gettype(globls, nl);
+            if (!tg)
+                puttype(globls, nl, tl);
             else
-                unify(file, gettype(globls, n), ty);
+                fatal(nl->line, "Exported type %s double-declared on line %d", namestr(nl), tg->line);
         } else {
-            t = gettype(globls, n);
-            if (t) 
-                updatetype(exports, n, tf(t));
+            tg = gettype(globls, nl);
+            if (tg) 
+                updatetype(exports, nl, tf(tg));
             else
-                fatal(n->line, "Exported type %s not declared", namestr(n));
+                fatal(nl->line, "Exported type %s not declared", namestr(nl));
         }
     }
     free(k);
@@ -745,11 +747,16 @@
 
     k = htkeys(exports->dcl, &nk);
     for (i = 0; i < nk; i++) {
-        n = getdcl(exports, k[i]);
-        if (!getdcl(globls, k[i]))
-            putdcl(globls, n);
+        nl = getdcl(exports, k[i]);
+        ng = getdcl(globls, k[i]);
+        /* if an export has an initializer, it shouldn't be declared in the
+         * body */
+        if (nl->decl.init && ng)
+            fatal(nl->line, "Export %s double-defined on line %d", ctxstr(nl), ng->line);
+        if (!ng)
+            putdcl(globls, nl);
         else
-            unify(n, type(getdcl(globls, k[i])), type(n));
+            unify(nl, type(ng), type(nl));
     }
     free(k);
     popstab();
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -108,7 +108,7 @@
 
     d = getdcl(st, s->decl.name);
     if (d)
-        fatal(s->line, "%s already declared (line %d", namestr(s->decl.name), d->line);
+        fatal(s->line, "%s already declared (on line %d)", namestr(s->decl.name), d->line);
     if (st->name)
         setns(s->decl.name, namestr(st->name));
     htput(st->dcl, s->decl.name, s);