#ifndef _PLY_NODE_H #define _PLY_NODE_H #include #include struct sym; /* symbol information is defined externally */ enum ntype { N_EXPR, N_IDENT, N_NUM, N_STRING, }; struct node { struct node *next, *prev, *up; struct sym *sym; enum ntype ntype; union { struct { char *func; struct node *args; } expr; struct { char *name; } ident; struct { int64_t num; } num; struct { char *data; } string; }; }; /* debug */ void node_print(struct node *n, FILE *fp); typedef int (*nwalk_fn)(struct node *, void *); int node_walk(struct node *n, nwalk_fn pre, nwalk_fn post, void *ctx); int node_replace(struct node *n, struct node *new); /* constructors */ struct node *node_string(char *data); struct node *node_num (int64_t num); struct node *node_ident (char *name); struct node *node_append(struct node *n, struct node *arg); struct node *node_expr (char *func, ...); /* helpers */ static inline int node_nargs(struct node *n) { struct node *arg; int nargs = 0; for (arg = n->expr.args; arg; arg = arg->next, nargs++); return nargs; } int node_is_block(struct node *n); int node_is_map (struct node *n); #define node_expr_foreach(_expr, _arg) \ for ((_arg) = (_expr)->expr.args; (_arg); (_arg) = (_arg)->next) #endif /* _PLY_NODE_H */