|
|
@@ -18,27 +18,20 @@ void __type_dump(type_t *t, FILE *fp)
|
|
18
|
18
|
fputs(">\e[0m", fp);
|
|
19
|
19
|
}
|
|
20
|
20
|
|
|
21
|
|
-void node_print(node_t *n, FILE *fp)
|
|
|
21
|
+void node_print(struct node *n, FILE *fp)
|
|
22
|
22
|
{
|
|
23
|
23
|
switch (n->ntype) {
|
|
24
|
|
- case N_LIST:
|
|
25
|
|
- fputs("()", fp);
|
|
26
|
|
- break;
|
|
27
|
|
- case N_KEYWORD:
|
|
28
|
|
- fputs("\e[31m", fp);
|
|
29
|
|
- fputc(n->keyword.class, fp);
|
|
30
|
|
- if (n->keyword.op)
|
|
31
|
|
- fputc(n->keyword.op, fp);
|
|
32
|
|
- fputs("\e[0m", fp);
|
|
|
24
|
+ case N_EXPR:
|
|
|
25
|
+ fprintf(fp, "\e[31m%s\e[0m", n->expr.func);
|
|
33
|
26
|
break;
|
|
34
|
27
|
case N_IDENT:
|
|
35
|
|
- fputs(n->ident, fp);
|
|
|
28
|
+ fputs(n->ident.name, fp);
|
|
36
|
29
|
break;
|
|
37
|
30
|
case N_NUM:
|
|
38
|
|
- fprintf(fp, "%"PRId64, n->num);
|
|
|
31
|
+ fprintf(fp, "%"PRId64, n->num.num);
|
|
39
|
32
|
break;
|
|
40
|
33
|
case N_STRING:
|
|
41
|
|
- fprintf(fp, "\"%s\"", n->string);
|
|
|
34
|
+ fprintf(fp, "\"%s\"", n->string.data);
|
|
42
|
35
|
break;
|
|
43
|
36
|
|
|
44
|
37
|
default:
|
|
|
@@ -50,68 +43,67 @@ void node_print(node_t *n, FILE *fp)
|
|
50
|
43
|
|
|
51
|
44
|
struct node_dump_info {
|
|
52
|
45
|
FILE *fp;
|
|
53
|
|
- int indent;
|
|
|
46
|
+ int indent, width;
|
|
54
|
47
|
};
|
|
55
|
48
|
|
|
56
|
|
-int __node_dump_pre(node_t *n, void *_info)
|
|
|
49
|
+int __node_dump_pre(struct node *n, void *_info)
|
|
57
|
50
|
{
|
|
58
|
51
|
struct node_dump_info *info = _info;
|
|
|
52
|
+ struct node *arg;
|
|
59
|
53
|
|
|
60
|
|
- switch (n->ntype) {
|
|
61
|
|
- case N_LIST:
|
|
62
|
|
- fprintf(info->fp, "%s%*s(",
|
|
63
|
|
- info->indent? "\n" : "",
|
|
64
|
|
- info->indent, "");
|
|
65
|
|
- info->indent += 2;
|
|
66
|
|
- break;
|
|
|
54
|
+ if (info->indent)
|
|
|
55
|
+ fprintf(info->fp, "\n%*s", info->width, "");
|
|
|
56
|
+ else
|
|
|
57
|
+ fputc(' ', fp);
|
|
67
|
58
|
|
|
68
|
|
- default:
|
|
69
|
|
- if (n->prev) {
|
|
70
|
|
- node_t *c;
|
|
|
59
|
+ if (n->ntype == N_EXPR) {
|
|
|
60
|
+ fputc('(', fp);
|
|
|
61
|
+
|
|
|
62
|
+ info->indent = 0;
|
|
|
63
|
+
|
|
|
64
|
+ node_expr_foreach(n, arg) {
|
|
|
65
|
+ struct node *subarg;
|
|
71
|
66
|
|
|
72
|
|
- for (c = n->next; c; c = c->next) {
|
|
73
|
|
- if (c->ntype == N_LIST) {
|
|
74
|
|
- fprintf(info->fp, "\n%*s",
|
|
75
|
|
- info->indent, "");
|
|
|
67
|
+ if (arg->ntype != N_EXPR)
|
|
|
68
|
+ continue;
|
|
|
69
|
+
|
|
|
70
|
+ node_expr_foreach(arg, subarg) {
|
|
|
71
|
+ if (arg->ntype == N_EXPR) {
|
|
|
72
|
+ info->indent = 1;
|
|
|
73
|
+ info->width += 2;
|
|
76
|
74
|
break;
|
|
77
|
75
|
}
|
|
78
|
76
|
}
|
|
79
|
77
|
|
|
80
|
|
- if (!c)
|
|
81
|
|
- fputc(' ', info->fp);
|
|
|
78
|
+ if (info->indent)
|
|
|
79
|
+ break;
|
|
82
|
80
|
}
|
|
83
|
|
-
|
|
84
|
|
- node_print(n, info->fp);
|
|
85
|
81
|
}
|
|
86
|
82
|
|
|
|
83
|
+ node_print(n, info->fp);
|
|
87
|
84
|
return 0;
|
|
88
|
85
|
}
|
|
89
|
86
|
|
|
90
|
|
-int __node_dump_post(node_t *n, void *_info)
|
|
|
87
|
+int __node_dump_post(struct node *n, void *_info)
|
|
91
|
88
|
{
|
|
92
|
89
|
struct node_dump_info *info = _info;
|
|
93
|
|
- node_t *c;
|
|
|
90
|
+ struct node *c;
|
|
94
|
91
|
|
|
95
|
92
|
|
|
96
|
|
- if (n->ntype != N_LIST)
|
|
|
93
|
+ if (n->ntype != N_EXPR)
|
|
97
|
94
|
return 0;
|
|
98
|
95
|
|
|
99
|
|
- info->indent -= 2;
|
|
|
96
|
+ /* if (info->ident) { */
|
|
|
97
|
+ /* info->width -= 2; */
|
|
100
|
98
|
|
|
101
|
|
- for (c = n->list; c; c = c->next) {
|
|
102
|
|
- if (c->ntype == N_LIST) {
|
|
103
|
|
- fprintf(info->fp, "\n%*s)", info->indent, "");
|
|
104
|
|
- __type_dump(n->type, info->fp);
|
|
105
|
|
- return 0;
|
|
106
|
|
- }
|
|
107
|
|
- }
|
|
|
99
|
+ /* fprintf(info->fp, "\n%*s", info->width, ""); */
|
|
|
100
|
+ /* } */
|
|
108
|
101
|
|
|
109
|
102
|
fputc(')', info->fp);
|
|
110
|
|
- __type_dump(n->type, info->fp);
|
|
111
|
103
|
return 0;
|
|
112
|
104
|
}
|
|
113
|
105
|
|
|
114
|
|
-void node_dump(node_t *n, FILE *fp)
|
|
|
106
|
+void node_dump(struct node *n, FILE *fp)
|
|
115
|
107
|
{
|
|
116
|
108
|
struct node_dump_info info = {
|
|
117
|
109
|
.fp = fp,
|
|
|
@@ -121,9 +113,9 @@ void node_dump(node_t *n, FILE *fp)
|
|
121
|
113
|
}
|
|
122
|
114
|
|
|
123
|
115
|
|
|
124
|
|
-int node_walk(node_t *n,
|
|
125
|
|
- int (*pre)(node_t *, void *),
|
|
126
|
|
- int (*post)(node_t *, void *),
|
|
|
116
|
+int node_walk(struct node *n,
|
|
|
117
|
+ int (*pre)(struct node *, void *),
|
|
|
118
|
+ int (*post)(struct node *, void *),
|
|
127
|
119
|
void *ctx)
|
|
128
|
120
|
{
|
|
129
|
121
|
int err = 0;
|
|
|
@@ -131,11 +123,11 @@ int node_walk(node_t *n,
|
|
131
|
123
|
if (pre && (err = pre(n, ctx)))
|
|
132
|
124
|
return err;
|
|
133
|
125
|
|
|
134
|
|
- if (n->ntype == N_LIST) {
|
|
135
|
|
- node_t *c;
|
|
|
126
|
+ if (n->ntype == N_EXPR) {
|
|
|
127
|
+ struct node *arg;
|
|
136
|
128
|
|
|
137
|
|
- for (c = n->list; c; c = c->next) {
|
|
138
|
|
- err = node_walk(c, pre, post, ctx);
|
|
|
129
|
+ node_expr_foreach(n, arg) {
|
|
|
130
|
+ err = node_walk(arg, pre, post, ctx);
|
|
139
|
131
|
if (err)
|
|
140
|
132
|
return err;
|
|
141
|
133
|
}
|
|
|
@@ -147,7 +139,7 @@ int node_walk(node_t *n,
|
|
147
|
139
|
return 0;
|
|
148
|
140
|
}
|
|
149
|
141
|
|
|
150
|
|
-int node_replace(node_t *n, node_t *new)
|
|
|
142
|
+int node_replace(struct node *n, struct node *new)
|
|
151
|
143
|
{
|
|
152
|
144
|
new->up = n->up;
|
|
153
|
145
|
|
|
|
@@ -167,148 +159,77 @@ int node_replace(node_t *n, node_t *new)
|
|
167
|
159
|
|
|
168
|
160
|
/* constructors */
|
|
169
|
161
|
|
|
170
|
|
-static node_t *__node(ntype_t ntype)
|
|
|
162
|
+static struct node *node_new(enum ntype ntype)
|
|
171
|
163
|
{
|
|
172
|
|
- node_t *n = calloc(1, sizeof(*n));
|
|
|
164
|
+ struct node *n;
|
|
|
165
|
+
|
|
|
166
|
+ n = calloc(1, sizeof(*n));
|
|
173
|
167
|
assert(n);
|
|
174
|
168
|
|
|
175
|
|
- n->ntype = ntype;
|
|
|
169
|
+ n->ntype = N_IDENT;
|
|
|
170
|
+ n->ident.name = name;
|
|
176
|
171
|
return n;
|
|
177
|
172
|
}
|
|
178
|
173
|
|
|
179
|
|
-node_t *node_list(node_t *head)
|
|
|
174
|
+struct node *node_string(char *data)
|
|
180
|
175
|
{
|
|
181
|
|
- node_t *n = __node(N_LIST);
|
|
|
176
|
+ struct node *n = node_new(N_STRING);
|
|
182
|
177
|
|
|
183
|
|
- n->list = head;
|
|
184
|
|
- head->up = n;
|
|
|
178
|
+ n->string.data = data;
|
|
185
|
179
|
return n;
|
|
186
|
180
|
}
|
|
187
|
181
|
|
|
188
|
|
-node_t *node_vlist(node_t *head, ...)
|
|
|
182
|
+struct node *node_num(int64_t num)
|
|
189
|
183
|
{
|
|
190
|
|
- va_list ap;
|
|
191
|
|
- node_t *n, *next;
|
|
192
|
|
-
|
|
193
|
|
- n = node_list(head);
|
|
194
|
|
-
|
|
195
|
|
- va_start(ap, head);
|
|
196
|
|
- while ((next = va_arg(ap, node_t *))) {
|
|
197
|
|
- insque(next, head);
|
|
198
|
|
- next->up = n;
|
|
199
|
|
- head = next;
|
|
200
|
|
- }
|
|
201
|
|
- va_end(ap);
|
|
|
184
|
+ struct node *n = node_new(N_NUM);
|
|
202
|
185
|
|
|
|
186
|
+ n->num.num = num;
|
|
203
|
187
|
return n;
|
|
204
|
188
|
}
|
|
205
|
189
|
|
|
206
|
|
-node_t *node_keyword(kwclass_t class, int op)
|
|
|
190
|
+struct node *node_ident(char *name)
|
|
207
|
191
|
{
|
|
208
|
|
- node_t *n = __node(N_KEYWORD);
|
|
209
|
|
- n->keyword.class = class;
|
|
210
|
|
- n->keyword.op = op;
|
|
211
|
|
- return n;
|
|
212
|
|
-}
|
|
|
192
|
+ struct node *n = node_new(N_IDENT);
|
|
213
|
193
|
|
|
214
|
|
-node_t *node_ident(const char *name)
|
|
215
|
|
-{
|
|
216
|
|
- node_t *n = __node(N_IDENT);
|
|
217
|
|
- n->ident = name;
|
|
|
194
|
+ n->ident.name = name;
|
|
218
|
195
|
return n;
|
|
219
|
196
|
}
|
|
220
|
197
|
|
|
221
|
|
-node_t *node_num(int64_t num)
|
|
|
198
|
+struct node *node_append(struct node *n, struct node *arg)
|
|
222
|
199
|
{
|
|
223
|
|
- node_t *n = __node(N_NUM);
|
|
224
|
|
- n->num = num;
|
|
|
200
|
+ struct node *last;
|
|
|
201
|
+ assert(n->ntype == N_EXPR);
|
|
225
|
202
|
|
|
226
|
|
- /* TODO: handle L UL ULL and such nonsense */
|
|
227
|
|
- n->type = &t_s64;
|
|
228
|
|
- return n;
|
|
229
|
|
-}
|
|
|
203
|
+ arg->up = n;
|
|
230
|
204
|
|
|
231
|
|
-node_t *node_string(const char *string)
|
|
232
|
|
-{
|
|
233
|
|
- node_t *n = __node(N_STRING);
|
|
234
|
|
- n->string = string;
|
|
|
205
|
+ if (!n->expr.args) {
|
|
|
206
|
+ n->expr.args = arg;
|
|
|
207
|
+ return n;
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ for (last = n->expr.args; last->next; last = last->next);
|
|
235
|
211
|
|
|
236
|
|
- /* TODO: string type like dtrace? */
|
|
237
|
|
- n->type = type_ptr_of(&t_char);
|
|
|
212
|
+ last->next = arg;
|
|
|
213
|
+ arg->prev = last;
|
|
238
|
214
|
return n;
|
|
239
|
215
|
}
|
|
240
|
216
|
|
|
241
|
|
-/* static struct node *node_new(enum ntype ntype) */
|
|
242
|
|
-/* { */
|
|
243
|
|
-/* struct node *n; */
|
|
244
|
|
-
|
|
245
|
|
-/* n = calloc(1, sizeof(*n)); */
|
|
246
|
|
-/* assert(n); */
|
|
247
|
|
-
|
|
248
|
|
-/* n->ntype = N_IDENT; */
|
|
249
|
|
-/* n->ident.name = name; */
|
|
250
|
|
-/* return n; */
|
|
251
|
|
-/* } */
|
|
252
|
|
-
|
|
253
|
|
-/* struct node *node_string(char *data) */
|
|
254
|
|
-/* { */
|
|
255
|
|
-/* struct node *n = node_new(N_STRING); */
|
|
256
|
|
-
|
|
257
|
|
-/* n->string.data = data; */
|
|
258
|
|
-/* return n; */
|
|
259
|
|
-/* } */
|
|
260
|
|
-
|
|
261
|
|
-/* struct node *node_num(int64_t num) */
|
|
262
|
|
-/* { */
|
|
263
|
|
-/* struct node *n = node_new(N_NUM); */
|
|
264
|
|
-
|
|
265
|
|
-/* n->num.num = num; */
|
|
266
|
|
-/* return n; */
|
|
267
|
|
-/* } */
|
|
268
|
|
-
|
|
269
|
|
-/* struct node *node_ident(char *name) */
|
|
270
|
|
-/* { */
|
|
271
|
|
-/* struct node *n = node_new(N_IDENT); */
|
|
272
|
|
-
|
|
273
|
|
-/* n->ident.name = name; */
|
|
274
|
|
-/* return n; */
|
|
275
|
|
-/* } */
|
|
276
|
|
-
|
|
277
|
|
-/* struct node *node_append(struct node *n, struct node *arg) */
|
|
278
|
|
-/* { */
|
|
279
|
|
-/* struct node *last; */
|
|
280
|
|
-/* assert(n->ntype == N_EXPR); */
|
|
281
|
|
-
|
|
282
|
|
-/* arg->up = n; */
|
|
283
|
|
-
|
|
284
|
|
-/* if (!n->expr.args) { */
|
|
285
|
|
-/* n->expr.args = arg; */
|
|
286
|
|
-/* return n; */
|
|
287
|
|
-/* } */
|
|
288
|
|
-
|
|
289
|
|
-/* for (last = n->expr.args; last->next; last = last->next); */
|
|
290
|
|
-
|
|
291
|
|
-/* last->next = arg; */
|
|
292
|
|
-/* arg->prev = last; */
|
|
293
|
|
-/* return n; */
|
|
294
|
|
-/* } */
|
|
295
|
|
-
|
|
296
|
|
-/* struct node *node_expr(char *func, ...) */
|
|
297
|
|
-/* { */
|
|
298
|
|
-/* va_list ap; */
|
|
299
|
|
-/* struct node *n, *arg; */
|
|
|
217
|
+struct node *node_expr(char *func, ...)
|
|
|
218
|
+{
|
|
|
219
|
+ va_list ap;
|
|
|
220
|
+ struct node *n, *arg;
|
|
300
|
221
|
|
|
301
|
|
-/* n = node_new(N_EXPR); */
|
|
|
222
|
+ n = node_new(N_EXPR);
|
|
302
|
223
|
|
|
303
|
|
-/* n->expr.func = func; */
|
|
|
224
|
+ n->expr.func = func;
|
|
304
|
225
|
|
|
305
|
|
-/* va_start(ap, func); */
|
|
|
226
|
+ va_start(ap, func);
|
|
306
|
227
|
|
|
307
|
|
-/* while ((arg = va_arg(ap, struct node *))) */
|
|
308
|
|
-/* append(n, arg); */
|
|
|
228
|
+ while ((arg = va_arg(ap, struct node *)))
|
|
|
229
|
+ append(n, arg);
|
|
309
|
230
|
|
|
310
|
|
-/* va_end(ap); */
|
|
|
231
|
+ va_end(ap);
|
|
311
|
232
|
|
|
312
|
|
-/* return n; */
|
|
|
233
|
+ return n;
|
|
313
|
234
|
|
|
314
|
|
-/* } */
|
|
|
235
|
+}
|