Browse Source

create structs for multi-keyed maps

Tobias Waldekranz.com 8 years ago
parent
commit
3a91720c0e
4 changed files with 97 additions and 23 deletions
  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
+#define _GNU_SOURCE 		/* asprintf */
1
 #include <assert.h>
2
 #include <assert.h>
2
 #include <errno.h>
3
 #include <errno.h>
4
+#include <stdlib.h>
3
 #include <string.h>
5
 #include <string.h>
4
 
6
 
5
 #include "arch.h"
7
 #include "arch.h"
14
 {
16
 {
15
 	struct node *map, *key;
17
 	struct node *map, *key;
16
 	struct type *ktype;
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
 	map = n->expr.args;
23
 	map = n->expr.args;
20
 
24
 
21
 	if (node_nargs(n) == 2)
25
 	if (node_nargs(n) == 2)
22
 		return map->next->sym->type;
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
 static int global_assign_type_infer_map(struct node *n)
49
 static int global_assign_type_infer_map(struct node *n)
33
 	map = n->expr.args;
54
 	map = n->expr.args;
34
 
55
 
35
 	for (key = map->next; key; key = key->next) {
56
 	for (key = map->next; key; key = key->next) {
36
-		node_print(key, stdout);printf("WKZ\n");
37
 		if (type_sizeof(key->sym->type) < 0)
57
 		if (type_sizeof(key->sym->type) < 0)
38
 			return 0;
58
 			return 0;
39
-		node_print(key, stdout);printf("WKZ\n");
40
 	}
59
 	}
41
 
60
 
42
 	map->sym->type = type_map_of(global_map_ktype(n), n->sym->type);
61
 	map->sym->type = type_map_of(global_map_ktype(n), n->sym->type);

+ 5 - 0
ply.c

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

+ 67 - 18
type.c

6
 
6
 
7
 #include "type.h"
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
 static void type_dump_func(struct type *t, const char *name, FILE *fp)
27
 static void type_dump_func(struct type *t, const char *name, FILE *fp)
10
 {
28
 {
11
 	struct tfield *arg;
29
 	struct tfield *arg;
12
 
30
 
13
 	type_dump(t->func.type, NULL, fp);
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
 	if (!t->func.args) {
34
 	if (!t->func.args) {
17
-		fputs("void)", fp);
35
+		__faint(fp, "void");
36
+		fputc(')', fp);
18
 		return;
37
 		return;
19
 	}
38
 	}
20
 
39
 
37
 	switch (t->ttype){
56
 	switch (t->ttype){
38
 	case T_VOID:
57
 	case T_VOID:
39
 	print_void:
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
 		break;
62
 		break;
42
 	case T_TYPEDEF:
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
 		break;
67
 		break;
45
 	case T_SCALAR:
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
 		break;
72
 		break;
48
 	case T_POINTER:
73
 	case T_POINTER:
49
 		type_dump(t->ptr.type, NULL, fp);
74
 		type_dump(t->ptr.type, NULL, fp);
50
-		fprintf(fp, " *%s", name ? : "");
75
+		fputs(" *", fp);
76
+		__bold(fp, name);
51
 		break;
77
 		break;
52
 	case T_ARRAY:
78
 	case T_ARRAY:
53
 		type_dump(t->array.type, NULL, fp);
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
 		break;
83
 		break;
56
 	case T_STRUCT:
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
 		break;
88
 		break;
59
 	case T_FUNC:
89
 	case T_FUNC:
60
 		type_dump_func(t, name, fp);
90
 		type_dump_func(t, name, fp);
61
 		break;
91
 		break;
62
 	case T_MAP:
92
 	case T_MAP:
63
 		type_dump(t->map.vtype, NULL, fp);
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
 		type_dump(t->map.ktype, NULL, fp);
97
 		type_dump(t->map.ktype, NULL, fp);
66
-		fputs("} ", fp);
98
+		fputc('}', fp);
67
 		break;
99
 		break;
68
 	}
100
 	}
69
 }
101
 }
74
 
106
 
75
 	type_dump(t, NULL, fp);
107
 	type_dump(t, NULL, fp);
76
 	fputs(" {\n", fp);
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
 		fputc('\t', fp);
110
 		fputc('\t', fp);
79
 		type_dump(f->type, NULL, fp);
111
 		type_dump(f->type, NULL, fp);
80
 		fprintf(fp, " %s;\n", f->name);
112
 		fprintf(fp, " %s;\n", f->name);
301
 } all_types;
333
 } all_types;
302
 
334
 
303
 #define types_foreach(_t) \
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
 int type_add(struct type *t)
352
 int type_add(struct type *t)
307
 {
353
 {
334
 
380
 
335
 struct type *type_array_of(struct type *type, size_t len)
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
 		if ((t->ttype == T_ARRAY)
387
 		if ((t->ttype == T_ARRAY)
341
 		    && (t->array.type == type)
388
 		    && (t->array.type == type)
342
 		    && (t->array.len == len))
389
 		    && (t->array.len == len))
353
 
400
 
354
 struct type *type_map_of(struct type *ktype, struct type *vtype)
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
 		if ((t->ttype == T_MAP)
407
 		if ((t->ttype == T_MAP)
360
 		    && (t->map.ktype == ktype)
408
 		    && (t->map.ktype == ktype)
361
 		    && (t->map.vtype == vtype))
409
 		    && (t->map.vtype == vtype))
372
 
420
 
373
 struct type *type_ptr_of(struct type *type)
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
 		if ((t->ttype == T_POINTER)
427
 		if ((t->ttype == T_POINTER)
379
 		    && (t->ptr.type == type))
428
 		    && (t->ptr.type == type))
380
 			return t;
429
 			return t;

+ 1 - 0
type.h

83
 
83
 
84
 void type_dump     (struct type *t, const char *name, FILE *fp);
84
 void type_dump     (struct type *t, const char *name, FILE *fp);
85
 void type_dump_decl(struct type *t, FILE *fp);
85
 void type_dump_decl(struct type *t, FILE *fp);
86
+void type_dump_decls(FILE *fp);
86
 
87
 
87
 ssize_t type_alignof(struct type *t);
88
 ssize_t type_alignof(struct type *t);
88
 ssize_t type_offsetof(struct type *t, const char *field);
89
 ssize_t type_offsetof(struct type *t, const char *field);