static NODE * factor(void){ NODE * t = NULL; switch(token.type){ case NUM: t = new_exp(N_CONST); if ((t != NULL) && (token.type == NUM)){ t->attribute.val = token.attribute.val; } match(NUM); break; case VAR: t = new_exp(N_VAR); if ((t != NULL) && (token.type == VAR)){ t->attribute.val = toHeap(token.attribute.name); } match(VAR); break; case LPAR: match(LPAR); t = exp(); match(RPAR); break; default: syntax_error("Unexpected Token -> "); token = getToken(); break; } return t; }
static inline struct expression * new_exp_1(enum expression_operator op, struct expression *right) { struct expression *args[1]; args[0] = right; return new_exp(1, op, args); }
static struct expression * new_exp_2(enum expression_operator op, struct expression *left, struct expression *right) { struct expression *args[2]; args[0] = left; args[1] = right; return new_exp(2, op, args); }
static inline struct expression * new_exp_3(enum expression_operator op, struct expression *bexp, struct expression *tbranch, struct expression *fbranch) { struct expression *args[3]; args[0] = bexp; args[1] = tbranch; args[2] = fbranch; return new_exp(3, op, args); }
//TODO PRobably a problem here static NODE * exp(void){ NODE * t = new_exp(N_OP); if (t != NULL){ t->child[0] = exp_helper(); t->attribute.token_class = token.type; } match(token.type); // ADD or SUB if (t != NULL){ t->child[1] = exp_helper(); } }
static NODE * term(void){ NODE * t = factor(); while (token.type == MUL || token.type == DIV){ NODE * temp = new_exp(N_OP); if (temp != NULL){ temp->child[0] = t; temp->attribute.token_class = token.type; t = temp; match(token.type); temp->child[1] = factor(); } } return t; }
static NODE *exp_helper(void){ NODE * t = term(); while (token.type == ADD || token.type == SUB){ NODE * temp = new_exp(N_OP); if (temp != NULL){ temp->child[0] = t; temp->attribute.token_class = token.type; t = temp; match(token.type); p->child[1] = term(); } } return t; }
static inline struct expression * new_exp_0(enum expression_operator op) { return new_exp(0, op, NULL); }