-
Notifications
You must be signed in to change notification settings - Fork 6
/
val.c
69 lines (63 loc) · 1.38 KB
/
val.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
// bison.h includes val.h
#include "bison.h"
#include "flex.h"
#include "util.h"
val_ptr val_new(int type, char *s) {
val_ptr r = malloc(sizeof(*r));
r->type = type;
if (s) r->s = strdup(s);
r->nkid = 0;
r->kid = 0;
return r;
}
val_ptr val_append(val_ptr op, val_ptr v) {
op->nkid++;
op->kid = realloc(op->kid, op->nkid * sizeof(struct val_s));
op->kid[op->nkid - 1] = v;
return op;
}
val_ptr val_append0(val_ptr op, ...) {
assert(op->type == T_STRING);
va_list params;
va_start(params, op);
for(;;) {
val_ptr v = va_arg(params, val_ptr);
if (!v) break;
val_append(op, v);
}
va_end(params);
return op;
}
val_ptr val_fun(val_ptr fun, val_ptr args) {
assert(fun->type == T_STRING);
assert(!fun->nkid);
args->s = fun->s;
free(fun);
return args;
}
void val_free(val_ptr v) {
switch(v->type) {
case T_STRING:
for (int i = 0; i < v->nkid; i++) {
val_free(v->kid[i]);
}
free(v->s);
free(v->kid);
break;
case T_INT:
break;
}
free(v);
}
void val_parse_forall(char *s, val_callback_t callback) {
yyscan_t scanner;
if (yylex_init(&scanner)) die("yylex_init error");
YY_BUFFER_STATE buf = s ? yy_scan_string(s, scanner) : 0;
if (yyparse(scanner, callback)) die("parse error");
yy_delete_buffer(buf, scanner);
yylex_destroy(scanner);
}