ref: a14644877bbb7cbe45cbd006f2773a3e4b9008a7
parent: 10c5c3d7f4a76b67a72278b989d4b0cb52e145b3
author: Ori Bernstein <[email protected]>
date: Mon Jan 13 06:39:33 EST 2014
Work towards parsing traits definitions.
--- a/myrtypes/myrtypes.c
+++ b/myrtypes/myrtypes.c
@@ -109,6 +109,9 @@
printf("Body:\n");
dumptypes(n->func.body, indent + 1);
break;
+ case Ntrait:
+ die("Traits not fully implemented\n");
+ break;
case Nname:
break;
case Nnone:
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -226,6 +226,9 @@
fprintf(fd, "%s", n->name.name);
fprintf(fd, ")\n");
break;
+ case Ntrait:
+ fprintf(fd, "Trait definition");
+ break;
case Nnone:
fprintf(stderr, "Nnone not a real node type!");
fprintf(fd, "Nnone\n");
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -93,6 +93,7 @@
%token<tok> Tboollit
%token<tok> Ttrait /* trait */
+%token<tok> Timpl /* trait */
%token<tok> Tstruct /* struct */
%token<tok> Tunion /* union */
%token<tok> Ttyparam /* @typename */
@@ -131,6 +132,7 @@
%type <tok> asnop cmpop addop mulop shiftop optident
%type <tydef> tydef typeid
+%type <node> traitdef
%type <node> exprln retexpr goto expr atomicexpr littok literal asnexpr lorexpr landexpr borexpr
%type <node> bandexpr cmpexpr unionexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
@@ -146,6 +148,7 @@
%type <nodelist> structbody structelts arrayelts
%type <nodelist> tupbody tuprest
%type <nodelist> decl decllist
+%type <nodelist> traitbody
%type <uconlist> unionbody
@@ -198,6 +201,7 @@
| tydef
{puttype(file->file.globls, mkname($1.line, $1.name), $1.type);
installucons(file->file.globls, $1.type);}
+ | traitdef
| /* empty */
;
@@ -270,6 +274,7 @@
}}
| tydef {puttype(file->file.exports, mkname($1.line, $1.name), $1.type);
installucons(file->file.exports, $1.type);}
+ | traitdef
| visdef {die("Unimplemented visdef");}
| /* empty */
;
@@ -294,6 +299,21 @@
| Tident Tdot name
{$$ = $3; setns($3, $1->str);}
;
+
+traitdef: Ttrait Tident generictype Tasn traitbody endlns Tendblk
+ {$$ = mktrait($1->line, mkname($2->line, $2->str), $5.nl, $5.nn, NULL, 0);}
+ ;
+
+traitbody
+ : endlns {$$.nl = NULL; $$.nn = 0;}
+ | traitbody decl endlns
+ {size_t i;
+ $$ = $1;
+ for (i = 0; i < $2.nn; i++)
+ lappend(&$$.nl, &$$.nn, $2.nl[i]);
+ }
+ ;
+
tydef : Ttype typeid Tasn type
{$$ = $2;
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1399,6 +1399,9 @@
inferfunc(st, n);
popstab();
break;
+ case Ntrait:
+ die("Trait inference not yet implemented\n");
+ break;
case Nname:
case Nlit:
case Nuse:
@@ -1713,6 +1716,9 @@
default: break;
}
break;
+ case Ntrait:
+ die("Trait inference not yet implemented\n");
+ break;
case Nname:
case Nuse:
break;
@@ -1820,6 +1826,9 @@
for (i = 0; i < n->func.nargs; i++)
nodetag(st, n->func.args[i], ingeneric);
nodetag(st, n->func.body, ingeneric);
+ break;
+ case Ntrait:
+ die("Trait inference not yet implemented\n");
break;
case Nuse: case Nname:
--- a/parse/node.c
+++ b/parse/node.c
@@ -183,6 +183,19 @@
return n;
}
+Node *mktrait(int line, Node *name, Node **funcs, size_t nfuncs, Node **membs, size_t nmembs)
+{
+ Node *n;
+
+ n = mknode(line, Ntrait);
+ n->trait.name = name;
+ n->trait.funcs = funcs;
+ n->trait.nfuncs = nfuncs;
+ n->trait.membs = membs;
+ n->trait.nmembs = nmembs;
+ return n;
+}
+
Node *mkintlit(int line, uvlong val)
{
return mkexpr(line, Olit, mkint(line, val), NULL);
--- a/parse/nodes.def
+++ b/parse/nodes.def
@@ -12,3 +12,4 @@
N(Nname)
N(Ndecl)
N(Nfunc)
+N(Ntrait)
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -244,15 +244,18 @@
struct {
size_t did;
+ Node *name;
+ Type *type;
+ Node *init;
+ Node **impls;
+ size_t nimpls;
char vis;
char isglobl;
char isconst;
char isgeneric;
+ char istrait;
char isextern;
char ishidden;
- Node *name;
- Type *type;
- Node *init;
} decl;
struct {
@@ -269,6 +272,18 @@
Node **args;
Node *body;
} func;
+
+ struct {
+ Node *name;
+ size_t traitid;
+
+ Node **funcs;
+ size_t nfuncs;
+ Node **membs;
+ size_t nmembs;
+ } trait;
+
+
};
};
@@ -412,6 +427,7 @@
Node *mkmatchstmt(int line, Node *val, Node **matches, size_t nmatches);
Node *mkmatch(int line, Node *pat, Node *body);
Node *mkblock(int line, Stab *scope);
+Node *mktrait(int line, Node *name, Node **funcs, size_t nfuncs, Node **membs, size_t nmembs);
Node *mkintlit(int line, uvlong val);
Node *mkidxinit(int line, Node *idx, Node *init);
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -222,7 +222,7 @@
fixup(n->func.body);
popstab();
break;
- case Nnone: case Nname:
+ case Nnone: case Nname: case Ntrait:
break;
}
}
@@ -339,6 +339,8 @@
r->func.args[i] = specializenode(n->func.args[i], tsmap);
r->func.body = specializenode(n->func.body, tsmap);
popstab();
+ break;
+ case Ntrait:
break;
case Nnone:
die("Nnone should not be seen as node type!");
--- a/parse/tok.c
+++ b/parse/tok.c
@@ -171,6 +171,7 @@
{"generic", Tgeneric},
{"goto", Tgoto},
{"if", Tif},
+ {"impl", Timpl},
{"in", Tin},
{"match", Tmatch},
{"pkg", Tpkg},
--- a/parse/use.c
+++ b/parse/use.c
@@ -461,6 +461,9 @@
pickle(n->func.args[i], fd);
pickle(n->func.body, fd);
break;
+ case Ntrait:
+ die("Ntrait not yet supported!");
+ break;
case Nnone:
die("Nnone should not be seen as node type!");
break;
@@ -592,6 +595,9 @@
n->func.args[i] = unpickle(fd);
n->func.body = unpickle(fd);
popstab();
+ break;
+ case Ntrait:
+ die("Ntrait not yet implemented");
break;
case Nnone:
die("Nnone should not be seen as node type!");