Beispiel #1
0
Datei: node.c Projekt: oridb/mc
Node *
mkfunc(Srcloc loc, Node **args, size_t nargs, Type *ret, Node *body)
{
	Node *n;
	Node *f;
	size_t i;
	Stab *st;

	f = mknode(loc, Nfunc);
	f->func.args = args;
	f->func.nargs = nargs;
	f->func.body = body;
	f->func.scope = mkstab(1);
	f->func.type = mktyfunc(loc, args, nargs, ret);
	f->func.env = mkenv();

	bindtype(f->func.env, f->func.type);
	st = body->block.scope;
	for (i = 0; i < nargs; i++)
		putdcl(st, args[i]);

	n = mknode(loc, Nlit);
	n->lit.littype = Lfunc;
	n->lit.fnval = f;
	return n;
}
Beispiel #2
0
Datei: parse.c Projekt: xtao/c
static Node *
decl()
{
	Node   *n, *init;
	char   *name;
	CTy    *type, *basety;
	SrcPos *pos;
	Sym    *sym;
	Vec    *syms;
	int     sclass;

	pos = &tok->pos;
	syms  = vec();
	basety = declspecs(&sclass);
	while(tok->k != ';' && tok->k != TOKEOF) {
		type = declarator(basety, &name, &init);
		switch(sclass){
		case SCNONE:
			if(isglobal()) {
				sclass = SCGLOBAL;
			} else {
				sclass = SCAUTO;
			}
			break;
		case SCTYPEDEF:
			if(init)
				errorposf(pos, "typedef cannot have an initializer");
			break;
		}
		if(!name)
			errorposf(pos, "decl needs to specify a name");
		sym = definesym(pos, sclass, name, type, init);
		vecappend(syms, sym);
		if(isglobal() && tok->k == '{') {
			if(init)
				errorposf(pos, "function declaration has an initializer");
			if(type->t != CFUNC)
				errorposf(pos, "expected a function");
			curfunc = mknode(NFUNC, pos);
			curfunc->type = type;
			curfunc->Func.name = name;
			curfunc->Func.params = vec();
			curfunc->Func.stkslots = vec();
			fbody();
			definesym(pos, sclass, name, type, curfunc);
			curfunc = 0;
			goto done;
		}
		if(tok->k == ',')
			next();
		else
			break;
	}
	expect(';');
  done:
	n = mknode(NDECL, pos);
	n->Decl.syms = syms;
	return n;
}
Beispiel #3
0
t_btree		*P_ops(char *str, int i)
{
	t_btree *tree;
	t_btree *tree1;
	op;

	if (str[i + 1] >= '0' && str[i + 1] <= '9')
	{
		tree = mkleaf(str, i + 1);
		read;
		return (tree);
	}
	else if (str[i + 1] == '(')
	{
		read;
		tree = E_ops(str, i);
		expect(')');
		return (tree);
	}
	else if (str[i + 1] == '-')
	{
		read;
		tree = P_ops(str, i);
		return (mknode(unary('-'), tree));
	}
	else
		write(2, "Parse error\n", 12);
	return (tree);
}
Beispiel #4
0
struct kpnode* mktree(struct lnode *list)
{
	struct kpnode *root = 0;
	struct tlist *tlp = mktlist(0);
	struct tlist *tlroot = tlp;

	/* make leaves */
	for (; list != 0; list = list->next) {
		tlp->tree = mknode(list->ch);
		tlp->freq = list->freq;

		if (list->next != 0) {
			tlp->next = mktlist(0);
			tlp = tlp->next;
		}
	}

	tlroot = sorttlist(tlroot);

	disptlist(tlroot);

	/* connect leaves to form whole tree */
	tlroot = unite(tlroot);

	disptlist(tlroot);

	root = tlroot->tree;

