Selaa lähdekoodia

create structs for multi-keyed maps

Tobias Waldekranz.com 8 vuotta sitten
vanhempi
commit
3a91720c0e
4 muutettua tiedostoa jossa 97 lisäystä ja 23 poistoa
  1. 24 5
      global.c
  2. 5 0
      ply.c
  3. 67 18
      type.c
  4. 1 0
      type.h

+ 24 - 5
global.c

@@ -1,5 +1,7 @@
1
+#define _GNU_SOURCE 		/* asprintf */
1 2
 #include <assert.h>
2 3
 #include <errno.h>
4
+#include <stdlib.h>
3 5
 #include <string.h>
4 6
 
5 7
 #include "arch.h"
@@ -14,15 +16,34 @@ static struct type *global_map_ktype(struct node *n)
14 16
 {
15 17
 	struct node *map, *key;
16 18
 	struct type *ktype;
17
-	int nargs = node_nargs(n);
19
+	struct tfield *kfields, *f;
20
+	int i, nargs = node_nargs(n);
21
+	char *kname;
18 22
 
19 23
 	map = n->expr.args;
20 24
 
21 25
 	if (node_nargs(n) == 2)
22 26
 		return map->next->sym->type;
23 27
 
24
-	assert(0);
25
-	return NULL;
28
+	ktype = calloc(1, sizeof(*ktype));
29
+	assert(ktype);
30
+
31
+	kfields = calloc(nargs, sizeof(*kfields));
32
+	assert(kfields);
33
+
34
+	for (key = map->next, f = kfields, i = 0; key; key = key->next, f++, i++) {
35
+		asprintf(&f->name, "k%d", i);
36
+		f->type = key->sym->type;
37
+	}
38
+
39
+	f->type = &t_void;
40
+
41
+	asprintf(&ktype->sou.name, ":%s_key", map->ident.name);
42
+	ktype->ttype = T_STRUCT;
43
+	ktype->sou.fields = kfields;
44
+
45
+	type_add(ktype);
46
+	return ktype;
26 47
 }
27 48
 
28 49
 static int global_assign_type_infer_map(struct node *n)
@@ -33,10 +54,8 @@ static int global_assign_type_infer_map(struct node *n)
33 54
 	map = n->expr.args;
34 55
 
35 56
 	for (key = map->next; key; key = key->next) {
36
-		node_print(key, stdout);printf("WKZ\n");
37 57
 		if (type_sizeof(key->sym->type) < 0)
38 58
 			return 0;
39
-		node_print(key, stdout);printf("WKZ\n");
40 59
 	}
41 60
 
42 61
 	map->sym->type = type_map_of(global_map_ktype(n), n->sym->type);

+ 5 - 0
ply.c

@@ -8,6 +8,7 @@
8 8
 #include "node.h"
9 9
 #include "ply.h"
10 10
 #include "sym.h"
11
+#include "type.h"
11 12
 
12 13
 struct providers {
13 14
 	struct provider **ps;
@@ -84,6 +85,7 @@ struct ctx *ctx_get(void)
84 85
 				    node_expr(":map",
85 86
 					      node_ident("t"),
86 87
 					      node_num(0),
88
+					      node_expr("pid", NULL),
87 89
 					      NULL),
88 90
 				    node_expr("time", NULL),
89 91
 				    NULL),
@@ -527,6 +529,9 @@ int main(void)
527 529
 	printf("\n\n-- globals\n");
528 530
 	symtab_dump(ctx->globals, stdout);
529 531
 
532
+	printf("\n\n-- decls\n");
533
+	type_dump_decls(stdout);
534
+
530 535
 	if (err)
531 536
 		printf("ERR: %d\n", err);
532 537
 

+ 67 - 18
type.c

@@ -6,15 +6,34 @@
6 6
 
7 7
 #include "type.h"
8 8
 
9
+static void __sgr(FILE *fp, int sgr, const char *s)
10
+{
11
+	if (!s)
12
+		return;
13
+
14
+	fprintf(fp, "\e[%dm%s\e[0m", sgr, s);
15
+}
16
+
17
+static void __bold(FILE *fp, const char *s)
18
+{
19
+	__sgr(fp, 1, s);
20
+}
21
+
22
+static void __faint(FILE *fp, const char *s)
23
+{
24
+	__sgr(fp, 2, s);
25
+}
26
+
9 27
 static void type_dump_func(struct type *t, const char *name, FILE *fp)
