| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #ifndef _PLY_NODE_H
- #define _PLY_NODE_H
- #include <stdint.h>
- #include <stdio.h>
- 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);
- void node_dump (struct node *n, FILE *fp);
- void node_error(struct node *n, FILE *fp, const char *fmt, ...);
- 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 */
|