diff options
Diffstat (limited to 'node.h')
-rw-r--r-- | node.h | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -0,0 +1,56 @@ +#pragma once + +#include "type.h" + + +enum node_type { + N_LIST, // [head] [tail] + N_LIST_END, // -- + N_BLOCK, // [body] + N_VAR_DECL_INIT, // rtype name [value] + N_VAR_DECL, // rtype name + N_FUNC_DECL, // rtype name [params] [body] + N_IF, // [cond] [body] [else-body] + N_WHILE, // [cond] [body] + N_RETURN, // [value] + N_NUM, // value + N_VAR, // name + N_BINOP, // [left] oper [right] + N_UNOP, // oper [arg] + N_CALL, // name [args] +}; + +enum operator { + // binary + OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, + OP_EQ, OP_NEQ, OP_LT, OP_GT, OP_LEQ, OP_GEQ, + OP_AND, OP_OR, + OP_ASSIGN, + // unary + OP_NEG, OP_NOT, OP_DEREF, OP_ADDROF, +}; + +struct node { + enum node_type type; + struct node *child1, *child2, *child3; + struct type *rtype; + char *name; + int value; + enum operator oper; +}; + + +struct node* node_make_0(enum node_type type); +struct node* node_make_1(enum node_type type, struct node *c1); +struct node* node_make_2(enum node_type type, struct node *c1, struct node *c2); +struct node* node_make_3(enum node_type type, struct node *c1, struct node *c2, struct node *c3); + +__attribute__((sentinel)) +struct node* node_make_list_(struct node *first, ...); +#define node_make_list(...) node_make_list_(__VA_ARGS__, NULL) + +int node_list_length(struct node *node); + +void node_delete_recursive(struct node *node); + +void node_print(const struct node *node, FILE *f, int indent); |