ref: c8c925b99018ddf8cfabcbabbdf69c6dc8de67a1
parent: 49a8bf1bb3dde551d210abe1a659af99cc46a783
author: Ori Bernstein <[email protected]>
date: Sun Nov 20 18:03:45 EST 2011
We don't error on package specs now
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -113,6 +113,8 @@
%type <tok> asnop cmpop addop mulop shiftop
+%type <tydef> tydef
+
%type <node> exprln retexpr expr atomicexpr literal asnexpr lorexpr landexpr borexpr
%type <node> bandexpr cmpexpr addexpr mulexpr shiftexpr prefixexpr postfixexpr
%type <node> funclit arraylit name block blockbody stmt label use
@@ -119,7 +121,7 @@
%type <node> decl declbody declcore structelt enumelt unionelt
%type <node> ifstmt forstmt whilestmt elifs optexprln
-%type <nodelist> arglist argdefs structbody enumbody unionbody params
+%type <nodelist> arglist argdefs structbody enumbody unionbody params
%union {
struct {
@@ -132,6 +134,11 @@
Type **types;
size_t ntypes;
} tylist;
+ struct { /* FIXME: unused */
+ int line;
+ char *name;
+ Type *type;
+ } tydef;
Node *node;
Tok *tok;
Type *ty;
@@ -150,13 +157,12 @@
toplev
: decl
{nlappend(&file->file.stmts, &file->file.nstmts, $1);
- def($1);}
+ def(file->file.globls, $1);}
| use
{nlappend(&file->file.uses, &file->file.nuses, $1);}
| package
- {die("package unimplemented");}
- | typedef
- {die("typedef unimplemented");}
+ | tydef
+ {die("tydef unimplemented");}
| TEndln
;
@@ -180,7 +186,7 @@
{$$ = mkuse($1->line, $2->str, 1);}
;
-package : TPkg TIdent TAsn pkgbody TEndblk {}
+package : TPkg TIdent TAsn pkgbody TEndblk
;
@@ -188,9 +194,9 @@
| pkgbody pkgitem
;
-pkgitem : decl
- | typedef
- | visdef
+pkgitem : decl {def(file->file.globls, $1);}
+ | tydef {deftype(file->file.globls, $1.name, $1.type);}
+ | visdef {die("Unimplemented visdef");}
| TEndln
;
@@ -216,8 +222,14 @@
{$$ = $3; setns($3, $1->str);}
;
-typedef : TType TIdent TAsn type TEndln
+tydef : TType TIdent TAsn type TEndln
+ {$$.line = $1->line;
+ $$.name = $2->str;
+ $$.type = $4;}
| TType TIdent TEndln
+ {$$.line = $1->line;
+ $$.name = $2->str;
+ $$.type = NULL;}
;
type : structdef
--- a/parse/main.c
+++ b/parse/main.c
@@ -46,6 +46,8 @@
for (i = optind; i < argc; i++) {
tokinit(argv[i]);
file = mkfile(argv[i]);
+ file->file.exports = mkstab(NULL);
+ file->file.globls = mkstab(NULL);
yyparse();
dump(file, stdout);
gen();
--- a/parse/node.c
+++ b/parse/node.c
@@ -198,9 +198,23 @@
return n->decl.sym->type;
}
-void def(Node *n)
+void def(Stab *s, Node *n)
{
assert(n->type == Ndecl);
+}
+
+void deftype(Stab *s, char *name, Type *ty)
+{
+ assert(name != NULL);
+}
+
+Stab *mkstab(Stab *super)
+{
+ Stab *st;
+
+ st = zalloc(sizeof(Stab));
+ st->super = super;
+ return st;
}
void setns(Node *n, char *name)
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -48,6 +48,11 @@
};
struct Stab {
+ Stab *super;
+ char *name;
+
+ /* Contents of stab.
+ * Types and values are in separate namespaces. */
int ntypes;
Type **types;
int nsyms;
@@ -102,6 +107,7 @@
size_t nstmts;
Node **stmts;
Stab *globls;
+ Stab *exports;
} file;
struct {
@@ -197,7 +203,7 @@
int yyparse(void);
/* stab creation */
-Stab *mkstab();
+Stab *mkstab(Stab *super);
Stab *stput(Sym *decl);
Sym *stget(char *name);
Sym *mksym(int line, Node *name, Type *ty);
@@ -249,7 +255,8 @@
Type *decltype(Node *n);
void addstmt(Node *file, Node *stmt);
void setns(Node *n, char *name);
-void def(Node *n);
+void def(Stab *s, Node *n);
+void deftype(Stab *s, char *name, Type *t);
/* usefiles */
void readuse(Node *use, Stab *into);