| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #ifndef _PLY_NODE_H
- #define _PLY_NODE_H
- #include <stdint.h>
- #include <stdio.h>
- #include "type.h"
- typedef struct node node_t;
- struct cons {
- node_t *car;
- node_t *cdr;
- };
- typedef enum op {
- OP_AGG = '@',
- OP_CALL = '(',
- OP_DEREF = '*',
- OP_DOT = '.',
- OP_MAP = '{',
- } op_t;
- typedef enum ntype {
- N_CONS,
- N_OP,
- N_IDENT,
- N_NUM,
- N_STRING,
- } ntype_t;
- struct node {
- ntype_t ntype;
- union {
- /* atom_t atom; */
- struct cons cons;
- op_t op;
- char *ident;
- int64_t num;
- char *string;
- };
- node_t *up;
- type_t *type;
- };
- /* debug */
- typedef struct ndump_info {
- FILE *fp;
- int indent;
- } ndump_info_t;
- void ndump(node_t *n, ndump_info_t *info);
- typedef int (*walk_fn)(node_t *, void *);
- int nwalk(node_t *n, walk_fn pre, walk_fn post, void *ctx);
- /* high-level constructors */
- node_t *ncall(char *name, node_t *args);
- node_t *nmap (char *name, node_t *key);
- /* basic constructors */
- node_t *ncons (node_t *car, node_t *cdr);
- node_t *nop (op_t op);
- node_t *nident (char *name);
- node_t *nnum (int64_t num);
- node_t *nstring(char *string);
- /* utilities */
- static inline int nop_is(node_t *n, op_t op)
- {
- if (!n || (n->ntype != N_OP))
- return 0;
- return n->op == op;
- }
- static inline node_t *ncar(node_t *n)
- {
- if (!n || (n->ntype != N_CONS))
- return NULL;
- return n->cons.car;
- }
- static inline node_t *ncdr(node_t *n)
- {
- if (!n || (n->ntype != N_CONS))
- return NULL;
- return n->cons.cdr;
- }
- static inline node_t *nup(node_t *n)
- {
- return n? n->up : NULL;
- }
- #endif /* _PLY_NODE_H */
|