-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-analyzer.c
218 lines (173 loc) · 5.56 KB
/
main-analyzer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include "fatarena.h"
#include "lib/map.h"
#include "token.h"
#include "lex.h"
#include "parser.h"
#include "analyzer.h"
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define TODO(message) \
do { \
fprintf(stderr, "TODO(%s): %s\n", TOSTRING(__LINE__), message); \
} while (0)
#define ERR(...) fprintf(stderr, "ERR(%d,%d): ", __LINE__, line); fprintf(stderr, __VA_ARGS__), fprintf(stderr, "\n")
const Str langtypes[NLANGTYPE] = {
{(uint8_t*)"bool", 4},
{(uint8_t*)"u8", 2},
{(uint8_t*)"u16", 3},
{(uint8_t*)"u32", 3},
{(uint8_t*)"u64", 3},
{(uint8_t*)"u128", 4},
{(uint8_t*)"i8", 2},
{(uint8_t*)"i16", 3},
{(uint8_t*)"i32", 3},
{(uint8_t*)"i64", 3},
{(uint8_t*)"i128", 4},
{(uint8_t*)"f32", 3},
{(uint8_t*)"f64", 3},
};
int ident2langtype[NLANGTYPE * 4] = {-1};
int langtype2ident[NLANGTYPE] = {-1};
int typeoffset = -1;
int typeend = -1;
FatArena fttok = {0};
FatArena ftident = {0};
FatArena ftlit = {0};
FatArena ftimmed = {0};
FatArena fttmp = {0};
FatArena ftast = {0};
FatArena ftsym = {0};
Symbols funsym = {0};
Symbols identsym = {0};
Symbols typesym = {0};
Symbols signatures = {0};
Symbols genstructsym = {0};
Symbols modsign = {0};
Symbols modskul = {0};
Symbols modimpl = {0};
Symbols moddef = {0};
Symbols modsym[NMODSYM] = {0};
StmtArray pubsym = {0};
StmtArray imports = {0};
SymInfo *syminfo = NULL;
int nsym = 0;
int fstlangtype = -1;
int lstlangtype = -1;
int line = 0;
int
main(int argc, char *argv[])
{
(void) argc;
(void) argv;
int pagesz = getpgsz();
POK(ftnew(&ftident, 10 * pagesz) != 0, "fail to create a FatArena");
ftalloc(&ftident, NKEYWORDS + 1);// burn significant int
POK(ftnew(&ftimmed, 10 * pagesz) != 0, "fail to create a FatArena");
ftalloc(&ftimmed, NKEYWORDS + 1);// burn significant int
POK(ftnew(&ftlit, 10 * pagesz) != 0, "fail to create a FatArena");
ftalloc(&ftlit, NKEYWORDS + 1);// burn significant int
POK(ftnew(&fttok, 10 * pagesz) != 0, "fail to create a FatArena");
int ntok = 0;
POK(ftnew(&fttmp, 10 * pagesz) != 0, "fail to create a FatArena");
POK(ftnew(&ftast, 10 * pagesz) != 0, "fail to create a FatArena");
POK(ftnew(&ftsym, 100 * pagesz) != 0, "fail to create a FatArena");
identsym.array = ftalloc(&ftsym, sizeof(Symbol) * pagesz);
funsym.array = ftalloc(&ftsym, sizeof(Symbol) * pagesz);
typesym.array = ftalloc(&ftsym, sizeof(Symbol) * pagesz);
signatures.array = ftalloc(&ftsym, sizeof(Symbol) * pagesz);
genstructsym.array = ftalloc(&ftsym, sizeof(Symbol) * pagesz);
for (int i = 0; i < NMODSYM; i++) {
modsym[i].array = ftalloc(&ftsym, sizeof(Symbol) * pagesz);
}
pubsym.stmts = ftalloc(&ftsym, sizeof(intptr) * pagesz);
imports.stmts = ftalloc(&ftsym, sizeof(intptr) * pagesz);
// -------------------- Alloc lang types
int type = -1;
for (unsigned long i = 0; i < NLANGTYPE; i++) {
if (typeoffset == -1) {
typeoffset = stri_insert(langtypes[i]);
type = typeoffset;
} else if (i == NLANGTYPE - 1) {
typeend = stri_insert(langtypes[i]);
type = typeend;
} else {
type = stri_insert(langtypes[i]);
}
// save conversion table.
langtype2ident[i] = type;
ident2langtype[type - typeoffset] = i;
}
// -------------------- Lexing
ETok *tlst = (ETok*) ftptr(&fttok, 0);
Tok t;
do {
t = getnext();
ftalloc(&fttok, sizeof(Tok));
if (t.type != SPC && t.type != NEWLINE) {
tlst[ntok++] = t.type;
}
} while (t.type != EOI);
// -------------------- Parsing
int res = -1;
res = parse_tokens(tlst, &signatures, &identsym, &typesym, modsym, &pubsym, &imports);
if (res < 0) {
ERR("Error when parsing tokens.");
return 1;
}
// -------------------- Analyzing
intptr info = ftalloc(&ftsym, sizeof(SymInfo) * pagesz);
syminfo = (SymInfo*) ftptr(&ftsym, info);
nsym = 0;
assert(analyseimport(&imports));
printstmtarray(stdout, &imports);
Symbol *sym = (Symbol*) ftptr(&ftsym, typesym.array);
for (int i = 0; i < typesym.nsym; i++) {
SStruct* str = (SStruct*) ftptr(&ftast, sym[i].stmt);
assert(analyzetype(str));
}
Symbol *symd = (Symbol*) ftptr(&ftsym, identsym.array);
for (int i = 0; i < identsym.nsym; i++) {
SDecl *decl = (SDecl*) ftptr(&ftast, symd[i].stmt);
assert(analyzeglobalcst(decl, nsym));
nsym++;
}
printsymbolsinfo(stdout, nsym);
Symbol *symsign = (Symbol*) ftptr(&ftsym, modsym[MODSIGN].array);
for (int i = 0; i < modsym[MODSIGN].nsym; i++) {
SModSign *s = (SModSign*) ftptr(&ftast, symsign[i].stmt);
analyzemodsign(s);
}
Symbol *symimpl = (Symbol*) ftptr(&ftsym, modsym[MODIMPL].array);
for (int i = 0; i < modsym[MODIMPL].nsym; i++) {
SModImpl *s = (SModImpl*) ftptr(&ftast, symimpl[i].stmt);
analyzemodimpl(s);
}
Symbol *symskel = (Symbol*) ftptr(&ftsym, modsym[MODSKEL].array);
for (int i = 0; i < modsym[MODSKEL].nsym; i++) {
SModSkel *s = (SModSkel*) ftptr(&ftast, symskel[i].stmt);
analyzemodskel(s);
}
Symbol *symdef = (Symbol*) ftptr(&ftsym, modsym[MODDEF].array);
for (int i = 0; i < modsym[MODDEF].nsym; i++) {
SModDef *s = (SModDef*) ftptr(&ftast, symdef[i].stmt);
analyzemoddef(s);
}
Symbol *symf = (Symbol*) ftptr(&ftsym, signatures.array);
for (int i = 0; i < signatures.nsym; i++) {
intptr stmt = symf[i].stmt;
SFun* fun = (SFun*) ftptr(&ftast, stmt);
assert(analyzefun(NULL, fun, stmt, nsym));
}
assert(analyzepub(&pubsym) == 1);
printsymbols(stdout, &typesym);
printsymbols(stdout, &identsym);
printsymbols(stdout, &funsym);
for (int i = 0; i < NMODSYM; i++) {
printsymbols(stdout, modsym + i);
}
}