ref: c2086cda32afb3960938a81df6f5a6a209bfaf33
dir: /nod.c/
#include <u.h> #include <libc.h> #include "dat.h" #include "fns.h" Nlst append(Nlst l, Nod *n) { if(n == nil) return l; if(l.nv == l.cap){ if(l.cap == 0) l.cap = 1; l.cap *= 2; l.v = realloc(l.v, l.cap*sizeof(Nod*)); if(l.v == nil) abort(); } l.v[l.nv++] = n; return l; } Nod* new(int t) { Nod *n; if((n = mallocz(sizeof(Nod), 1)) == nil) abort(); n->loc.line = lexline; n->loc.file = lexfile; n->t = t; return n; } Nod* mkexpr(int op, Nod *lhs, Nod *rhs) { Nod *e; e = new(Nexpr); e->expr.op = op; e->expr.lhs = lhs; e->expr.rhs = rhs; e->expr.type = nil; return e; } Nod* mkiter(Nod *idx, Nod *arg, Nod *body) { Nod *n; n = new(Niter); n->iter.idx = idx; n->iter.arg = arg; n->iter.body = body; return n; } Nod* mkfor(Nod *init, Nod *cond, Nod *step, Nod *body) { Nod *n; n = new(Nfor); n->nfor.init = init; n->nfor.cond= cond; n->nfor.step = step; n->nfor.body = body; return n; } 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 *s, 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); n->func.name = s; return n; } Nod* mkcall(char *s, Nlst args) { Nod *n; n = new(Ncall); n->call.name = s; n->call.args = args; return n; } Nod* mklit(vlong v) { Nod *n; n = new(Nlit); n->lit.val = v; return n; } Nod* mksym(char *s) { Nod *n; n = new(Nsym); n->sym.sym = s; return n; } Nod* mkdecl(char *s, Typ *t, Nod *i) { Nod *n; n = new(Ndcl); n->dcl.name = s; n->dcl.type = t; n->dcl.init = i; return n; } Nod* mkblk(Nlst nl) { Nod *n; n = new(Nblk); n->blk.body = nl; return n; } Typ* mktype(char *s, int tt) { Typ *t; if((t = mallocz(sizeof(Typ), 1)) == nil) abort(); t->t = tt; t->name = s; return t; } Typ* mktyarray(Typ *bt, Nod *sz) { Typ *t; if((t = mallocz(sizeof(Typ), 1)) == nil) abort(); t->t = Tarray; t->base = bt; t->size = sz; return t; } Typ* mktyslice(Typ *bt) { Typ *t; t = calloc(1, sizeof(Typ)); t->t = Tslice; t->base = bt; 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; }