summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2013-06-05 09:02:47 -0400
committerOri Bernstein <ori@eigenstate.org>2013-06-07 20:51:29 -0400
commit6586d3b6c2dad1ed0e1d3c798a42660f4c17d456 (patch)
tree8bbbffbe145effda006dee247a5dfbe52807a054
parent954ff1977d4345d53a0e1996c7c3b39c51a2d470 (diff)
downloadmc-6586d3b6c2dad1ed0e1d3c798a42660f4c17d456.tar.gz
Parses struct elements, but doesn't compile them.
-rw-r--r--6/isel.c6
-rw-r--r--6/simp.c2
-rw-r--r--parse/dump.c9
-rw-r--r--parse/gram.y38
-rw-r--r--parse/infer.c25
-rw-r--r--parse/lits.def3
-rw-r--r--parse/node.c16
-rw-r--r--parse/parse.h3
-rw-r--r--parse/specialize.c13
-rw-r--r--parse/use.c18
-rw-r--r--test/structlit.myr2
11 files changed, 103 insertions, 32 deletions
diff --git a/6/isel.c b/6/isel.c
index 6629273..8d5d736 100644
--- a/6/isel.c
+++ b/6/isel.c
@@ -895,7 +895,11 @@ static void writelit(FILE *fd, Node *v, size_t sz)
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;
diff --git a/6/simp.c b/6/simp.c
index 3b80cda..f1ba517 100644
--- a/6/simp.c
+++ b/6/simp.c
@@ -1158,7 +1158,7 @@ static Node *rval(Simp *s, Node *n, Node *dst)
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:
diff --git a/parse/dump.c b/parse/dump.c
index 9f72761..56536b5 100644
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -190,8 +190,13 @@ static void outnode(Node *n, FILE *fd, int depth)
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;
diff --git a/parse/gram.y b/parse/gram.y
index 3ff1d3b..98192e1 100644
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -132,7 +132,7 @@ static void constrainwith(Type *t, char *str);
%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 @@ static void constrainwith(Type *t, char *str);
%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 @@ structdef
;
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,23 +612,35 @@ params : declcore
{$$.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
;
diff --git a/parse/infer.c b/parse/infer.c
index e05d7e8..8fb783f 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -305,7 +305,8 @@ static Type *littype(Node *n)
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,7 +739,16 @@ static void checkns(Inferstate *st, Node *n, Node **ret)
*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;
@@ -980,8 +990,9 @@ static void inferexpr(Inferstate *st, Node *n, Type *ret, int *sawret)
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 @@ static void typesub(Inferstate *st, Node *n)
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;
diff --git a/parse/lits.def b/parse/lits.def
index ef364bd..0e9e6b9 100644
--- a/parse/lits.def
+++ b/parse/lits.def
@@ -4,5 +4,6 @@ L(Lint)
L(Lflt)
L(Lstr)
L(Lfunc)
-L(Lseq)
+L(Lstruct)
+L(Larray)
L(Llbl)
diff --git a/parse/node.c b/parse/node.c
index a13a737..8aeeefe 100644
--- a/parse/node.c
+++ b/parse/node.c
@@ -232,12 +232,24 @@ Node *mkfloat(int line, double val)
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;
diff --git a/parse/parse.h b/parse/parse.h
index 614ed63..9738601 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -394,7 +394,8 @@ Node *mkchar(int line, uint32_t val);
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);
diff --git a/parse/specialize.c b/parse/specialize.c
index 3cb332b..1e23065 100644
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -145,7 +145,11 @@ static void fixup(Node *n)
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;
@@ -237,7 +241,12 @@ static Node *specializenode(Node *n, Htab *tsmap)
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);
diff --git a/parse/use.c b/parse/use.c
index af28724..650cf37 100644
--- a/parse/use.c
+++ b/parse/use.c
@@ -355,7 +355,11 @@ static void pickle(Node *n, FILE *fd)
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;
@@ -474,7 +478,11 @@ static Node *unpickle(FILE *fd)
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;
@@ -737,7 +745,11 @@ static void nodetag(Node *n)
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;
diff --git a/test/structlit.myr b/test/structlit.myr
index f197a49..db7be47 100644
--- 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 = {