summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2013-06-08 12:47:09 -0400
committerOri Bernstein <ori@eigenstate.org>2013-06-08 12:47:09 -0400
commitfbbdeabda88a0262e59d68fcf5227c165d673afe (patch)
treea42bc809ce519df0724cc4e31768b4f6f989151c
parente119045a1d483c7dc7808c140a3659e41b9d160b (diff)
downloadmc-struct-lit.tar.gz
Add support for compiling struct literals.struct-lit
-rw-r--r--6/isel.c13
-rw-r--r--parse/gram.y4
-rw-r--r--parse/infer.c1
-rw-r--r--test/structlit.myr2
4 files changed, 14 insertions, 6 deletions
diff --git a/6/isel.c b/6/isel.c
index 8d5d736..74a7d30 100644
--- a/6/isel.c
+++ b/6/isel.c
@@ -877,6 +877,7 @@ static void writelit(FILE *fd, Node *v, size_t sz)
{
char lbl[128];
size_t i;
+ Node *val;
char *intsz[] = {
[1] = ".byte",
[2] = ".short",
@@ -896,12 +897,16 @@ static void writelit(FILE *fd, Node *v, size_t sz)
writeblob(fd, v->lit.strval, strlen(v->lit.strval));
break;
case Larray:
- for (i = 0; i < v->lit.nelt; i++)
- writelit(fd, v->lit.seqval[i]->expr.args[0], size(v->lit.seqval[i]));
+ for (i = 0; i < v->lit.nelt; i++) {
+ val = v->lit.seqval[i]->idxinit.init;
+ writelit(fd, val->expr.args[0], size(val));
+ }
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]));
+ for (i = 0; i < v->lit.nelt; i++) {
+ val = v->lit.seqval[i]->idxinit.init;
+ writelit(fd, val->expr.args[0], size(val));
+ }
break;
case Lfunc:
die("Generating this shit ain't ready yet ");
diff --git a/parse/gram.y b/parse/gram.y
index 0dd20e0..173d18a 100644
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -622,9 +622,9 @@ arrayelts
: /* empty */
| arrayelt
{$$.nl = NULL; $$.nn = 0;
- lappend(&$$.nl, &$$.nn, $1);}
+ lappend(&$$.nl, &$$.nn, mkidxinit($1->line, mkint($1->line, 0), $1));}
| arrayelts Tcomma arrayelt
- {lappend(&$$.nl, &$$.nn, $3);}
+ {lappend(&$$.nl, &$$.nn, mkidxinit($3->line, mkint($3->line, $$.nn), $3));}
arrayelt: endlns expr endlns {$$ = $2;}
;
diff --git a/parse/infer.c b/parse/infer.c
index 34bdad2..0febc1c 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -339,6 +339,7 @@ static Type *type(Inferstate *st, Node *n)
case Nexpr: t = n->expr.type; break;
case Ndecl: t = decltype(n); break;
case Nfunc: t = n->func.type; break;
+ case Nidxinit: t = type(st, n->idxinit.init); break;
default:
t = NULL;
die("untypeable node %s", nodestr(n->type));
diff --git a/test/structlit.myr b/test/structlit.myr
index 8f8cb9b..3e6a77e 100644
--- a/test/structlit.myr
+++ b/test/structlit.myr
@@ -1,3 +1,5 @@
+use std
+
/* checks that we can create struct literals with named initializers.
exits with 42. */
type t = struct