-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsymbol.c
135 lines (121 loc) · 3.99 KB
/
symbol.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "compilium.h"
static void PushSymbol(struct SymbolEntry **prev, struct SymbolEntry *sym) {
sym->prev = *prev;
*prev = sym;
}
static struct SymbolEntry *AllocSymbolEntry(enum SymbolType type,
const char *key,
struct Node *value) {
struct SymbolEntry *e = calloc(1, sizeof(struct SymbolEntry));
e->type = type;
e->key = key;
e->value = value;
return e;
}
int GetLastLocalVarOffset(struct SymbolEntry *e) {
for (; e; e = e->prev) {
if (e->type != kSymbolLocalVar) continue;
assert(e->value && e->value->type == kASTLocalVar);
return e->value->byte_offset;
}
return 0;
}
struct Node *AddLocalVar(struct SymbolEntry **ctx, const char *key,
struct Node *var_type) {
assert(ctx);
int ofs = GetLastLocalVarOffset(*ctx);
ofs += GetSizeOfType(var_type);
int align = GetSizeOfType(var_type);
ofs = (ofs + align - 1) / align * align;
struct Node *local_var = CreateASTLocalVar(ofs, var_type);
struct SymbolEntry *e = AllocSymbolEntry(kSymbolLocalVar, key, local_var);
PushSymbol(ctx, e);
return local_var;
}
void AddGlobalVar(struct SymbolEntry **ctx, const char *key,
struct Node *var_type) {
fprintf(stderr, "Gvar: %s: ", key);
PrintASTNode(var_type);
fprintf(stderr, "\n");
assert(ctx);
struct SymbolEntry *e = AllocSymbolEntry(kSymbolGlobalVar, key, var_type);
PushSymbol(ctx, e);
}
void AddExternVar(struct SymbolEntry **ctx, const char *key,
struct Node *var_type) {
fprintf(stderr, "Evar: %s: ", key);
PrintASTNode(var_type);
fprintf(stderr, "\n");
assert(ctx);
struct SymbolEntry *e = AllocSymbolEntry(kSymbolExternVar, key, var_type);
PushSymbol(ctx, e);
}
struct Node *FindExternVar(struct SymbolEntry *e, struct Node *key_token) {
// returns ASTNode which represents Type
for (; e; e = e->prev) {
if (e->type != kSymbolExternVar) continue;
if (!IsEqualTokenWithCStr(key_token, e->key)) continue;
return e->value;
}
return NULL;
}
struct Node *FindGlobalVar(struct SymbolEntry *e, struct Node *key_token) {
// returns ASTNode which represents Type
for (; e; e = e->prev) {
if (e->type != kSymbolGlobalVar) continue;
if (!IsEqualTokenWithCStr(key_token, e->key)) continue;
return e->value;
}
return NULL;
}
struct Node *FindLocalVar(struct SymbolEntry *e, struct Node *key_token) {
for (; e; e = e->prev) {
if (e->type != kSymbolLocalVar) continue;
if (!IsEqualTokenWithCStr(key_token, e->key)) continue;
return e->value;
}
return NULL;
}
void AddFuncDef(struct SymbolEntry **ctx, const char *key,
struct Node *func_def) {
assert(ctx);
struct SymbolEntry *e = AllocSymbolEntry(kSymbolFuncDef, key, func_def);
PushSymbol(ctx, e);
}
struct Node *FindFuncDef(struct SymbolEntry *e, struct Node *key_token) {
for (; e; e = e->prev) {
if (e->type != kSymbolFuncDef) continue;
if (!IsEqualTokenWithCStr(key_token, e->key)) continue;
return e->value;
}
return NULL;
}
void AddFuncDeclType(struct SymbolEntry **ctx, const char *key,
struct Node *func_decl) {
assert(ctx);
struct SymbolEntry *e = AllocSymbolEntry(kSymbolFuncDeclType, key, func_decl);
PushSymbol(ctx, e);
}
struct Node *FindFuncDeclType(struct SymbolEntry *e, struct Node *key_token) {
for (; e; e = e->prev) {
if (e->type != kSymbolFuncDeclType) continue;
if (!IsEqualTokenWithCStr(key_token, e->key)) continue;
return e->value;
}
return NULL;
}
void AddStructType(struct SymbolEntry **ctx, const char *key,
struct Node *type) {
assert(ctx);
struct SymbolEntry *e = AllocSymbolEntry(kSymbolStructType, key, type);
PushSymbol(ctx, e);
PrintASTNode(type);
}
struct Node *FindStructType(struct SymbolEntry *e, struct Node *key_token) {
for (; e; e = e->prev) {
if (e->type != kSymbolStructType) continue;
if (!IsEqualTokenWithCStr(key_token, e->key)) continue;
return e->value;
}
return NULL;
}