-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.cpp
286 lines (259 loc) · 11 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
#include"object.cpp"
#include"parser.cpp"
#include"symbol.cpp"
struct ByteCode {
Instruction instructions;
vector<unique_ptr<Object>> constants;
};
struct EmittedInstruction {
OpCode opcode;
int ip;
};
struct CompilationScope {
Instruction instructions;
EmittedInstruction last;
EmittedInstruction prevLast;
};
class Compiler {
private:
Instruction instructions;
vector<unique_ptr<Object>> constants;
SymbolTable symbolTable;
public:
vector<unique_ptr<CompilationScope>> scopes;
int scopeIndex;
Compiler() {
symbolTable = SymbolTable();
auto scope = CompilationScope{Instruction{}, EmittedInstruction{}, EmittedInstruction{}};
scopes.push_back(make_unique<CompilationScope>(scope));
scopeIndex = 0;
};
// Compiler(SymbolTable* st, vector<unique_ptr<Object>>&& constants) : symbolTable(*st), constants(move(constants)) {};
CompilationScope* getCurrScope() {
return scopes.at(scopeIndex).get();
}
Instruction getCurrInstructions() {
return scopes.at(scopeIndex).get()->instructions;
}
void setLastInstruction(OpCode opcode, int ip) {
auto scope = getCurrScope();
auto prevLast = scope->last;
auto last = EmittedInstruction{opcode, ip};
scope->prevLast = prevLast;
scope->last = last;
}
int removeIfLastIs(OpCode opcode) {
CompilationScope* scope = getCurrScope();
if (scope == nullptr) return 1; // invalid scope
if (scope->last.opcode == opcode) {
auto prevLast = scope->prevLast;
scope->instructions.pop_back();
scope->last = prevLast;
}
return 0;
}
int replaceIfLastIs(OpCode opcode, OpCode update) {
CompilationScope* scope = getCurrScope();
if (scope == nullptr) return 1; // invalid scope
if (scope->last.opcode == opcode) {
scope->instructions.at(scope->last.ip) = update;
scope->last = EmittedInstruction{update, scope->last.ip};
}
return 0;
}
int addIfLastIsNot(OpCode opcode, OpCode update) {
CompilationScope* scope = getCurrScope();
if (scope == nullptr) return 1; // invalid scope
if (scope->last.opcode != opcode) {
emit(update, vector<int>{});
}
return 0;
}
void changeOperand(int ip, vector<int> operand) {
auto scope = getCurrScope();
auto opcode = OpCode(scope->instructions.at(ip));
auto newInstruction = constructByteCode(opcode, operand);
// replace instruction
for (int i = 0; i < newInstruction.size(); i++) {
scope->instructions.at(ip + i) = newInstruction.at(i);
}
}
template<typename T> int compile(unique_ptr<T> node) {
string type = node.get()->getType();
if (type == ntypes.Identifier) {
Identifier* ident = dynamic_cast<Identifier*>(node.get());
emit(OpGetGlobal, vector<int>{symbolTable.resolve(ident->value).get()->index});
}
else if (type == ntypes.LetStatement) {
LetStatement* stmt = dynamic_cast<LetStatement*>(node.get());
if (compile(move(stmt->value))) return 1; // failed to compile let statement expression
// store to symbol table
emit(OpSetGlobal, vector<int>{symbolTable.define(stmt->identifier.value).get()->index});
}
else if (type == ntypes.FnLiteral) {
FnLiteral* fn = dynamic_cast<FnLiteral*>(node.get());
enterScope();
if (compile(move(fn->body))) return 1; // failed to compile func body
if (replaceIfLastIs(OpPop, OpRetVal)) return 1; // failed to replace pop instruction with return instruction
if (addIfLastIsNot(OpRetVal, OpRet)); // handle empty function
auto instructions = leaveScope();
auto compiledFn = CompiledFunction(instructions);
int constIdx = addConstant(make_unique<CompiledFunction>(compiledFn));
emit(OpConstant, vector<int>{constIdx});
}
else if (type == ntypes.CallExpression) {
CallExpression* exp = dynamic_cast<CallExpression*>(node.get());
if (compile(move(exp->function))) return 1; // failed to compile function of function call
emit(OpCall, vector<int>{});
}
else if (type == ntypes.ReturnStatement) {
ReturnStatement* stmt = dynamic_cast<ReturnStatement*>(node.get());
if (compile(move(stmt->value))) return 1; // failed to compile return statement
emit(OpRetVal, vector<int>{});
}
else if (type == ntypes.ExpressionStatement) {
ExpressionStatement* stmt = dynamic_cast<ExpressionStatement*>(node.get());
if (compile(move(stmt->expression))) return 1;
emit(OpPop, vector<int>{});
}
else if (type == ntypes.PrefixExpression) {
PrefixExpression* exp = dynamic_cast<PrefixExpression*>(node.get());
if (compile(move(exp->right))) return 1; // expression invalid
if (exp->Operator == "-") {
emit(OpMinus, vector<int>{});
} else if (exp->Operator == "!") {
emit(OpSurprise, vector<int>{});
}
}
else if (type == ntypes.InfixExpression) {
InfixExpression* exp = dynamic_cast<InfixExpression*>(node.get());
if (exp->Operator == "<") {
if (compile(move(exp->right))) return 1;
if (compile(move(exp->left))) return 1;
emit(OpGt, vector<int>{});
return 0;
}
if (compile(move(exp->left))) return 1;
if (compile(move(exp->right))) return 1;
if (exp->Operator == "+") {
emit(OpAdd, vector<int>{});
} else if (exp->Operator == "-") {
emit(OpSub, vector<int>{});
} else if (exp->Operator == "*") {
emit(OpMul, vector<int>{});
} else if (exp->Operator == "/") {
emit(OpDiv, vector<int>{});
} else if (exp->Operator == "==") {
emit(OpEq, vector<int>{});
} else if (exp->Operator == "!=") {
emit(OpNeq, vector<int>{});
} else if (exp->Operator == ">") {
emit(OpGt, vector<int>{});
} else {
return 1; // unknown operator
}
}
else if (type == ntypes.IntLiteral) {
IntLiteral* lit = dynamic_cast<IntLiteral*>(node.get());
emit(OpConstant, vector<int>{addConstant(make_unique<Integer>(lit->value))});
}
else if (type == ntypes.BoolLiteral) {
BoolLiteral* lit = dynamic_cast<BoolLiteral*>(node.get());
if (lit->value) emit(OpTrue, vector<int>{});
else emit(OpFalse, vector<int>{});
}
else if (type == ntypes.IfExpression) {
IfExpression* exp = dynamic_cast<IfExpression*>(node.get());
if (compile(move(exp->condition))) return 1; // failed to compile condition of if statement
int posJumpIfFalse = emit(OpJumpIfFalse, vector<int>{-1}); // fix later
if (compile(move(exp->consequence))) return 1; // failed to compile consequence
if (removeIfLastIs(OpPop)) return 1; // do not pop the result of consequence off stack
int posJump = emit(OpJump, vector<int>{-1});
changeOperand(posJumpIfFalse, vector<int>{(int) getCurrScope()->instructions.size()});
if (exp->alternative == nullptr) {
emit(OpNull, vector<int>{});
} else {
if (compile(move(exp->alternative))) return 1; // failed to compile alternative of if statement
if (removeIfLastIs(OpPop)) return 1;
}
changeOperand(posJump, vector<int>{(int) getCurrScope()->instructions.size()});
}
else if (type == ntypes.BlockStatement) {
BlockStatement* stmt = dynamic_cast<BlockStatement*>(node.get());
for (int i = 0; i < stmt->statements.size(); i++) {
if (compile(move(stmt->statements.at(i)))) return 1;
}
}
else if (type == ntypes.StringLiteral) {
StringLiteral* lit = dynamic_cast<StringLiteral*>(node.get());
emit(OpConstant, vector<int>{addConstant(make_unique<String>(lit->value))});
}
else if (type == ntypes.ArrayLiteral) {
ArrayLiteral* arr = dynamic_cast<ArrayLiteral*>(node.get());
for (auto& exp : arr->elements) {
if (compile(move(exp))) return 1; // error compiling element in array
}
emit(OpArray, vector<int>{(int) arr->elements.size()});
}
else if (type == ntypes.HashLiteral) {
HashLiteral* hash = dynamic_cast<HashLiteral*>(node.get());
for (auto& pair : hash->pairs) {
if (compile(move(pair.first))) return 1;
if (compile(move(pair.second))) return 1;
}
emit(OpHash, vector<int>{(int) hash->pairs.size() * 2});
}
else if (type == ntypes.IndexExpression) {
IndexExpression* index = dynamic_cast<IndexExpression*>(node.get());
if (compile(move(index->entity))) return 1; // failed to compile entity
if (compile(move(index->index))) return 1; // failed to compile index
emit(OpIndex, vector<int>{});
}
return 0;
}
int compileProgram(Program* program) {
for (int i = 0; i < program->statements.size(); i++) {
if (compile(move(program->statements.at(i)))) return 1;
}
return 0;
}
ByteCode getByteCode() {
ByteCode bc = {getCurrScope()->instructions, move(constants)};
return bc;
}
int addConstant(unique_ptr<Object> obj) {
constants.push_back(move(obj));
return constants.size() - 1; // return index of obj in the constant list as the unique id
}
int emit(OpCode opcode, vector<int> operands) {
auto instruction = constructByteCode(opcode, operands);
// cout << serialize(instruction) << endl;
int pos = addInstruction(instruction);
auto scope = getCurrScope();
scope->prevLast = scope->last;
scope->last = EmittedInstruction{opcode, pos};
return pos;
}
int addInstruction(Instruction instruction) {
Instruction temp = getCurrInstructions();
int pos = temp.size();
temp.insert(temp.end(), instruction.begin(), instruction.end());
scopes.at(scopeIndex).get()->instructions = temp; // suspect
return pos;
}
void enterScope() {
auto scope = CompilationScope{
Instruction{},
EmittedInstruction{},
EmittedInstruction{}
};
scopes.push_back(make_unique<CompilationScope>(scope));
scopeIndex++;
}
Instruction leaveScope() {
auto instructions = getCurrInstructions();
scopes.pop_back();
scopeIndex--;
return instructions;
}
};