ソースを参照

save before ast rework

Tobias Waldekranz.com 8 年 前
コミット
0f71ebffc9
共有3 個のファイルを変更した165 個の追加286 個の削除を含む
  1. 85 164
      node.c
  2. 58 96
      node.h
  3. 22 26
      ply.c

+ 85 - 164
node.c

@@ -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
+}

+ 58 - 96
node.h

@@ -8,123 +8,85 @@
8 8
 #include "sym.h"
9 9
 #include "type.h"
10 10
 
11
-/* enum ntype { */
12
-/* 	N_EXPR, */
13
-/* 	N_IDENT, */
14
-/* 	N_NUM, */
15
-/* 	N_STRING, */
16
-/* }; */
17
-
18
-/* struct node { */
19
-/* 	struct node *next, *prev, *up; */
20
-
21
-/* 	enum ntype ntype; */
22
-
23
-/* 	union { */
24
-/* 		struct { */
25
-/* 			char *func; */
26
-/* 			struct node *args; */
27
-/* 		} expr; */
28
-/* 		struct { */
29
-/* 			char *name; */
30
-/* 		} ident; */
31
-/* 		struct { */
32
-/* 			int64_t num; */
33
-/* 		} num; */
34
-/* 		struct { */
35
-/* 			char *data; */
36
-/* 		} string; */
37
-/* 	}; */
38
-/* }; */
39
-	
11
+/* REEEEEEEEEEEEEEMOVE */
40 12
 typedef struct node node_t;
41 13
 
42
-typedef enum kwclass {
43
-	KW_SUBSCRIPT = '[',
44
-	KW_ASSIGN = '=',
45
-	KW_BINOP = 'b',
46
-} kwclass_t;
47
-
48
-typedef enum kwarith {
49
-	KW_ADD = '+',
50
-	KW_SUB = '-',
51
-	KW_MUL = '*',
52
-	KW_DIV = '/',
53
-} kwarith_t;
54
-
55
-typedef struct keyword {
56
-	kwclass_t class;
57
-	int op;
58
-} keyword_t;
59
-
60
-typedef enum ntype {
61
-	N_LIST,
62
-	N_KEYWORD,
14
+enum ntype {
15
+	N_EXPR,
63 16
 	N_IDENT,
64 17
 	N_NUM,
65 18
 	N_STRING,
66
-} ntype_t;
19
+};
67 20
 
68 21
 struct node {
69
-	node_t *next, *prev, *up;
22
+	struct node *next, *prev, *up;
23
+
24
+	enum ntype ntype;
70 25
 
71
-	ntype_t ntype;
72 26
 	union {
73
-		node_t *list;
74
-		keyword_t keyword;
75
-		const char *ident;
76
-		int64_t num;
77
-		const char *string;
27
+		struct {
28
+			char *func;
29
+			struct node *args;
30
+		} expr;
31
+		struct {
32
+			char *name;
33
+		} ident;
34
+		struct {
35
+			int64_t num;
36
+		} num;
37
+		struct {
38
+			char *data;
39
+		} string;
78 40
 	};
79 41
 
42
+
80 43
 	type_t *type;
81 44
 	sym_t *sym;
82
-
83 45
 	irstate_t irs;
84 46
 };
85 47
 
86 48
 /* debug */
87
-void node_print(node_t *n, FILE *fp);
88
-void node_dump (node_t *n, FILE *fp);
49
+void node_print(struct node *n, FILE *fp);
50
+void node_dump (struct node *n, FILE *fp);
89 51
 
90
-typedef int (*walk_fn)(node_t *, void *);
91
-int node_walk(node_t *n, walk_fn pre, walk_fn post, void *ctx);
52
+typedef int (*walk_fn)(struct node *, void *);
53
+int node_walk(struct node *n, walk_fn pre, walk_fn post, void *ctx);
92 54
 
93
-int node_replace(node_t *n, node_t *new);
55
+int node_replace(struct node *n, struct node *new);
94 56
 
57
+#define node_expr_foreach(_expr, _arg) \
58
+	for ((_arg) = (_expr)->args; (_arg); (_arg) = (_arg)->next)
95 59
 
96 60
 /* constructors */
