Browse Source

work work

Tobias Waldekranz.com 8 years ago
parent
commit
c95960155a
7 changed files with 82 additions and 92 deletions
  1. 5 6
      func.c
  2. 7 14
      global.c
  3. 12 64
      node.c
  4. 0 3
      node.h
  5. 3 4
      ply.c
  6. 12 0
      ply.h
  7. 43 1
      printxf_test.c

+ 5 - 6
func.c

@@ -2,6 +2,7 @@
2 2
 
3 3
 #include "func.h"
4 4
 #include "node.h"
5
+#include "ply.h"
5 6
 #include "sym.h"
6 7
 #include "type.h"
7 8
 
@@ -18,8 +19,8 @@ int func_validate_expr(const struct func *func, struct node *n, int strict)
18 19
 		    || type_compatible(arg->sym->type, f->type) )
19 20
 			continue;
20 21
 
21
-		node_error(n, stderr, "type mismatch for argument %d, expected %T, got %T",
22
-			   nargs, f->type, arg->sym->type);
22
+		_e("%#N: %O argument is of type '%T', expected '%T'.\n",
23
+		   n, nargs, arg->sym->type, f->type);
23 24
 	}
24 25
 
25 26
 	return 0;
@@ -33,16 +34,14 @@ int func_static_validate(const struct func *func, struct node *n)
33 34
 	switch (n->ntype) {
34 35
 	case N_EXPR:
35 36
 		if (func->type->ttype != T_FUNC) {
36
-			node_error(n, stderr, "'%s' is not callable",
37
-				   n->expr.func);
37
+			_e("%#N: %N is not callable.\n", n, n);
38 38
 			return -EINVAL;
39 39
 		}
40 40
 		return func_validate_expr(func, n, 0);
41 41
 
42 42
 	case N_IDENT:
43 43
 		if (func->type->ttype == T_FUNC) {
44
-			node_error(n, stderr, "'%s' is a function",
45
-				   n->ident.name);
44
+			_e("%#N: %N is a function.\n", n, n);
46 45
 			return -EINVAL;
47 46
 		}
48 47
 		/* fall-through */

+ 7 - 14
global.c

@@ -65,13 +65,13 @@ static int global_map_static_verify(struct node *n)
65 65
 {
66 66
 	int nargs = node_nargs(n);
67 67
 
68
-	if (nargs < 2) {
69
-		node_error(n, stderr, "map without key");
68
+	if (nargs < 2) {		
69
+		_e("%#N: map without key.\n", n);
70 70
 		return -EINVAL;
71 71
 	}
72 72
 
73 73
 	if (n->expr.args->ntype != N_IDENT) {
74
-		node_error(n, stderr, "map must be identifier");
74
+		_e("%#N: %N is not subscriptable.\n", n, n);
75 75
 		return -EINVAL;
76 76
 	}
77 77
 
@@ -120,14 +120,8 @@ static int global_assign_type_infer(struct prog *prog, struct node *n)
120 120
 	if (type_compatible(lval->sym->type, rval->sym->type))
121 121
 		return 0;
122 122
 
123
-	if (lval->ntype == N_IDENT) {
124
-		node_error(n, stderr, "conflicting types for '%s'",
125
-			   lval->ident.name);
126
-	} else {
127
-		node_error(n, stderr, "conflicting types for '%s'",
128
-			   lval->expr.args->ident.name);
129
-	}
130
-
123
+	_e("%#N: can't assign %N (type '%T'), to %N (type '%T').\n",
124
+	   n, rval, rval->sym->type, lval, lval->sym->type);
131 125
 	return -EINVAL;
132 126
 }
133 127
 
@@ -137,8 +131,7 @@ static int global_assign_static_verify(struct node *n)
137 131
 	int nargs = node_nargs(n);
138 132
 
139 133
 	if (nargs != 2) {
140
-		node_error(n, stderr, "assignment operator expects 2 arguments,"
141
-			   " %d given", nargs);
134
+		_e("%#N: expected 2 arguments, %d given.\n", n, nargs);
142 135
 		return -EINVAL;
143 136
 	}
144 137
 
@@ -147,7 +140,7 @@ static int global_assign_static_verify(struct node *n)
147 140
 	if (node_is_map(lval) || (lval->ntype == N_IDENT))
148 141
 		return 0;
149 142
 
150
-	node_error(n, stderr, "assignment destination must be a variable or map");
143
+	_e("%#N: %N is not assignable.\n", n, lval);
151 144
 	return -EINVAL;
152 145
 }
153 146
 

+ 12 - 64
node.c

@@ -8,6 +8,7 @@
8 8
 #include <string.h>
9 9
 
10 10
 #include "node.h"
11
+#include "printxf.h"
11 12
 
12 13
 void node_print(struct node *n, FILE *fp)
13 14
 {
@@ -30,74 +31,15 @@ void node_print(struct node *n, FILE *fp)
30 31
 	}
31 32
 }
