ref: 789d36d2bbe0b625a8cf37ccb1e4c04b2a8531dd
parent: aeb2cf580363be7a37cf0de4c4de6b7a6a93fc9c
author: Ori Bernstein <[email protected]>
date: Tue May 15 20:20:47 EDT 2012
Parse type params We don't deal with them correctly yet in the type inference stage, but they get parsed correctly. We also don't restrict them to generic contexts yet.
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -86,6 +86,7 @@
%token<tok> Tenum /* enum */
%token<tok> Tstruct /* struct */
%token<tok> Tunion /* union */
+%token<tok> Ttyparam /* @typename */
%token<tok> Tconst /* const */
%token<tok> Tvar /* var */
@@ -95,6 +96,7 @@
%token<tok> Texport /* export */
%token<tok> Tprotect /* protect */
+
%token<tok> Tellipsis /* ... */
%token<tok> Tendln /* ; or \n */
%token<tok> Tendblk /* ;; */
@@ -112,6 +114,7 @@
%start module
%type <ty> type structdef uniondef enumdef compoundtype functype funcsig
+%type <ty> generictype
%type <tok> asnop cmpop addop mulop shiftop
@@ -246,6 +249,12 @@
| uniondef
| enumdef
| compoundtype
+ | generictype
+ ;
+
+generictype
+ : Ttyparam {$$ = mktyparam($1->line, $1->str);}
+ /* FIXME: allow constrained typarmas */
;
compoundtype
--- a/parse/tok.c
+++ b/parse/tok.c
@@ -165,13 +165,11 @@
return Tident;
}
-static Tok *kwident(void)
+static int identstr(char *buf, size_t sz)
{
- char buf[1024];
- char c;
int i;
- Tok *t;
-
+ char c;
+
i = 0;
for (c = peek(); i < 1023 && identchar(c); c = peek()) {
next();
@@ -178,6 +176,16 @@
buf[i++] = c;
}
buf[i] = '\0';
+ return i;
+}
+
+static Tok *kwident(void)
+{
+ char buf[1024];
+ Tok *t;
+
+ if (!identstr(buf, sizeof buf))
+ return NULL;
t = mktok(kwd(buf));
t->str = strdup(buf);
return t;
@@ -428,6 +436,21 @@
return t;
}
+static Tok *typaram()
+{
+ Tok *t;
+ char buf[1024];
+
+ t = NULL;
+ if (!match('@'))
+ return NULL;
+ if (!identstr(buf, 1024))
+ return NULL;
+ t = mktok(Ttyparam);
+ t->str = strdup(buf);
+ return t;
+}
+
static Tok *toknext()
{
Tok *t;
@@ -449,6 +472,8 @@
t = charlit();
} else if (isdigit(c)) {
t = numlit();
+ } else if (c == '@') {
+ t = typaram();
} else {
t = oper();
}
--- a/parse/type.c
+++ b/parse/type.c
@@ -99,7 +99,7 @@
{
Type *t;
- t = mkty(line, Tyvar);
+ t = mkty(line, Typaram);
t->pname = strdup(name);
return t;
}