#ifndef _PLY_NODE_H #define _PLY_NODE_H #include #include #include "sym.h" #include "type.h" typedef struct node node_t; typedef enum kwclass { KW_SUBSCRIPT = '[', KW_ASSIGN = '=', KW_BINOP = 'b', } kwclass_t; typedef enum kwarith { KW_ADD = '+', KW_SUB = '-', KW_MUL = '*', KW_DIV = '/', } kwarith_t; typedef struct keyword { kwclass_t class; int op; } keyword_t; typedef enum ntype { N_LIST, N_KEYWORD, N_IDENT, N_NUM, N_STRING, } ntype_t; struct node { node_t *next, *prev, *up; ntype_t ntype; union { node_t *list; keyword_t keyword; const char *ident; int64_t num; const char *string; }; type_t *type; sym_t *sym; }; /* debug */ void node_print(node_t *n, FILE *fp); void node_dump (node_t *n, FILE *fp); typedef int (*walk_fn)(node_t *, void *); int node_walk(node_t *n, walk_fn pre, walk_fn post, void *ctx); int node_replace(node_t *n, node_t *new); /* constructors */ node_t *node_list (node_t *head); node_t *node_vlist (node_t *head, ...); node_t *node_keyword(kwclass_t class, int op); node_t *node_ident (const char *name); node_t *node_num (int64_t num); node_t *node_string (const char *string); static inline node_t *node_head(node_t *n) { if (!n) return NULL; for (; n->prev; n = n->prev); return n; } static inline node_t *node_prev(node_t *n) { return n ? n->prev : NULL; } static inline node_t *node_next(node_t *n) { return n ? n->next : NULL; } static inline node_t *node_up(node_t *n) { return n ? n->up : NULL; } #endif /* _PLY_NODE_H */