|
|
@@ -1,19 +1,18 @@
|
|
1
|
1
|
#include <assert.h>
|
|
2
|
2
|
#include <ctype.h>
|
|
3
|
3
|
#include <inttypes.h>
|
|
|
4
|
+#include <search.h>
|
|
|
5
|
+#include <stdarg.h>
|
|
4
|
6
|
#include <stdio.h>
|
|
5
|
7
|
#include <stdlib.h>
|
|
6
|
8
|
|
|
7
|
9
|
#include "node.h"
|
|
8
|
10
|
|
|
9
|
|
-void nprint(node_t *n, FILE *fp)
|
|
|
11
|
+void node_print(node_t *n, FILE *fp)
|
|
10
|
12
|
{
|
|
11
|
13
|
switch (n->ntype) {
|
|
12
|
|
- case N_CONS:
|
|
13
|
|
- fputs("<CONS>", fp);
|
|
14
|
|
- break;
|
|
15
|
|
- case N_OP:
|
|
16
|
|
- fputc(n->op, fp);
|
|
|
14
|
+ case N_LIST:
|
|
|
15
|
+ fputs("()", fp);
|
|
17
|
16
|
break;
|
|
18
|
17
|
case N_IDENT:
|
|
19
|
18
|
fputs(n->ident, fp);
|
|
|
@@ -28,21 +27,47 @@ void nprint(node_t *n, FILE *fp)
|
|
28
|
27
|
default:
|
|
29
|
28
|
fputs("<INVALID>", fp);
|
|
30
|
29
|
}
|
|
|
30
|
+
|
|
|
31
|
+ /* if (n->type) { */
|
|
|
32
|
+ /* fputc(' ', fp); */
|
|
|
33
|
+ /* type_dump(n->type, fp); */
|
|
|
34
|
+ /* } */
|
|
31
|
35
|
}
|
|
32
|
36
|
|
|
|
37
|
+struct node_dump_info {
|
|
|
38
|
+ FILE *fp;
|
|
|
39
|
+ int indent;
|
|
|
40
|
+};
|
|
|
41
|
+
|
|
33
|
42
|
int __node_dump_pre(node_t *n, void *_info)
|
|
34
|
43
|
{
|
|
35
|
|
- ndump_info_t *info = _info;
|
|
|
44
|
+ struct node_dump_info *info = _info;
|
|
36
|
45
|
|
|
37
|
46
|
switch (n->ntype) {
|
|
38
|
|
- case N_CONS:
|
|
|
47
|
+ case N_LIST:
|
|
|
48
|
+ fprintf(info->fp, "%s%*s(",
|
|
|
49
|
+ info->indent? "\n" : "",
|
|
|
50
|
+ info->indent, "");
|
|
39
|
51
|
info->indent += 2;
|
|
40
|
52
|
break;
|
|
41
|
53
|
|
|
42
|
54
|
default:
|
|
43
|
|
- fprintf(info->fp, "%*s", info->indent, "");
|
|
44
|
|
- nprint(n, info->fp);
|
|
45
|
|
- fputc('\n', info->fp);
|
|
|
55
|
+ if (n->prev) {
|
|
|
56
|
+ node_t *c;
|
|
|
57
|
+
|
|
|
58
|
+ for (c = n->next; c; c = c->next) {
|
|
|
59
|
+ if (c->ntype == N_LIST) {
|
|
|
60
|
+ fprintf(info->fp, "\n%*s",
|
|
|
61
|
+ info->indent, "");
|
|
|
62
|
+ break;
|
|
|
63
|
+ }
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ if (!c)
|
|
|
67
|
+ fputc(' ', info->fp);
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ node_print(n, info->fp);
|
|
46
|
71
|
}
|
|
47
|
72
|
|
|
48
|
73
|
return 0;
|
|
|
@@ -50,40 +75,54 @@ int __node_dump_pre(node_t *n, void *_info)
|
|
50
|
75
|
|
|
51
|
76
|
int __node_dump_post(node_t *n, void *_info)
|
|
52
|
77
|
{
|
|
53
|
|
- ndump_info_t *info = _info;
|
|
54
|
|
- node_t *next;
|
|
|
78
|
+ struct node_dump_info *info = _info;
|
|
|
79
|
+ node_t *c;
|
|
|
80
|
+
|
|
|
81
|
+
|
|
|
82
|
+ if (n->ntype != N_LIST)
|
|
|
83
|
+ return 0;
|
|
55
|
84
|
|
|
56
|
|
- if (n->ntype == N_CONS)
|
|
57
|
|
- info->indent -= 2;
|
|
|
85
|
+ info->indent -= 2;
|
|
58
|
86
|
|
|
|
87
|
+ for (c = n->list; c; c = c->next) {
|
|
|
88
|
+ if (c->ntype == N_LIST) {
|
|
|
89
|
+ fprintf(info->fp, "\n%*s)", info->indent, "");
|
|
|
90
|
+ return 0;
|
|
|
91
|
+ }
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ fputc(')', info->fp);
|
|
59
|
95
|
return 0;
|
|
60
|
96
|
}
|
|
61
|
97
|
|
|
62
|
|
-void ndump(node_t *n, ndump_info_t *info)
|
|
|
98
|
+void node_dump(node_t *n, FILE *fp)
|
|
63
|
99
|
{
|
|
|
100
|
+ struct node_dump_info info = {
|
|
|
101
|
+ .fp = fp,
|
|
|
102
|
+ };
|
|
64
|
103
|
|
|
65
|
|
- nwalk(n, __node_dump_pre, __node_dump_post, info);
|
|
|
104
|
+ node_walk(n, __node_dump_pre, __node_dump_post, &info);
|
|
66
|
105
|
}
|
|
67
|
106
|
|
|
68
|
107
|
|
|
69
|
|
-int nwalk(node_t *n,
|
|
70
|
|
- int (*pre)(node_t *, void *),
|
|
71
|
|
- int (*post)(node_t *, void *),
|
|
72
|
|
- void *ctx)
|
|
|
108
|
+int node_walk(node_t *n,
|
|
|
109
|
+ int (*pre)(node_t *, void *),
|
|
|
110
|
+ int (*post)(node_t *, void *),
|
|
|
111
|
+ void *ctx)
|
|
73
|
112
|
{
|
|
74
|
113
|
int err = 0;
|
|
75
|
114
|
|
|
76
|
115
|
if (pre && (err = pre(n, ctx)))
|
|
77
|
116
|
return err;
|
|
78
|
117
|
|
|
79
|
|
- if (n->ntype == N_CONS) {
|
|
80
|
|
- err = ncar(n) ? nwalk(ncar(n), pre, post, ctx) : 0;
|
|
81
|
|
- if (err)
|
|
82
|
|
- return err;
|
|
|
118
|
+ if (n->ntype == N_LIST) {
|
|
|
119
|
+ node_t *c;
|
|
83
|
120
|
|
|
84
|
|
- err = ncdr(n) ? nwalk(ncdr(n), pre, post, ctx) : 0;
|
|
85
|
|
- if (err)
|
|
86
|
|
- return err;
|
|
|
121
|
+ for (c = n->list; c; c = c->next) {
|
|
|
122
|
+ err = node_walk(c, pre, post, ctx);
|
|
|
123
|
+ if (err)
|
|
|
124
|
+ return err;
|
|
|
125
|
+ }
|
|
87
|
126
|
}
|
|
88
|
127
|
|
|
89
|
128
|
if (post && (err = post(n, ctx)))
|
|
|
@@ -93,19 +132,7 @@ int nwalk(node_t *n,
|
|
93
|
132
|
}
|
|
94
|
133
|
|
|
95
|
134
|
|
|
96
|
|
-/* high-level constructors */
|
|
97
|
|
-
|
|
98
|
|
-node_t *ncall(char *name, node_t *args)
|
|
99
|
|
-{
|
|
100
|
|
- return ncons(nop('('), ncons(nident(name), args));
|
|
101
|
|
-}
|
|
102
|
|
-
|
|
103
|
|
-node_t *nmap(char *name, node_t *key)
|
|
104
|
|
-{
|
|
105
|
|
- return ncons(nop('{'), ncons(nident(name), key));
|
|
106
|
|
-}
|
|
107
|
|
-
|
|
108
|
|
-/* basic constructors */
|
|
|
135
|
+/* constructors */
|
|
109
|
136
|
|
|
110
|
137
|
static node_t *__node(ntype_t ntype)
|
|
111
|
138
|
{
|
|
|
@@ -116,43 +143,46 @@ static node_t *__node(ntype_t ntype)
|
|
116
|
143
|
return n;
|
|
117
|
144
|
}
|
|
118
|
145
|
|
|
119
|
|
-node_t *ncons(node_t *car, node_t *cdr)
|
|
|
146
|
+node_t *node_list(node_t *head)
|
|
120
|
147
|
{
|
|
121
|
|
- node_t *n = __node(N_CONS);
|
|
122
|
|
-
|
|
123
|
|
- assert(car);
|
|
124
|
|
- car->up = n;
|
|
|
148
|
+ node_t *n = __node(N_LIST);
|
|
125
|
149
|
|
|
126
|
|
- if (cdr)
|
|
127
|
|
- cdr->up = n;
|
|
128
|
|
-
|
|
129
|
|
- n->cons.car = car;
|
|
130
|
|
- n->cons.cdr = cdr;
|
|
|
150
|
+ n->list = head;
|
|
131
|
151
|
return n;
|
|
132
|
152
|
}
|
|
133
|
153
|
|
|
134
|
|
-node_t *nop(op_t op)
|
|
|
154
|
+node_t *node_vlist(node_t *head, ...)
|
|
135
|
155
|
{
|
|
136
|
|
- node_t *n = __node(N_OP);
|
|
137
|
|
- n->op = op;
|
|
|
156
|
+ va_list ap;
|
|
|
157
|
+ node_t *n, *next;
|
|
|
158
|
+
|
|
|
159
|
+ n = node_list(head);
|
|
|
160
|
+
|
|
|
161
|
+ va_start(ap, head);
|
|
|
162
|
+ while ((next = va_arg(ap, node_t *))) {
|
|
|
163
|
+ insque(next, head);
|
|
|
164
|
+ head = next;
|
|
|
165
|
+ }
|
|
|
166
|
+ va_end(ap);
|
|
|
167
|
+
|
|
138
|
168
|
return n;
|
|
139
|
169
|
}
|
|
140
|
170
|
|
|
141
|
|
-node_t *nident(char *name)
|
|
|
171
|
+node_t *node_ident(char *name)
|
|
142
|
172
|
{
|
|
143
|
173
|
node_t *n = __node(N_IDENT);
|
|
144
|
174
|
n->ident = name;
|
|
145
|
175
|
return n;
|
|
146
|
176
|
}
|
|
147
|
177
|
|
|
148
|
|
-node_t *nnum(int64_t num)
|
|
|
178
|
+node_t *node_num(int64_t num)
|
|
149
|
179
|
{
|
|
150
|
180
|
node_t *n = __node(N_NUM);
|
|
151
|
181
|
n->num = num;
|
|
152
|
182
|
return n;
|
|
153
|
183
|
}
|
|
154
|
184
|
|
|
155
|
|
-node_t *nstring(char *string)
|
|
|
185
|
+node_t *node_string(char *string)
|
|
156
|
186
|
{
|
|
157
|
187
|
node_t *n = __node(N_STRING);
|
|
158
|
188
|
n->string = string;
|