ref: 0c8cf4098baef78d7c3da9d3c029cdce5bd8ebb2
parent: 72abaa282749e37df7831959b1a344664b1827f3
author: Jacob Moody <moody@posixcafe.org>
date: Sat Nov 18 11:31:43 EST 2023
more ast work from ori
--- a/dat.h
+++ b/dat.h
@@ -10,6 +10,8 @@
char linear;
char *name;
Typ *base;
+ Typ **args;
+ int nargs;
Nod *size;
};
@@ -25,12 +27,15 @@
};
enum {
+ Tvoid,
Tarray,
Tslice,
+ Tfunc,
};
enum {
Nexpr,
+ Nif,
Nfor,
Niter,
Nblk,
@@ -37,6 +42,7 @@
Nlit,
Nsym,
Ndcl,
+ Nfunc,
};
enum {
@@ -89,6 +95,11 @@
Nod *body;
} nfor;
struct {
+ Nod *cond;
+ Nod *t;
+ Nod *f;
+ } nif;
+ struct {
vlong val;
} lit;
struct {
@@ -108,6 +119,11 @@
struct {
Nlst body;
} blk;
+ struct {
+ Nlst args;
+ Typ *type;
+ Nod *body;
+ } func;
};
};
--- a/fns.h
+++ b/fns.h
@@ -1,9 +1,15 @@
+void loaduse(char*);
Nlst append(Nlst, Nod*);
Nod* mkexpr(int, Nod*, Nod*);
Nod* mkiter(Nod*, Nod*, Nod*);
Nod* mkfor(Nod*, Nod*, Nod*, Nod*);
+Nod* mkif(Nod*, Nod*, Nod*);
+Nod* mkfunc(char*, Nlst, Typ*, Nlst);
Nod* mklit(vlong);
Nod* mksym(char*);
Nod* mkdecl(char*, Typ*, Nod*);
+Nod* mkblk(Nlst);
+Typ* mktype(int);
Typ* mktyarray(Typ*, Nod*);
Typ* mktyslice(Typ*);
+Typ* mktyfunc(Typ**, int, Typ*);
\ No newline at end of file
--- a/n.y
+++ b/n.y
@@ -10,10 +10,17 @@
int goteof;
int lexline;
+char *modname;
char *lexfile = "<stdin>";
int yyparse(void);
void yyerror(char*);
+void
+loaduse(char*)
+{
+ fprint(2, "not implement: loaduse\n");
+}
+
%}
%union
@@ -32,21 +39,23 @@
%token <ival> NUM
%type<nod> arg expr logexpr shifexpr addexpr mulexpr
-%type<nod> unary sufexpr compexpr
-%type<lst> args
-%type<typ> type
+%type<nod> unary prefexpr sufexpr compexpr top stmt
+%type<nod> decl
+%type<lst> args prog stmts
+%type<typ> type return
%%
prog
-: prog top
-| top
+: prog top { $$ = append($1, $2); }
+| top { $$ = append(ZL, $1); }
top
: FUNC NAME '(' args ')' return '{' stmts '}'
-| MOD NAME ';'
-| USE NAME ';'
-| def ';'
+ { $$ = mkfunc($2, $4, $6, $8); }
+| MOD NAME ';' { $$ = nil; modname = $2; }
+| USE NAME ';' { $$ = nil; loaduse($2); }
+| def ';' { $$ = nil; }
sdecls
:
@@ -53,12 +62,9 @@
| sdecls NAME ':' type ';'
def
-: DEF NAME NAME
- {
- }
+: DEF NAME NAME { }
| DEF NAME STRUCT '{' sdecls '}'
- {
- }
+ { }
type
: NAME { $$ = nil; }
@@ -67,27 +73,29 @@
| type '!' { $$ = $1; $$->linear = 1; }
return
-: ARROWR type
-|
+: ARROWR type { $$ = $2; }
+| { $$ = mktype(Tvoid); }
-
unary
: NUM { $$ = mkexpr(Olit, mklit($1), nil); }
| NAME { $$ = mkexpr(Ovar, mksym($1), nil); }
-| ARROWL unary { $$ = mkexpr(Orcv, $2, nil); }
| '(' expr ')' { $$ = $2; }
sufexpr
-: unary '--' { $$ = mkexpr(Odec, $1, nil); }
-| unary '++' { $$ = mkexpr(Oinc, $1, nil); }
+: sufexpr '--' { $$ = mkexpr(Odec, $1, nil); }
+| sufexpr '++' { $$ = mkexpr(Oinc, $1, nil); }
| unary { $$ = $1; }
-mulexpr
-: mulexpr '*' sufexpr { $$ = mkexpr(Omul, $1, $3); }
-| mulexpr '/' sufexpr { $$ = mkexpr(Odiv, $1, $3); }
-| mulexpr '%' sufexpr { $$ = mkexpr(Omod, $1, $3); }
+prefexpr
+: ARROWL sufexpr { $$ = mkexpr(Orcv, $2, nil); }
| sufexpr { $$ = $1; }
+mulexpr
+: mulexpr '*' prefexpr { $$ = mkexpr(Omul, $1, $3); }
+| mulexpr '/' prefexpr { $$ = mkexpr(Odiv, $1, $3); }
+| mulexpr '%' prefexpr { $$ = mkexpr(Omod, $1, $3); }
+| prefexpr { $$ = $1; }
+
addexpr
: addexpr '+' mulexpr { $$ = mkexpr(Oadd, $1, $3); }
| addexpr '-' mulexpr { $$ = mkexpr(Osub, $1, $3); }
@@ -119,21 +127,25 @@
| logexpr { $$ = $1; }
stmts
-:
-| stmts stmt
+: { $$ = ZL; }
+| stmts stmt { $$ = append($1, $2); }
decl
-: NAME ':' type
-| NAME ':' '=' expr
+: NAME ':' type { $$ = mkdecl($1, $3, nil); }
+| NAME ':' '=' expr { $$ = mkdecl($1, nil, $4); }
+| NAME ':' type '=' expr { $$ = mkdecl($1, $3, $5); }
stmt
-: expr ';'
-| '{' stmts '}'
-| decl ';'
-| FOR '(' expr ')' stmt
+: expr ';' { $$ = $1; }
+| '{' stmts '}' { $$ = mkblk($2); }
+| decl ';' { $$ = $1->dcl.init; }
+| FOR '(' unary ARROWL expr ')' stmt
+ { $$ = mkiter($3, $5, $7); }
| FOR '(' expr ';' expr ';' expr ')' stmt
+ { $$ = mkfor($3, $5, $7, $9); }
| IF '(' expr ')' stmt ELSE stmt
-| IF '(' expr ')' stmt
+ { $$ = mkif($3, $5, $7); }
+| IF '(' expr ')' stmt { $$ = mkif($3, $5, nil); }
arg
: NAME ':' type { $$ = mkdecl($1, $3, nil); }
--- a/nod.c
+++ b/nod.c
@@ -7,6 +7,8 @@
Nlst
append(Nlst l, Nod *n)
{
+ if(n == nil)
+ return l;
if(l.nv == l.cap){
if(l.cap == 0)
l.cap = 1;
@@ -71,6 +73,36 @@
}
Nod*
+mkif(Nod *cond, Nod *t, Nod *f)
+{
+ Nod *n;
+
+ n = new(Nif);
+ n->nif.cond = cond;
+ n->nif.t = t;
+ n->nif.f = f;
+ return n;
+}
+
+Nod*
+mkfunc(char*, Nlst args, Typ *ret, Nlst body)
+{
+ Nod *n;
+ Typ **tv;
+ int i;
+
+ if((tv = calloc(args.nv, sizeof(Typ*))) == nil)
+ abort();
+ for(i = 0; i < args.nv; i++)
+ tv[i] = args.v[i]->dcl.type;
+ n = new(Nfunc);
+ n->func.args = args;
+ n->func.type = mktyfunc(tv, args.nv, ret);
+ n->func.body = mkblk(body);
+ return n;
+}
+
+Nod*
mklit(vlong v)
{
Nod *n;
@@ -103,7 +135,29 @@
return n;
}
+Nod*
+mkblk(Nlst nl)
+{
+ Nod *n;
+
+ n = new(Ndcl);
+ n->blk.body = nl;
+ return n;
+}
+
+
Typ*
+mktype(int tt)
+{
+ Typ *t;
+
+ if((t = mallocz(sizeof(Typ), 1)) == nil)
+ abort();
+ t->t = tt;
+ return t;
+}
+
+Typ*
mktyarray(Typ *bt, Nod *sz)
{
Typ *t;
@@ -127,3 +181,15 @@
return t;
}
+Typ*
+mktyfunc(Typ **tv, int nv, Typ *ret)
+{
+ Typ *t;
+
+ t = calloc(1, sizeof(Typ));
+ t->t = Tfunc;
+ t->args = tv;
+ t->nargs = nv;
+ t->base = ret;
+ return t;
+}