shithub: hammer

ref: 72abaa282749e37df7831959b1a344664b1827f3
dir: /nod.c/

View raw version
#include <u.h>
#include <libc.h>

#include "dat.h"
#include "fns.h"

Nlst
append(Nlst l, Nod *n)
{
	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*
mklit(vlong v)
{
	Nod *n;

	n = new(Nlit);
	n->lit.val = v;
	return n;
}

Nod*
mksym(char *s)
{
	Nod *n;

	n = new(Nlit);
	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;
}

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;
}