summaryrefslogtreecommitdiff
path: root/parse
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-12-14 23:27:06 -0800
committerOri Bernstein <ori@eigenstate.org>2015-12-14 23:27:06 -0800
commitf5a1454387098fa3abb7bf95f4123e1fac85cb7d (patch)
treec5410430516ebf119d8139c8b599732cbd545d51 /parse
parent607b2662bc1093833bfea0c49236f098271cf348 (diff)
downloadmc-f5a1454387098fa3abb7bf95f4123e1fac85cb7d.tar.gz
Initial support for void as an expression.
Diffstat (limited to 'parse')
-rw-r--r--parse/dump.c1
-rw-r--r--parse/gram.y9
-rw-r--r--parse/infer.c4
-rw-r--r--parse/lits.def1
-rw-r--r--parse/node.c10
-rw-r--r--parse/parse.h1
-rw-r--r--parse/specialize.c14
-rw-r--r--parse/tok.c1
-rw-r--r--parse/use.c2
9 files changed, 31 insertions, 12 deletions
diff --git a/parse/dump.c b/parse/dump.c
index 9651b60..c9eb139 100644
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -209,6 +209,7 @@ static void outnode(Node *n, FILE *fd, int depth)
break;
case Nlit:
switch (n->lit.littype) {
+ case Lvoid: fprintf(fd, " Lvoid\n"); break;
case Lchr: fprintf(fd, " Lchr %c\n", n->lit.chrval); break;
case Lbool: fprintf(fd, " Lbool %s\n", n->lit.boolval ? "true" : "false"); break;
case Lint: fprintf(fd, " Lint %llu\n", n->lit.intval); break;
diff --git a/parse/gram.y b/parse/gram.y
index 8aaa385..7e930ef 100644
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -97,6 +97,7 @@ static void setupinit(Node *n);
%token<tok> Tfloatlit
%token<tok> Tchrlit
%token<tok> Tboollit
+%token<tok> Tvoidlit
%token<tok> Ttrait /* trait */
%token<tok> Timpl /* trait */
@@ -474,10 +475,11 @@ compoundtype
| type Tosqbrac Tcolon Tcsqbrac {$$ = mktyslice($2->loc, $1);}
| type Tosqbrac expr Tcsqbrac {$$ = mktyarray($2->loc, $1, $3);}
| type Tosqbrac Tellipsis Tcsqbrac {$$ = mktyarray($2->loc, $1, NULL);}
- | type Tderef {$$ = mktyptr($2->loc, $1);}
- | Tat Tident {$$ = mktyparam($1->loc, $2->id);}
- | name {$$ = mktyunres($1->loc, $1, NULL, 0);}
| name Toparen typelist Tcparen {$$ = mktyunres($1->loc, $1, $3.types, $3.ntypes);}
+ | type Tderef {$$ = mktyptr($2->loc, $1);}
+ | Tat Tident {$$ = mktyparam($1->loc, $2->id);}
+ | Tvoidlit {$$ = mktyunres($1->loc, mkname($1->loc, $1->id), NULL, 0);}
+ | name {$$ = mktyunres($1->loc, $1, NULL, 0);}
;
functype: Toparen funcsig Tcparen {$$ = $2;}
@@ -748,6 +750,7 @@ littok : Tstrlit {$$ = mkstr($1->loc, $1->strval);}
| Tchrlit {$$ = mkchar($1->loc, $1->chrval);}
| Tfloatlit {$$ = mkfloat($1->loc, $1->fltval);}
| Tboollit {$$ = mkbool($1->loc, !strcmp($1->id, "true"));}
+ | Tvoidlit {$$ = mkvoid($1->loc);}
| Tintlit {
$$ = mkint($1->loc, $1->intval);
if ($1->inttype)
diff --git a/parse/infer.c b/parse/infer.c
index 3b5e413..6894a1c 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -526,6 +526,7 @@ static Type *littype(Node *n)
t = NULL;
if (!n->lit.type) {
switch (n->lit.littype) {
+ case Lvoid: t = mktype(n->loc, Tyvoid); break;
case Lchr: t = mktype(n->loc, Tychar); break;
case Lbool: t = mktype(n->loc, Tybool); break;
case Lint: t = mktylike(n->loc, Tyint); break;
@@ -999,9 +1000,6 @@ static void unifycall(Inferstate *st, Node *n)
fatal(n, "calling uncallable type %s", tystr(ft));
}
/* first arg: function itself */
- for (i = 1; i < n->expr.nargs; i++)
- if (exprtype(n->expr.args[i])->type == Tyvoid)
- fatal(n, "void passed where value expected, near %s", ctxstr(st, n));
for (i = 1; i < n->expr.nargs; i++) {
if (i == ft->nsub)
fatal(n, "%s arity mismatch (expected %zd args, got %zd)",
diff --git a/parse/lits.def b/parse/lits.def
index 2e5342f..53725ff 100644
--- a/parse/lits.def
+++ b/parse/lits.def
@@ -5,3 +5,4 @@ L(Lflt)
L(Lstr)
L(Lfunc)
L(Llbl)
+L(Lvoid)
diff --git a/parse/node.c b/parse/node.c
index 62be764..8d0decf 100644
--- a/parse/node.c
+++ b/parse/node.c
@@ -374,6 +374,15 @@ Node *mkbool(Srcloc loc, int val)
return n;
}
+Node *mkvoid(Srcloc loc)
+{
+ Node *n;
+
+ n = mknode(loc, Nlit);
+ n->lit.littype = Lvoid;
+ return n;
+}
+
char *declname(Node *n)
{
Node *name;
@@ -413,6 +422,7 @@ int liteq(Node *a, Node *b)
if (!tyeq(a->lit.type, b->lit.type))
return 0;
switch (a->lit.littype) {
+ case Lvoid: return 1;
case Lchr: return a->lit.chrval == b->lit.chrval;
case Lbool: return a->lit.boolval == b->lit.boolval;
case Lint: return a->lit.intval == b->lit.intval;
diff --git a/parse/parse.h b/parse/parse.h
index ab71500..f94635e 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -563,6 +563,7 @@ Node *mkimplstmt(Srcloc l, Node *name, Type *type, Node **impls, size_t nimpls);
Node *mkintlit(Srcloc l, uvlong val);
Node *mkidxinit(Srcloc l, Node *idx, Node *init);
+Node *mkvoid(Srcloc loc);
Node *mkbool(Srcloc l, int val);
Node *mkint(Srcloc l, uint64_t val);
Node *mkchar(Srcloc l, uint32_t val);
diff --git a/parse/specialize.c b/parse/specialize.c
index 1414119..7a9eb70 100644
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -180,12 +180,13 @@ static void fixup(Node *n)
case Nlit:
switch (n->lit.littype) {
case Lfunc: fixup(n->lit.fnval); break;
- case Lchr:
- case Lint:
- case Lflt:
- case Lstr:
- case Llbl:
- case Lbool: break;
+ case Lvoid: break;
+ case Lchr: break;
+ case Lint: break;
+ case Lflt: break;
+ case Lstr: break;
+ case Llbl: break;
+ case Lbool: break;
}
break;
case Nifstmt:
@@ -273,6 +274,7 @@ static Node *specializenode(Node *n, Htab *tsmap)
r->lit.littype = n->lit.littype;
r->lit.type = tysubst(n->expr.type, tsmap);
switch (n->lit.littype) {
+ case Lvoid: break;
case Lchr: r->lit.chrval = n->lit.chrval; break;
case Lint: r->lit.intval = n->lit.intval; break;
case Lflt: r->lit.fltval = n->lit.fltval; break;
diff --git a/parse/tok.c b/parse/tok.c
index 068af8e..fd3597c 100644
--- a/parse/tok.c
+++ b/parse/tok.c
@@ -191,6 +191,7 @@ static int kwd(char *s)
{"union", Tunion},
{"use", Tuse},
{"var", Tvar},
+ {"void", Tvoidlit},
{"while", Twhile},
};
diff --git a/parse/use.c b/parse/use.c
index 88ac64a..5c5e229 100644
--- a/parse/use.c
+++ b/parse/use.c
@@ -469,6 +469,7 @@ static void pickle(FILE *fd, Node *n)
wrtype(fd, n->lit.type);
wrint(fd, n->lit.nelt);
switch (n->lit.littype) {
+ case Lvoid: break;
case Lchr: wrint(fd, n->lit.chrval); break;
case Lint: wrint(fd, n->lit.intval); break;
case Lflt: wrflt(fd, n->lit.fltval); break;
@@ -598,6 +599,7 @@ static Node *unpickle(FILE *fd)
rdtype(fd, &n->lit.type);
n->lit.nelt = rdint(fd);
switch (n->lit.littype) {
+ case Lvoid: break;
case Lchr: n->lit.chrval = rdint(fd); break;
case Lint: n->lit.intval = rdint(fd); break;
case Lflt: n->lit.fltval = rdflt(fd); break;