10 28
 {
11 29
 	struct tfield *arg;
12 30
 
13 31
 	type_dump(t->func.type, NULL, fp);
14
-	fprintf(fp, " (*%s)(", name ? : "");
32
+	fprintf(fp, " (*\e[1m%s\e[0m)(", name ? : "");
15 33
 
16 34
 	if (!t->func.args) {
17
-		fputs("void)", fp);
35
+		__faint(fp, "void");
36
+		fputc(')', fp);
18 37
 		return;
19 38
 	}
20 39
 
@@ -37,33 +56,46 @@ void type_dump(struct type *t, const char *name, FILE *fp)
37 56
 	switch (t->ttype){
38 57
 	case T_VOID:
39 58
 	print_void:
40
-		fprintf(fp, "\e[2mvoid\e[0m%s%s", name? " " : "", name ? : "");
59
+		__faint(fp, "void");
60
+		fputs(name ? " " : "", fp);
61
+		__bold(fp, name);
41 62
 		break;
42 63
 	case T_TYPEDEF:
43
-		fprintf(fp, "\e[2m%s\e[0m%s%s", t->tdef.name, name? " " : "", name ? : "");
64
+		__faint(fp, t->tdef.name);
65
+		fputs(name ? " " : "", fp);
66
+		__bold(fp, name);
44 67
 		break;
45 68
 	case T_SCALAR:
46
-		fprintf(fp, "\e[2m%s\e[0m%s%s", t->scalar.name, name? " " : "", name ? : "");
69
+		__faint(fp, t->scalar.name);
70
+		fputs(name ? " " : "", fp);
71
+		__bold(fp, name);
47 72
 		break;
48 73
 	case T_POINTER:
49 74
 		type_dump(t->ptr.type, NULL, fp);
50
-		fprintf(fp, " *%s", name ? : "");
75
+		fputs(" *", fp);
76
+		__bold(fp, name);
51 77
 		break;
52 78
 	case T_ARRAY:
53 79
 		type_dump(t->array.type, NULL, fp);
54
-		fprintf(fp, "%s%s[%zu]", name ? " " : "", name? : "", t->array.len);
80
+		fputs(name ? " " : "", fp);
81
+		__bold(fp, name);
82
+		fprintf(fp, "[%zu]", t->array.len);
55 83
 		break;
56 84
 	case T_STRUCT:
57
-		fprintf(fp, "struct %s%s%s", t->sou.name, name? " " : "", name? : "");
85
+		fputs("struct ", fp);
86
+		__faint(fp, t->sou.name);
87
+		__bold(fp, name);
58 88
 		break;
59 89
 	case T_FUNC:
60 90
 		type_dump_func(t, name, fp);
61 91
 		break;
62 92
 	case T_MAP:
63 93
 		type_dump(t->map.vtype, NULL, fp);
64
-		fprintf(fp, " %s{", name ? : "");
94
+		fputs(name ? " " : "", fp);		
95
+		__bold(fp, name);
96
+		fputc('{', fp);
65 97
 		type_dump(t->map.ktype, NULL, fp);
66
-		fputs("} ", fp);
98
+		fputc('}', fp);
67 99
 		break;
68 100
 	}
69 101
 }
@@ -74,7 +106,7 @@ static void type_dump_decl_sou(struct type *t, FILE *fp)
74 106
 
75 107
 	type_dump(t, NULL, fp);
76 108
 	fputs(" {\n", fp);
77
-	for (f = t->sou.fields; f->type; f++) {
109
+	for (f = t->sou.fields; f->type->ttype != T_VOID; f++) {
78 110
 		fputc('\t', fp);
79 111
 		type_dump(f->type, NULL, fp);
80 112
 		fprintf(fp, " %s;\n", f->name);
@@ -301,7 +333,21 @@ struct type_list {
301 333
 } all_types;
302 334
 
303 335
 #define types_foreach(_t) \
304
-	for ((_t) = all_types.types[0]; (_t) <= all_types.types[all_types.len]; (_t)++)
336
+	for ((_t) = all_types.types; (_t) < &all_types.types[all_types.len]; (_t)++)
337
+
338
+void type_dump_decls(FILE *fp)
339
+{
340
+	struct type **ti, *t;
341
+
342
+	types_foreach(ti) {
343
+		t = *ti;
344
+		if (t->ttype == T_SCALAR)
345
+			continue;
346
+
347
+		type_dump_decl(t, fp);
348
+		fputc('\n', fp);
349
+	}
350
+}
305 351
 
306 352
 int type_add(struct type *t)
307 353
 {
@@ -334,9 +380,10 @@ int type_add_list(struct type **ts)
334 380
 
335 381
 struct type *type_array_of(struct type *type, size_t len)
336 382
 {
337
-	struct type *t;
383
+	struct type **ti, *t;
338 384
 
339
-	types_foreach(t) {
385
+	types_foreach(ti) {
386
+		t = *ti;
340 387
 		if ((t->ttype == T_ARRAY)
341 388
 		    && (t->array.type == type)
342 389
 		    && (t->array.len == len))
@@ -353,9 +400,10 @@ struct type *type_array_of(struct type *type, size_t len)
353 400
 
354 401
 struct type *type_map_of(struct type *ktype, struct type *vtype)
355 402
 {
356
-	struct type *t;
403
+	struct type **ti, *t;
357 404
 
358
-	types_foreach(t) {
405
+	types_foreach(ti) {
406
+		t = *ti;
359 407
 		if ((t->ttype == T_MAP)
360 408
 		    && (t->map.ktype == ktype)
361 409
 		    && (t->map.vtype == vtype))
@@ -372,9 +420,10 @@ struct type *type_map_of(struct type *ktype, struct type *vtype)
372 420
 
373 421
 struct type *type_ptr_of(struct type *type)
374 422
 {
375
-	struct type *t;
423
+	struct type **ti, *t;
376 424
 
377
-	types_foreach(t) {
425
+	types_foreach(ti) {
426
+		t = *ti;
378 427
 		if ((t->ttype == T_POINTER)
379 428
 		    && (t->ptr.type == type))
380 429
 			return t;

+ 1 - 0
type.h

@@ -83,6 +83,7 @@ int type_compatible(struct type *a, struct type *b);
83 83
 
84 84
 void type_dump     (struct type *t, const char *name, FILE *fp);
85 85
 void type_dump_decl(struct type *t, FILE *fp);
86
+void type_dump_decls(FILE *fp);
86 87
 
87 88
 ssize_t type_alignof(struct type *t);
88 89
 ssize_t type_offsetof(struct type *t, const char *field);