	return root;
}
Beispiel #5
0
/* insert into red-black tree */
void bt_dict_insert(bt_dict *t, const char *k, bt_element *v)
{
    bt_dict *p, *g, *gg, *x;
    int n;

    if (t->key == NULL) {	/* initially empty tree */
        xfernode(t, k, v);
        return;
    }

    x = p = g = t;
    while (x != NULL) {
        gg = g;
        g = p;
        p = x;
        n = strcmp(k, x->key);
        if (n == 0) return;	/* no dupes */
        x = n < 0 ? x->left : x->right;
        if (x && x->left && x->right && x->left->red && x->right->red)
            x = split(t, x, p, g, gg, k);
    }

    x = mknode(k, v);
    if (less(k, p->key)) p->left = x;
    else p->right = x;
    split(t, x, p, g, gg, k);
}
Beispiel #6
0
Datei: node.c Projekt: oridb/mc
Node *
mkimplstmt(Srcloc loc, Node *name, Type *t, Type **aux, size_t naux, Node **decls, size_t ndecls)
{
	Node *n;
	size_t i;

	n = mknode(loc, Nimpl);
	n->impl.traitname = name;
	n->impl.type = t;
	n->impl.aux = aux;
	n->impl.naux = naux;
	n->impl.decls = decls;
	n->impl.ndecls = ndecls;
	lappend(&impltab, &nimpltab, n);
	if (hasparams(t)) {
		n->impl.env = mkenv();
		bindtype(n->impl.env, t);
	}
	for (i = 0; i < naux; i++)
		if (hasparams(aux[i]))
			bindtype(n->impl.env, aux[i]);
	for (i = 0; i < ndecls; i++) {
		if (name->name.ns)
			setns(decls[i]->decl.name, name->name.ns);
		if (decls[i]->decl.env)
			decls[i]->decl.env->super = n->impl.env;
	}
	return n;
}
Beispiel #7
0
int main()
{
	Node *tmp = mknode(17);
	
	fprintf(stdout, "Before calling rmnode(tmp) ...   \n");
	fprintf(stdout, "---------------------------------------------\n");
	fprintf(stdout, "tmp has an address of:         %p\n", &tmp);
	fprintf(stdout, "tmp points to:                 %p\n",  tmp);

	tmp = rmnode(tmp);

	fprintf(stdout, "\nAfter calling rmnode(tmp) ...    \n");
	fprintf(stdout, "---------------------------------------------\n");
	fprintf(stdout, "tmp has an address of:         %p\n", &tmp);
	fprintf(stdout, "tmp points to:                 %p\n",  tmp);

	if (tmp == NULL)
	{
		fprintf(stdout, "tmp successfully deleted.\n");
	}
	else
	{
		fprintf(stderr, "error deleting tmp.\n");
		exit(1);
	}

	return(0);
}
Beispiel #8
0
Datei: parse.c Projekt: xtao/c
static Node *
mkunop(SrcPos *p, int op, Node *o)
{
	Node *n;
	
	n = mknode(NUNOP, p);
	n->Unop.op = op;
	switch(op) {
	case '&':
		if(!islval(o))
			errorposf(&o->pos, "& expects an lvalue");
		n->type = mkptr(o->type);
		break;
	case '*':
		if(!isptr(o->type))
			errorposf(&o->pos, "cannot deref non pointer");
		n->type = o->type->Ptr.subty;
		break;
	default:
		o = ipromote(o);
		n->type = o->type;
		break;
	}
	n->Unop.operand = o;
	return n;
}
Beispiel #9
0
struct kpnode* insnode(char data, struct kpnode* childl, struct kpnode* childr)
{
	struct kpnode* node = mknode(data);
	node->lnext = childl;
	node->rnext = childr;

	return node;
}
Beispiel #10
0
tree *
mkid (char *id)
{
   tree *t = mknode (t_id, 0, NULL, NULL);
   t->value.idval = id;
   t->id = counter++;
   return t;
}
Beispiel #11
0
tree *
mknum (int num)
{
   tree *t = mknode (t_num, 0, NULL, NULL);
   t->value.numval = num;
   t->id = counter++;
   return t;
}
Beispiel #12
0
Datei: parse.c Projekt: xtao/c
static Node *
mkblock(SrcPos *p, Vec *v)
{
	Node *n;

	n = mknode(NBLOCK, p);
	n->Block.stmts = v;
	return n;
}
Beispiel #13
0
Datei: node.c Projekt: oridb/mc
Node *
mkvoid(Srcloc loc)
{
	Node *n;

	n = mknode(loc, Nlit);
	n->lit.littype = Lvoid;
	return n;
}
Beispiel #14
0
Datei: node.c Projekt: oridb/mc
Node *
mkblock(Srcloc loc, Stab *scope)
{
	Node *n;

	n = mknode(loc, Nblock);
	n->block.scope = scope;
	return n;
}
Beispiel #15
0
Datei: symbol.c Projekt: 1587/ltp
/*
 * Create a new key node, add it to the *end* of this list
 */
