-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabsyn.c
107 lines (89 loc) · 2.67 KB
/
absyn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "asdl.h"
#define MAX_KEY 32
#alloc absyn_heap, absyn_alloc, absyn_realloc, absyn_dump
#hashfunc absyn_hash
Field *fields;
Constructor *constructors;
Rule *rules;
void init_absyn(void) {
constructors = NULL;
fields = NULL;
rules = NULL;
}
void finalize_absyn(void) { translate_rule_chain(rules); }
void dump_absyn(void) { absyn_dump(); }
TypeId *create_typeid(TypeIdKind kind, char *value) {
TypeId *id = absyn_alloc(sizeof(TypeId));
id->kind = kind;
id->value = value;
}
Field *add_field(TypeId *type_id, int modifier, char *id) {
Field *field = (Field *)absyn_alloc(sizeof(Field));
field->type_id = type_id;
field->kind = modifier;
field->id = id;
field->cache[0] = field->cache[1] = NULL;
field->next = fields;
fields = field;
return field;
}
Constructor *add_constructor(char *id, Field *fields, bool is_enum) {
Constructor *constructor = (Constructor *)absyn_alloc(sizeof(Constructor));
constructor->id = id;
constructor->fields = fields;
constructor->is_enum = is_enum;
constructor->next = constructors;
constructors = constructor;
return constructor;
}
Rule *add_sum_type(Constructor *constructors, Field *attributes) {
Rule *rule = (Rule *)absyn_alloc(sizeof(Rule));
rule->type = (Type *)absyn_alloc(sizeof(Type));
rule->type->sum = (Sum *)absyn_alloc(sizeof(Sum));
rule->type->sum->constructors = constructors;
rule->type->sum->attributes = attributes;
rule->type->kind = TYPE_SUM;
rule->next = rules;
rules = rule;
return rule;
}
Rule *add_product_type(Field *fields) {
Rule *rule = (Rule *)absyn_alloc(sizeof(Rule));
rule->type = (Type *)absyn_alloc(sizeof(Type));
rule->type->product = (Product *)absyn_alloc(sizeof(Product));
rule->type->product->fields = fields;
rule->type->kind = TYPE_PRODUCT;
rule->next = rules;
rules = rule;
return rule;
}
char *gc_strndup(const char *str, size_t n) {
char *dup = (char *)absyn_alloc(n);
return memmove(dup, str, n);
}
Symtable *stab = NULL;
Symtable *symtable_init(void) { stab = NULL; }
void symtable_insert(const char *key, const void *value) {
Symtable *node = (Symtable *)absyn_alloc(sizeof(Symtable));
node->key = gc_strndup(key, strlen(key));
node->value = (void *)value;
node->next = stab;
stab = node;
}
void *symtable_retrieve(const char *key) {
for (Symtable *st = stab; st != NULL; st = st->next)
if (!strncmp(key, st->key, MAX_KEY))
return st->value;
return NULL;
}
bool symtable_exists(const char *key) {
for (Symtable *st = stab; st != NULL; st = st->next)
if (!strncmp(key, st->key, MAX_KEY))
return true;
return false;
}