| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <inttypes.h>
- #include <search.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "node.h"
- #include "printxf.h"
- void node_print(struct node *n, FILE *fp)
- {
- switch (n->ntype) {
- case N_EXPR:
- fprintf(fp, "\e[31m%s\e[0m", n->expr.func);
- break;
- case N_IDENT:
- fputs(n->ident.name, fp);
- break;
- case N_NUM:
- if (n->num.unsignd)
- fprintf(fp, "%"PRIu64, n->num.u64);
- else
- fprintf(fp, "%"PRId64, n->num.s64);
- break;
- case N_STRING:
- fprintf(fp, "\"%s\"", n->string.data);
- break;
- default:
- fputs("<INVALID>", fp);
- }
- }
- int node_vfprintxf(struct printxf *pxf, FILE *fp, const char *spec, va_list ap)
- {
- struct node *n;
- n = va_arg(ap, struct node *);
- /* TODO: "%#N" should print source file location */
- node_print(n, fp);
- return 0;
- }
- int node_walk(struct node *n,
- int (*pre)(struct node *, void *),
- int (*post)(struct node *, void *),
- void *ctx)
- {
- int err = 0;
-
- if (pre && (err = pre(n, ctx)))
- return err;
- if (n->ntype == N_EXPR) {
- struct node *arg;
- node_expr_foreach(n, arg) {
- err = node_walk(arg, pre, post, ctx);
- if (err)
- return err;
- }
- }
- if (post && (err = post(n, ctx)))
- return err;
- return 0;
- }
- int node_replace(struct node *n, struct node *new)
- {
- new->up = n->up;
- if (n->prev) {
- new->prev = n->prev;
- n->prev->next = new;
- }
- if (n->next) {
- new->next = n->next;
- n->next->prev = new;
- }
- /* TODO: don't leak memory */
- return 0;
- }
- /* helpers */
- int node_is_block(struct node *n)
- {
- if (!n || (n->ntype != N_EXPR))
- return 0;
- return !strcmp(n->expr.func, ":block");
- }
- int node_is_map(struct node *n)
- {
- if (!n || (n->ntype != N_EXPR))
- return 0;
- return !strcmp(n->expr.func, "{}");
- }
- /* constructors */
- static struct node *node_new(enum ntype ntype)
- {
- struct node *n;
- n = calloc(1, sizeof(*n));
- assert(n);
- n->ntype = ntype;
- return n;
- }
- struct node *node_string(char *data)
- {
- struct node *n = node_new(N_STRING);
- n->string.data = data;
- return n;
- }
- struct node *node_num(const char *numstr)
- {
- struct node *n = node_new(N_NUM);
- if (numstr[0] == '-')
- numstr++;
- else
- n->num.unsignd = 1;
-
- errno = 0;
- n->num.u64 = strtoull(numstr, NULL, 0);
- if (!errno) {
- if (!n->num.unsignd)
- n->num.s64 = -n->num.u64;
- return n;
- }
- assert(0);
- return NULL;
- }
- struct node *node_ident(char *name)
- {
- struct node *n = node_new(N_IDENT);
- n->ident.name = name;
- return n;
- }
- struct node *node_append(struct node *n, struct node *arg)
- {
- struct node *last;
- assert(n->ntype == N_EXPR);
- arg->up = n;
- if (!n->expr.args) {
- n->expr.args = arg;
- return n;
- }
- for (last = n->expr.args; last->next; last = last->next);
- last->next = arg;
- arg->prev = last;
- return n;
- }
- struct node *node_expr(char *func, ...)
- {
- va_list ap;
- struct node *n, *arg;
- n = node_new(N_EXPR);
- n->expr.func = func;
- va_start(ap, func);
- while ((arg = va_arg(ap, struct node *)))
- node_append(n, arg);
- va_end(ap);
- return n;
-
- }
- __attribute__((constructor))
- static void node_init(void)
- {
- printxf_default.vfprintxf['N'] = node_vfprintxf;
- }
|