32 33
 
33
-struct node_dump_info {
34
-	FILE *fp;
35
-	int indent;
36
-};
37
-
38
-int __node_dump_pre(struct node *n, void *_info)
39
-{
40
-	struct node_dump_info *info = _info;
41
-	struct node *arg;
42
-
43
-	if (n->prev)
44
-		fputc(' ', info->fp);
45
-
46
-	if (node_is_block(n->up)) {
47
-		info->indent += 4;
48
-		fprintf(info->fp, "\n%*s", info->indent, "");
49
-	}
50
-
51
-	if (n->ntype == N_EXPR)
52
-		fputc('(', info->fp);
53
-
54
-	node_print(n, info->fp);
55
-
56
-	if ((n->ntype == N_EXPR) && n->expr.args)
57
-		fputc(' ', info->fp);
58
-
59
-	return 0;
60
-}
61
-
62
-int __node_dump_post(struct node *n, void *_info)
63
-{
64
-	struct node_dump_info *info = _info;
65
-	struct node *c;
66
-
67
-	if (node_is_block(n))
68
-		fprintf(info->fp, "\n%*s", info->indent, "");
69
-
70
-	if (n->ntype == N_EXPR)
71
-		fputc(')', info->fp);
72
-
73
-	if (node_is_block(n->up))
74
-		info->indent -= 4;
75
-	
76
-	return 0;
77
-}
78
-
79
-void node_dump(struct node *n, FILE *fp)
34
+int node_vfprintxf(struct printxf *pxf, FILE *fp, const char *spec, va_list ap)
80 35
 {
81
-	struct node_dump_info info = {
82
-		.fp = fp,
83
-	};
36
+	struct node *n;
84 37
 
85
-	node_walk(n, __node_dump_pre, __node_dump_post, &info);
86
-	fputc('\n', fp);		
87
-}
88
-
89
-void node_error(struct node *n, FILE *fp, const char *fmt, ...)
90
-{
91
-	va_list ap;
38
+	n = va_arg(ap, struct node *);
92 39
 
40
+	/* TODO: "%#N" should print source file location */
93 41
 	node_print(n, fp);
94
-	fputs(": ", fp);
95
-
96
-	va_start(ap, fmt);
97
-	vfprintf(fp, fmt, ap);
98
-	va_end(ap);
99
-
100
-	fputc('\n', fp);
42
+	return 0;
101 43
 }
102 44
 
103 45
 int node_walk(struct node *n,
@@ -238,3 +180,9 @@ struct node *node_expr(char *func, ...)
238 180
 	return n;
239 181
 	
240 182
 }
183
+
184
+__attribute__((constructor))
185
+static void node_init(void)
186
+{
187
+	printxf_default.vfprintxf['N'] = node_vfprintxf;
188
+}

+ 0 - 3
node.h

@@ -40,9 +40,6 @@ struct node {
40 40
 
41 41
 /* debug */
42 42
 void node_print(struct node *n, FILE *fp);
43
-void node_dump (struct node *n, FILE *fp);
44
-
45
-void node_error(struct node *n, FILE *fp, const char *fmt, ...);
46 43
 
47 44
 typedef int (*nwalk_fn)(struct node *, void *);
48 45
 int node_walk(struct node *n, nwalk_fn pre, nwalk_fn post, void *ctx);

+ 3 - 4
ply.c

@@ -155,9 +155,8 @@ int pass_sym_alloc(struct node *n, void *_prog)
155 155
 	}
