blob: f3ad3ba43d03a946d35c6036701851a3040328ab (
plain) (
tree)
|
|
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include "ast.h"
#include "cibic.tab.h"
#define NEW_CNODE ((CNode *)malloc(sizeof(CNode)))
CNode *ast_root;
void cnode_init() {
}
void cnode_reverse_chd(CNode *node) {
static CNode *chdn[MAX_CHDN];
CNode *p;
int n = 0;
for (p = node->chd; p; p = p->next)
cnode_reverse_chd(p);
for (p = node->chd; p; p = p->next)
chdn[n++] = p;
if (n)
{
node->chd = chdn[--n];
for (; n; n--)
chdn[n]->next = chdn[n - 1];
chdn[0]->next = NULL;
}
}
CNode *cnode_create_ast(CNode *wrapped) {
cnode_reverse_chd(wrapped);
return wrapped;
}
CNode *cnode_create_nop() {
CNode *nop = NEW_CNODE;
nop->type = NOP;
nop->next = nop->chd = NULL;
return nop;
}
CNode *cnode_create_general(int type, int subtype, int pnum, va_list ap) {
int i;
CNode *exp = NEW_CNODE;
exp->type = type;
exp->rec.subtype = subtype;
exp->next = exp->chd = NULL;
for (i = 0; i < pnum; i++)
{
CNode *subexp = va_arg(ap, CNode*);
#ifdef CNODE_DEBUG
assert(subexp->next == NULL);
#endif
subexp->next = exp->chd;
exp->chd = subexp;
}
return exp;
}
CNode *cnode_list_append(CNode *list, CNode *tail) {
if (list->type == NOP)
{
free(list);
return tail;
}
tail->next = list;
return tail;
}
CNode *cnode_create_identifier(char *val) {
CNode *exp = NEW_CNODE;
exp->type = ID;
exp->chd = exp->next = NULL;
exp->rec.strval = val;
return exp;
}
CNode *cnode_create_int_const(int val) {
/* TODO: overflow checking */
CNode *exp = NEW_CNODE;
exp->type = INT;
exp->chd = exp->next
|