-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.cpp
338 lines (285 loc) · 12.4 KB
/
Compiler.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "Compiler.h"
Compiler::Compiler() {
parseRules[Token::Type::LEFT_PAREN] = {[this] { grouping(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::RIGHT_PAREN] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::LEFT_BRACE] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::RIGHT_BRACE] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::COMMA] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::DOT] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::MINUS] = {[this] { unary(); }, [this] { binary(); }, Precedence::TERM};
parseRules[Token::Type::PLUS] = {nullptr, [this] { binary(); }, Precedence::TERM};
parseRules[Token::Type::SEMICOLON] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::SLASH] = {nullptr, [this] { binary(); }, Precedence::FACTOR};
parseRules[Token::Type::STAR] = {nullptr, [this] { binary(); }, Precedence::FACTOR};
parseRules[Token::Type::BANG] = {[this] { unary(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::BANG_EQUAL] = {nullptr, [this] { binary(); }, Precedence::EQUALITY};
parseRules[Token::Type::EQUAL] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::EQUAL_EQUAL] = {nullptr, [this] { binary(); }, Precedence::EQUALITY};
parseRules[Token::Type::GREATER] = {nullptr, [this] { binary(); }, Precedence::COMPARISON};
parseRules[Token::Type::GREATER_EQUAL] = {nullptr, [this] { binary(); }, Precedence::COMPARISON};
parseRules[Token::Type::LESS] = {nullptr, [this] { binary(); }, Precedence::COMPARISON};
parseRules[Token::Type::LESS_EQUAL] = {nullptr, [this] { binary(); }, Precedence::COMPARISON};
parseRules[Token::Type::IDENTIFIER] = {[this] { variable(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::STRING] = {[this] { string(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::NUMBER] = {[this] { number(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::MATRIX] = {[this] { matrix(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::AND] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::CLASS] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::ELSE] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::FALSE] = {[this] { literal(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::FOR] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::FUN] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::IF] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::NIL] = {[this] { literal(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::OR] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::PRINT] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::RETURN] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::SUPER] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::THIS] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::TRUE] = {[this] { literal(); }, nullptr, Precedence::NONE};
parseRules[Token::Type::VAR] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::WHILE] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::ERROR] = {nullptr, nullptr, Precedence::NONE};
parseRules[Token::Type::END_OF_FILE] = {nullptr, nullptr, Precedence::NONE};
}
bool Compiler::compile(const std::string &titanCode, Batch& batch) {
titanSourceCode = titanCode;
currentBatch = &batch;
scanner.init(titanCode);
advance();
while (!matchType(Token::Type::END_OF_FILE)) {
declaration();
}
emitOp(Op::Code::Return);
#ifdef DEBUG_PRINT_CODE
if (!parser.hadError) {
Debug::disassembleBatch(batch);
}
#endif //DEBUG_PRINT_CODE
return !parser.hadError;
}
Compiler::ParseRule *Compiler::getRule(Token::Type type) {
return &parseRules[type];
}
void Compiler::advance() {
parser.previous = parser.current;
for (;;) {
parser.current = scanner.scanToken();
if (parser.current.type != Token::Type::ERROR) break;
error(parser.current, "Expect end of expression.");
}
}
void Compiler::error(Token &token, const std::string &message) {
if (parser.panicMode) return;
parser.panicMode = true;
std::cerr << "[Line " << token.line << "] Error";
switch (token.type) {
case Token::Type::END_OF_FILE: std::cerr << " at end"; break;
case Token::Type::ERROR: break;
default: std::cerr << " at '" << titanSourceCode.substr(token.start, token.length) << "'"; break;
}
std::cerr << ": " << message << "\n";
parser.hadError = true;
}
void Compiler::consume(Token::Type tokenType, const std::string &message) {
if (parser.current.type == tokenType) {
advance();
return;
}
error(parser.current, message);
}
void Compiler::emitOp(Op::Code op) {
currentBatch->addOp(op, parser.previous.line);
}
void Compiler::emitOps(Op::Code a, Op::Code b) {
emitOp(a);
emitOp(b);
}
void Compiler::expression() {
parsePrecedence(Precedence::ASSIGNMENT);
}
void Compiler::number() {
double value = strtod(&titanSourceCode[parser.previous.start], nullptr);
emitConstant(Value::fromNumber(value));
}
void Compiler::grouping() {
expression();
consume(Token::Type::RIGHT_PAREN, "Expect ')' after expression.");
}
void Compiler::unary() {
Token::Type operatorType = parser.previous.type;
parsePrecedence(Precedence::UNARY);
switch (operatorType) {
case Token::Type::BANG: emitOp(Op::Code::Not); break;
case Token::Type::MINUS: emitOp(Op::Code::Negate); break;
default: return;
}
}
void Compiler::binary() {
Token::Type operatorType = parser.previous.type;
ParseRule* rule = getRule(operatorType);
parsePrecedence((Precedence)(rule->precedence + 1));
switch (operatorType) {
case Token::BANG_EQUAL: emitOp(Op::Code::NotEqual); break;
case Token::EQUAL_EQUAL: emitOp(Op::Code::Equal); break;
case Token::GREATER: emitOp(Op::Code::Greater); break;
case Token::GREATER_EQUAL: emitOp(Op::Code::GreaterEqual); break;
case Token::LESS: emitOp(Op::Code::Less); break;
case Token::LESS_EQUAL: emitOp(Op::Code::LessEqual); break;
case Token::PLUS: emitOp(Op::Code::Add); break;
case Token::MINUS: emitOp(Op::Code::Subtract); break;
case Token::STAR: emitOp(Op::Code::Multiply); break;
case Token::SLASH: emitOp(Op::Code::Divide); break;
default: return; // Unreachable.
}
}
void Compiler::literal() {
switch (parser.previous.type) {
case Token::FALSE: emitOp(Op::Code::False); break;
case Token::NIL: emitOp(Op::Code::Null); break;
case Token::TRUE: emitOp(Op::Code::True); break;
default: break;
}
}
void Compiler::string() {
emitConstant(Value::fromString(titanSourceCode.substr(parser.previous.start + 1, parser.previous.length - 2)));
}
void Compiler::matrix() {
// std::cout << "Matrix: '" << titanSourceCode.substr(parser.previous.start + 1, parser.previous.length - 1) << "'\n";
emitConstant(Value::fromMatrixD(MatrixD(titanSourceCode.substr(parser.previous.start + 1, parser.previous.length - 2))));
}
void Compiler::declaration() {
if (matchType(Token::Type::VAR)) {
variableDeclaration();
}
else {
statement();
}
if (parser.panicMode) {
synchronise();
}
}
void Compiler::statement() {
if (matchType(Token::Type::PRINT)) {
expression();
consume(Token::Type::SEMICOLON, "Expect ';' after value.");
emitOp(Op::Print);
}
else {
expressionStatement();
}
}
void Compiler::expressionStatement() {
expression();
consume(Token::Type::SEMICOLON, "Expect ';' after expression.");
emitOp(Op::Code::Pop);
}
void Compiler::variableDeclaration() {
size_t globalNameIndex = parseVariable("Expect variable name.");
if (matchType(Token::Type::EQUAL)) {
expression();
}
else {
emitOp(Op::Code::Null);
}
consume(Token::Type::SEMICOLON, "Expect ';' after variable declaration.");
// emitGlobalVariable(Value::fromString(titanSourceCode.substr(parser.previous.start, parser.previous.length - 1)));
defineVariable(globalNameIndex);
}
void Compiler::variable() {
namedVariable(parser.previous);
}
void Compiler::namedVariable(const Token &token) {
size_t arg = identifierConstant(token);
//Define Global
if (arg == (size_t)-1) {
error(parser.previous, "Error defining global variable: Out of 32-bit address space.");
}
else if (arg >= 1 << (sizeof(Op::Code) * 8)) {
emitOp(Op::Code::GetGlobal32);
auto indexAsOpCodes = Memory::toOpCodes(arg);
emitOps(indexAsOpCodes[0], indexAsOpCodes[1]);
}
else {
emitOp(Op::Code::GetGlobal);
emitOp((Op::Code)arg);
}
//emitGlobalVariable(Value::fromString(titanSourceCode.substr(token.start, token.length)), false);
}
void Compiler::synchronise() {
parser.panicMode = false;
while (parser.current.type != Token::Type::END_OF_FILE) {
if (parser.previous.type == Token::Type::SEMICOLON) return;
switch (parser.current.type) {
case Token::Type::CLASS:
case Token::Type::FUN:
case Token::Type::VAR:
case Token::Type::FOR:
case Token::Type::IF:
case Token::Type::WHILE:
case Token::Type::PRINT:
case Token::Type::RETURN:
return;
default: ; // Do nothing.
}
advance();
}
}
void Compiler::parsePrecedence(Compiler::Precedence precedence) {
advance();
ParseFunction prefixRule = getRule(parser.previous.type)->prefix;
if (prefixRule == nullptr) {
error(parser.previous, "Expect expression.");
return;
}
prefixRule();
while (precedence <= getRule(parser.current.type)->precedence) {
advance();
ParseFunction infixRule = getRule(parser.previous.type)->infix;
infixRule();
}
}
size_t Compiler::parseVariable(const std::string &errorMessage) {
consume(Token::Type::IDENTIFIER, errorMessage);
return identifierConstant(parser.previous);
}
size_t Compiler::identifierConstant(const Token &token) {
return emitConstant(Value::fromString(titanSourceCode.substr(token.start, token.length)));
}
void Compiler::defineVariable(size_t global) {
if (global == (size_t)-1) {
error(parser.previous, "Error defining global variable: Out of 32-bit address space.");
}
else if (global >= 1 << (sizeof(Op::Code) * 8)) {
emitOp(Op::Code::DefineGlobal32);
auto indexAsOpCodes = Memory::toOpCodes(global);
emitOps(indexAsOpCodes[0], indexAsOpCodes[1]);
}
else {
emitOp(Op::Code::DefineGlobal);
emitOp((Op::Code)global);
}
}
size_t Compiler::emitConstant(const Value& value) {
//Gets the current index in the constant pool.
size_t constantIndex = currentBatch->addConstant(value);
//If the current index won't fit in the address space of a single Op::Code, use a 32-bit address.
if (constantIndex == (size_t)-1) {
error(parser.previous, "Error adding new constant: Out of 32-bit address space.");
}
else if (constantIndex >= 1 << (sizeof(Op::Code) * 8)) {
emitOp(Op::Code::Constant32);
auto indexAsOpCodes = Memory::toOpCodes(constantIndex);
emitOps(indexAsOpCodes[0], indexAsOpCodes[1]);
}
else {
emitOp(Op::Code::Constant);
emitOp((Op::Code)constantIndex);
}
return constantIndex;
}
bool Compiler::matchType(Token::Type type) {
if (parser.current.type != type) return false;
advance();
return true;
}