156 156
 
157 157
 	if (err) {
158
-		if ((err == -ENOENT) && (n->ntype == N_EXPR))
159
-			node_error(n, stderr, "unknown function '%s'",
160
-				   n->expr.func);
158
+		if ((err == -ENOENT))
159
+			_e("%#N: unknown symbol %N.\n", n, n);
161 160
 	}
162 161
 
163 162
 	return err;
@@ -519,7 +518,7 @@ int main(void)
519 518
 
520 519
 	for (prog = ctx->progs; *prog; prog++) {
521 520
 		printf("\n\e[34m%s\e[0m\n", (*prog)->probe);
522
-		node_dump((*prog)->ast, stdout);
521
+		ast_fprint(stdout, (*prog)->ast);
523 522
 		printf("\n-- locals\n");
524 523
 		symtab_dump((*prog)->locals, stdout);
525 524
 		/* printf("-- ir\n"); */

+ 12 - 0
ply.h

@@ -1,6 +1,8 @@
1 1
 #ifndef _PLY_H
2 2
 #define _PLY_H
3 3
 
4
+#include <stdio.h>
5
+
4 6
 struct node;
5 7
 struct symtab;
6 8
 
@@ -42,4 +44,14 @@ struct provider {
42 44
 
43 45
 void provider_register(struct provider *p);
44 46
 
47
+/* utils */
48
+void ast_fprint(FILE *fp, struct node *root);
49
+
50
+#include "printxf.h"
51
+
52
+#define _d(fmt, ...) fprintxf(NULL, stderr, fmt, ##__VA_ARGS__)
53
+#define _i(fmt, ...) fprintxf(NULL, stderr, fmt, ##__VA_ARGS__)
54
+#define _w(fmt, ...) fprintxf(NULL, stderr, fmt, ##__VA_ARGS__)
55
+#define _e(fmt, ...) fprintxf(NULL, stderr, fmt, ##__VA_ARGS__)
56
+
45 57
 #endif	/* _PLY_H */

+ 43 - 1
printxf_test.c

@@ -1,7 +1,49 @@
1
+#include <stdarg.h>
2
+#include <stdio.h>
3
+#include <stdlib.h>
4
+#include <string.h>
5
+
1 6
 #include "printxf.h"
2 7
 
3 8
 
9
+int printxf_test(const char *fmt, ...)
10
+{
11
+	va_list ap, xap;
12
+	FILE *fp, *xfp;
13
+	char *buf, *xbuf;
14
+	size_t loc, xloc;
15
+	int len, xlen;
16
+
17
+	 fp = open_memstream( &buf,  &loc);
18
+	xfp = open_memstream(&xbuf, &xloc);
19
+
20
+	va_start(ap, fmt);
21
+	va_copy(xap, ap);
22
+
23
+	len = vfprintf (        fp, fmt,  ap);
24
+	xlen = vfprintxf(NULL, xfp, fmt, xap);
25
+
26
+	va_end(xap);
27
+	va_end( ap);
28
+
29
+	fclose(xfp);
30
+	fclose( fp);
31
+
32
+	if ((xlen == len)
33
+	    && (xloc == loc)
34
+	    && !strcmp(xbuf, buf)) {
35
+		free(xbuf);
36
+		free( buf);
37
+		return 0;
38
+	}
39
+
40
+	printf("FAIL  len:%2d  loc:%2zd  buf:\"%s\"\n",  len,  loc,  buf);
41
+	printf("     xlen:%2d xloc:%2zd xbuf:\"%s\"\n", xlen, xloc, xbuf);
42
+	return -1;
43
+}
44
+
45
+
4 46
 int main(void)
5 47
 {
6
-	printxf(NULL, "testing %% %.*s %d\n", 3, "w000t", 13);
48
+	printxf_test("testing %% %.*s %d\n%", 3, "w000t", 13);
7 49
 }