static int add_key(SYM sym, char *key, void *data)
{
	register struct sym *sn;

	if (sym->sym == NULL) {
		sym->sym = mknode(NULL, key, data);
		if (sym->sym == NULL) {
			return (-1);
		}
	} else {
		for (sn = sym->sym; sn != NULL && sn->next != NULL;
		     sn = sn->next) ;
		sn->next = mknode(NULL, key, data);
		assert(sn->next != NULL);
		if (sn->next == NULL)
			return (-1);
	}
	return (0);
}
Beispiel #16
0
Datei: node.c Projekt: oridb/mc
Node *
mkmatch(Srcloc loc, Node *pat, Node *body)
{
	Node *n;

	n = mknode(loc, Nmatch);
	n->match.pat = pat;
	n->match.block = body;
	return n;
}
Beispiel #17
0
Datei: node.c Projekt: oridb/mc
Node *
mkname(Srcloc loc, char *name)
{
	Node *n;

	n = mknode(loc, Nname);
	n->name.name = strdup(name);

	return n;
}
Beispiel #18
0
node * mknode_mass(int n, real m)
{
    node *root = mknode(n);	// note that sequential labels are always set.
    root->set_root(root);

    root->set_mass(m);
    for_all_daughters(node, root, b) b->set_mass(m/n);

    return root;
}
Beispiel #19
0
Datei: node.c Projekt: oridb/mc
Node *
mkfloat(Srcloc loc, double val)
{
	Node *n;

	n = mknode(loc, Nlit);
	n->lit.littype = Lflt;
	n->lit.fltval = val;

	return n;
}
Beispiel #20
0
Datei: node.c Projekt: oridb/mc
Node *
mkint(Srcloc loc, uvlong val)
{
	Node *n;

	n = mknode(loc, Nlit);
	n->lit.littype = Lint;
	n->lit.intval = val;

	return n;
}
Beispiel #21
0
Datei: node.c Projekt: oridb/mc
Node *
mkuse(Srcloc loc, char *use, int islocal)
{
	Node *n;

	n = mknode(loc, Nuse);
	n->use.name = strdup(use);
	n->use.islocal = islocal;

	return n;
}
Beispiel #22
0
Datei: node.c Projekt: oridb/mc
Node *
mkchar(Srcloc loc, uint32_t val)
{
	Node *n;

	n = mknode(loc, Nlit);
	n->lit.littype = Lchr;
	n->lit.chrval = val;

	return n;
}
Beispiel #23
0
Datei: node.c Projekt: oridb/mc
Node *
mkmatchstmt(Srcloc loc, Node *val, Node **matches, size_t nmatches)
{
	Node *n;

	n = mknode(loc, Nmatchstmt);
	n->matchstmt.val = val;
	n->matchstmt.matches = matches;
	n->matchstmt.nmatches = nmatches;
	return n;
}
Beispiel #24
0
Datei: node.c Projekt: oridb/mc
Node *
mkbool(Srcloc loc, int val)
{
	Node *n;

	n = mknode(loc, Nlit);
	n->lit.littype = Lbool;
	n->lit.boolval = val;

	return n;
}
Beispiel #25
0
Datei: node.c Projekt: oridb/mc
Node *
mkexprl(Srcloc loc, Op op, Node **args, size_t nargs)
{
	Node *n;

	n = mknode(loc, Nexpr);
	n->expr.op = op;
	n->expr.args = args;
	n->expr.nargs = nargs;
	return n;
}
Beispiel #26
0
Datei: node.c Projekt: oridb/mc
Node *
mkiterstmt(Srcloc loc, Node *elt, Node *seq, Node *body)
{
	Node *n;

	n = mknode(loc, Niterstmt);
	n->iterstmt.elt = elt;
	n->iterstmt.seq = seq;
	n->iterstmt.body = body;

	return n;
}
Beispiel #27
0
Datei: node.c Projekt: oridb/mc
Node *
mkifstmt(Srcloc loc, Node *cond, Node *iftrue, Node *iffalse)
{
	Node *n;

	n = mknode(loc, Nifstmt);
	n->ifstmt.cond = cond;
	n->ifstmt.iftrue = iftrue;
	n->ifstmt.iffalse = iffalse;

	return n;
}
Beispiel #28
0
Datei: node.c Projekt: oridb/mc
Node *
mklbl(Srcloc loc, char *lbl)
{
	Node *n;

	assert(lbl != NULL);
	n = mknode(loc, Nlit);
	n->lit.littype = Llbl;
	n->lit.lblname = NULL;
	n->lit.lblval = strdup(lbl);
	return mkexpr(loc, Olit, n, NULL);
}
Beispiel #29
0
Datei: parse.c Projekt: xtao/c
static Node *
mkcast(SrcPos *p, Node *o, CTy *to)
{
	Node *n;
	
	if(sametype(o->type, to))
		return o;
	n = mknode(NCAST, p);
	n->type = to;
	n->Cast.operand = o;
	return n;
}
Beispiel #30
0
Datei: node.c Projekt: oridb/mc
Node *
mknsname(Srcloc loc, char *ns, char *name)
{
	Node *n;

	n = mknode(loc, Nname);
	if (ns)
		n->name.ns = strdup(ns);
	n->name.name = strdup(name);

	return n;
}