shithub: mc

Download patch

ref: 852fa8ce58ef311f1faff678e89e20a45120e8e4
parent: 4a0be740af3f660508516835969ee345836ca6ec
author: Ori Bernstein <[email protected]>
date: Wed Jun 5 05:02:47 EDT 2013

Parses struct elements, but doesn't compile them.

--- a/6/isel.c
+++ b/6/isel.c
@@ -893,7 +893,11 @@
                         fprintf(fd, "%s:\n", lbl);
                         writeblob(fd, v->lit.strval, strlen(v->lit.strval));
                         break;
-        case Lseq:
+        case Larray:
+            for (i = 0; i < v->lit.nelt; i++)
+                writelit(fd, v->lit.seqval[i]->expr.args[0], size(v->lit.seqval[i]));
+            break;
+        case Lstruct:
             for (i = 0; i < v->lit.nelt; i++)
                 writelit(fd, v->lit.seqval[i]->expr.args[0], size(v->lit.seqval[i]));
             break;
--- a/6/simp.c
+++ b/6/simp.c
@@ -1158,7 +1158,7 @@
                 case Lchr: case Lbool: case Lint: case Llbl:
                     r = n;
                     break;
-                case Lstr: case Lseq: case Lflt:
+                case Lstr: case Lstruct: case Larray: case Lflt:
                     r = simplit(s, n, &s->blobs, &s->nblobs);
                     break;
                 case Lfunc:
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -190,8 +190,13 @@
                     fprintf(fd, " Lfunc\n");
                     outnode(n->lit.fnval, fd, depth+1);
                     break;
-                case Lseq:
-                    fprintf(fd, " Lseq\n");
+                case Larray:
+                    fprintf(fd, " Larray\n");
+                    for (i = 0; i < n->lit.nelt; i++)
+                        outnode(n->lit.seqval[i], fd, depth+1);
+                    break;
+                case Lstruct:
+                    fprintf(fd, " Lstruct\n");
                     for (i = 0; i < n->lit.nelt; i++)
                         outnode(n->lit.seqval[i], fd, depth+1);
                     break;
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -132,7 +132,7 @@
 %type <node> exprln retexpr goto expr atomicexpr littok literal asnexpr lorexpr landexpr borexpr
 %type <node> bandexpr cmpexpr unionexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
 %type <node> funclit seqlit name block stmt label use
-%type <node> decl declbody declcore structelt seqelt tuphead
+%type <node> decl declbody declcore structent arrayelt structelt tuphead
 %type <node> ifstmt forstmt whilestmt matchstmt elifs optexprln optexpr
 %type <node> pat unionpat match
 %type <node> castexpr
@@ -140,7 +140,7 @@
 %type <node> blkbody
 
 %type <nodelist> arglist argdefs params matches