97
-node_t *node_list  (node_t *head);
98
-node_t *node_vlist (node_t *head, ...);
99
-
100
-node_t *node_keyword(kwclass_t class, int op);
101
-node_t *node_ident  (const char *name);
102
-node_t *node_num    (int64_t num);
103
-node_t *node_string (const char *string);
104
-
105
-static inline node_t *node_head(node_t *n)
106
-{
107
-	if (!n)
108
-		return NULL;
109
-
110
-	for (; n->prev; n = n->prev);
111
-
112
-	return n;
113
-}
114
-
115
-static inline node_t *node_prev(node_t *n)
116
-{
117
-	return n ? n->prev : NULL;
118
-}
119
-
120
-static inline node_t *node_next(node_t *n)
121
-{
122
-	return n ? n->next : NULL;
123
-}
124
-
125
-static inline node_t *node_up(node_t *n)
126
-{
127
-	return n ? n->up : NULL;
128
-}
61
+struct node *node_string(char *data);
62
+struct node *node_num   (int64_t num);
63
+struct node *node_ident (char *name);
64
+struct node *node_append(struct node *n, struct node *arg);
65
+struct node *node_expr  (char *func, ...);
66
+
67
+/* static inline node_t *node_head(node_t *n) */
68
+/* { */
69
+/* 	if (!n) */
70
+/* 		return NULL; */
71
+
72
+/* 	for (; n->prev; n = n->prev); */
73
+
74
+/* 	return n; */
75
+/* } */
76
+
77
+/* static inline node_t *node_prev(node_t *n) */
78
+/* { */
79
+/* 	return n ? n->prev : NULL; */
80
+/* } */
81
+
82
+/* static inline node_t *node_next(node_t *n) */
83
+/* { */
84
+/* 	return n ? n->next : NULL; */
85
+/* } */
86
+
87
+/* static inline node_t *node_up(node_t *n) */
88
+/* { */
89
+/* 	return n ? n->up : NULL; */
90
+/* } */
129 91
 
130 92
 #endif	/* _PLY_NODE_H */

+ 22 - 26
ply.c

@@ -65,24 +65,22 @@ ctx_t *ctx_get(void)
65 65
 	prog->probe = "k:SyS_read";
66 66
 	/* { @t[0] = time(); @reads[pid()] = quantize(arg2) } */
67 67
 	prog->ast =
68
-		node_vlist(
69
-			node_vlist(node_keyword('=', 0),
70
-				   node_vlist(node_keyword('[', 0),
68
+		node_expr("",
69
+			  node_expr("=",
70
+				    node_expr("[]",
71 71
 					      node_ident("@t"),
72 72
 					      node_num(0),
73 73
 					      NULL),
74
-				   node_list(node_ident("time")),
75
-				   NULL),
76
-			node_vlist(node_keyword('=', 0),
77
-				   node_vlist(node_keyword('[', 0),
74
+				    node_expr("time"),
75
+				    NULL),
76
+			  node_expr("=",
77
+				    node_expr("[]",
78 78
 					      node_ident("@reads"),
79
-					      node_list(node_ident("pid")),
79
+					      node_expr("pid", NULL),
80 80
 					      NULL),
81
-				   node_vlist(node_ident("quantize"),
82
-					      node_ident("arg2"),
83
-					      NULL),
84
-				   NULL),
85
-			NULL);
81
+				    node_expr("quantize", node_ident("arg2"), NULL),
82
+				    NULL),
83
+			  NULL);
86 84
 
87 85
 	prog->provider = provider_get("k");
88 86
 	prog->provider->probe(prog);
@@ -98,20 +96,18 @@ ctx_t *ctx_get(void)
98 96
 	prog->probe = "k:SyS_read2"; 
99 97
 	/* { @times[pid()] = quantize(time() - t0) } */
100 98
 	prog->ast =
101
-		node_list(
102
-			node_vlist(node_keyword('=', 0),
103
-				   node_vlist(node_keyword('[', 0),
104
-					      node_ident("@times"),
105
-					      node_list(node_ident("pid")),
106
-					      NULL),
107
-				   node_vlist(node_ident("quantize"),
108
-					      node_vlist(node_keyword('b', '-'),
109
-							 node_list(node_ident("time")),
110
-							 node_ident("t0"),
111
-							 NULL),
99
+		node_expr("=",
100
+			  node_expr("[]",
101
+				    node_ident("@times"),
102
+				    node_expr("pid", NULL),
103
+				    NULL),
104
+			  node_expr("quantize",
105
+				    node_expr("-",
106
+					      node_expr("time", NULL),
107
+					      node_ident("t0"),
112 108
 					      NULL),
113
-				   NULL)
114
-			);
109
+				    NULL),
110
+			  NULL);
115 111
 
116 112
 	prog->provider = provider_get("k");
117 113
 	prog->provider->probe(prog);