-%type <nodelist> structbody seqbody tupbody tuprest
+%type <nodelist> structbody structelts arrayelts tupbody tuprest
 %type <uconlist> unionbody
 
 %union {
@@ -371,13 +371,13 @@
         ;
 
 structbody
-        : structelt
+        : structent
             {if ($1) {$$.nl = NULL; $$.nn = 0; lappend(&$$.nl, &$$.nn, $1);}}
-        | structbody structelt
+        | structbody structent
             {if ($2) {lappend(&$$.nl, &$$.nn, $2);}}
         ;
 
-structelt
+structent
         : declcore Tendln
             {$$ = $1;}
         | visdef Tendln
@@ -612,22 +612,34 @@
             {$$.nl = NULL; $$.nn = 0;}
         ;
 
-seqlit  : Tosqbrac seqbody Tcsqbrac
-            {$$ = mkseq($1->line, $2.nl, $2.nn);}
+seqlit  : Tosqbrac arrayelts Tcsqbrac
+            {$$ = mkarray($1->line, $2.nl, $2.nn);}
+        | Tosqbrac structelts Tcsqbrac
+            {$$ = mkstruct($1->line, $2.nl, $2.nn);}
         ;
 
-seqbody : /* empty */ {$$.nl = NULL; $$.nn = 0;}
-        | seqelt
+arrayelts
+        : /* empty */
+        | arrayelt
             {$$.nl = NULL; $$.nn = 0;
              lappend(&$$.nl, &$$.nn, $1);}
-        | seqbody Tcomma seqelt
+        | arrayelts Tcomma arrayelt
              {lappend(&$$.nl, &$$.nn, $3);}
+
+arrayelt: endlns expr endlns {$$ = $2;}
         ;
 
-seqelt  : Tdot Tident Tasn expr
-            {die("Unimplemented struct member init");}
-        | endlns expr endlns{$$ = $2;}
+structelts
+        : /* empty */ {$$.nl = NULL; $$.nn = 0;}
+        | structelt
+            {$$.nl = NULL; $$.nn = 0;
+             lappend(&$$.nl, &$$.nn, $1);}
+        | structelts Tcomma structelt
+             {lappend(&$$.nl, &$$.nn, $3);}
         ;
+
+structelt: endlns Tdot Tident Tasn expr endlns {$$ = $5;}
+         ;
 
 endlns  : /* none */
         | endlns Tendln
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -305,7 +305,8 @@
         case Lstr:      return mktyslice(n->line, mktype(n->line, Tybyte));     break;
         case Llbl:      return mktyptr(n->line, mktype(n->line, Tyvoid));       break;
         case Lfunc:     return n->lit.fnval->func.type;                         break;
-        case Lseq:      return NULL; break;
+        case Lstruct:   return NULL; break;
+        case Larray:    return NULL; break;
     };
     die("Bad lit type %d", n->lit.littype);
     return NULL;
@@ -738,9 +739,18 @@
     *ret = var;
 }
 
-static void inferseq(Inferstate *st, Node *n)
+static void inferstruct(Inferstate *st, Node *n)
 {
     size_t i;
+
+    for (i = 0; i < n->lit.nelt; i++)
+        infernode(st, n->lit.seqval[i], NULL, NULL);
+    die("Don't know what to do with struct lits yet, when it comes to inference");
+}
+
+static void inferarray(Inferstate *st, Node *n)
+{
+    size_t i;
     Type *t;
     Node *len;
 
@@ -980,8 +990,9 @@
         case Olit:      /* <lit>:@a::tyclass -> @a */
             switch (args[0]->lit.littype) {
                 case Lfunc:     infernode(st, args[0]->lit.fnval, NULL, NULL); break;
-                case Lseq:      inferseq(st, args[0]);                         break;
-                default:        /* pass */                                 break;
+                case Larray:    inferarray(st, args[0]);                       break;
+                case Lstruct:   inferstruct(st, args[0]);                      break;
+                default:        /* pass */                                     break;
             }
             settype(st, n, type(st, args[0]));
             break;
@@ -1329,7 +1340,11 @@
             settype(st, n, tyfix(st, n, type(st, n)));
             switch (n->lit.littype) {
                 case Lfunc:     typesub(st, n->lit.fnval); break;
-                case Lseq:
+                case Larray:
+                    for (i = 0; i < n->lit.nelt; i++)
+                        typesub(st, n->lit.seqval[i]);
+                    break;
+                case Lstruct:
                     for (i = 0; i < n->lit.nelt; i++)
                         typesub(st, n->lit.seqval[i]);
                     break;
--- a/parse/lits.def
+++ b/parse/lits.def
@@ -4,5 +4,6 @@
 L(Lflt)
 L(Lstr)
 L(Lfunc)
-L(Lseq)
+L(Lstruct)
+L(Larray)
 L(Llbl)
--- a/parse/node.c
+++ b/parse/node.c
@@ -232,12 +232,24 @@
     return n;
 }
 
-Node *mkseq(int line, Node **vals, size_t nvals)
+Node *mkarray(int line, Node **vals, size_t nvals)
 {
     Node *n;
 
     n = mknode(line, Nlit);
-    n->lit.littype = Lseq;
+    n->lit.littype = Larray;
+    n->lit.nelt = nvals;
+    n->lit.seqval = vals;
+
+    return n;
+}
+
+Node *mkstruct(int line, Node **vals, size_t nvals)
+{
+    Node *n;
+
+    n = mknode(line, Nlit);
+    n->lit.littype = Lstruct;
     n->lit.nelt = nvals;
     n->lit.seqval = vals;
 
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -394,7 +394,8 @@
 Node *mkstr(int line, char *s);
 Node *mkfloat(int line, double flt);
 Node *mkfunc(int line, Node **args, size_t nargs, Type *ret, Node *body);
-Node *mkseq(int line, Node **vals, size_t nvals);
+Node *mkstruct(int line, Node **vals, size_t nvals);
+Node *mkarray(int line, Node **vals, size_t nvals);
 Node *mktuple(int line, Node **vals, size_t nvals);
 Node *mkname(int line, char *name);
 Node *mknsname(int line, char *ns, char *name);
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -145,10 +145,14 @@
         case Nlit:
             switch (n->lit.littype) {
                 case Lfunc:     fixup(n->lit.fnval);          break;
-                case Lseq:
+                case Larray:
                     for (i = 0; i < n->lit.nelt; i++)
                         fixup(n->lit.seqval[i]);
                     break;
+                case Lstruct:
+                    for (i = 0; i < n->lit.nelt; i++)
+                        fixup(n->lit.seqval[i]);
+                    break;
                 case Lchr: case Lint: case Lflt:
                 case Lstr: case Llbl: case Lbool:
                     break;
@@ -237,7 +241,12 @@
                 case Llbl:      r->lit.lblval = n->lit.lblval;       break;
                 case Lbool:     r->lit.boolval = n->lit.boolval;     break;
                 case Lfunc:     r->lit.fnval = specializenode(n->lit.fnval, tsmap);       break;
-                case Lseq:
+                case Larray:
+                    r->lit.seqval = xalloc(n->lit.nelt * sizeof(Node*));
+                    for (i = 0; i < n->lit.nelt; i++)
+                        r->lit.seqval[i] = specializenode(n->lit.seqval[i], tsmap);
+                    break;
+                case Lstruct:
                     r->lit.seqval = xalloc(n->lit.nelt * sizeof(Node*));
                     for (i = 0; i < n->lit.nelt; i++)
                         r->lit.seqval[i] = specializenode(n->lit.seqval[i], tsmap);
--- a/parse/use.c
+++ b/parse/use.c
@@ -355,10 +355,14 @@
                 case Llbl:      wrstr(fd, n->lit.lblval);       break;
                 case Lbool:     wrbool(fd, n->lit.boolval);     break;
                 case Lfunc:     pickle(n->lit.fnval, fd);       break;
-                case Lseq:
+                case Larray:
                     for (i = 0; i < n->lit.nelt; i++)
                         pickle(n->lit.seqval[i], fd);
                     break;
+                case Lstruct:
+                    for (i = 0; i < n->lit.nelt; i++)
+                        pickle(n->lit.seqval[i], fd);
+                    break;
             }
             break;
         case Nloopstmt:
@@ -474,10 +478,14 @@
                 case Llbl:      n->lit.lblval = rdstr(fd);       break;
                 case Lbool:     n->lit.boolval = rdbool(fd);     break;
                 case Lfunc:     n->lit.fnval = unpickle(fd);       break;
-                case Lseq:
+                case Larray:
                     for (i = 0; i < n->lit.nelt; i++)
                         n->lit.seqval[i] = unpickle(fd);
                     break;
+                case Lstruct:
+                    for (i = 0; i < n->lit.nelt; i++)
+                        n->lit.seqval[i] = unpickle(fd);
+                    break;
             }
             break;
         case Nloopstmt:
@@ -737,7 +745,11 @@
             taghidden(n->lit.type);
             switch (n->lit.littype) {
                 case Lfunc: nodetag(n->lit.fnval); break;
-                case Lseq:
+                case Larray:
+                    for (i = 0; i < n->lit.nelt; i++)
+                        nodetag(n->lit.seqval[i]);
+                    break;
+                case Lstruct:
                     for (i = 0; i < n->lit.nelt; i++)
                         nodetag(n->lit.seqval[i]);
                     break;
--- a/test/structlit.myr
+++ b/test/structlit.myr
@@ -3,7 +3,7 @@
 type t = struct
 	a	: int
 	b	: char
-	c	: char[,]
+	c	: char[:]
 ;;
 
 const main = {