Repository navigation
Expand file tree
/
Copy pathCompiler.h
More file actions
483 lines (407 loc) · 10.5 KB
/
Copy pathCompiler.h
File metadata and controls
483 lines (407 loc) · 10.5 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#pragma once
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include "Token.h"
#include "JetInstructions.h"
#include "JetExceptions.h"
namespace Jet
{
//add custom operators
//add includes/modules
//parallelism maybe?
//add const
struct IntermediateInstruction
{
InstructionType type;
char* string;
union
{
int first;
char* string2;
};
union
{
double second;
struct
{
unsigned char a,b,c,d;
};
};
IntermediateInstruction(InstructionType type, const char* string, int num = 0)
{
if (string)
{
char* c = new char[strlen(string)+1];
strcpy(c, string);
this->string = c;
}
else
this->string = 0;
this->second = 0;
this->type = type;
this->first = num;
}
IntermediateInstruction(InstructionType type, std::string string, double num = 0)
{
char* c = new char[string.length()+1];
strcpy(c, string.c_str());
this->string = c;
this->type = type;
this->first = 0;
this->second = num;
}
IntermediateInstruction(InstructionType type, int num = 0, double num2 = 0)
{
this->type = type;
this->first = num;
this->second = num2;
this->string = 0;
}
};
class BlockExpression;
template <class T, class T2, class T3>
struct triple
{
T first;
T2 second;
T3 third;
triple(T t, T2 t2, T3 t3) : first(t), second(t2), third(t3)
{
};
};
class CompilerContext
{
friend class FunctionExpression;
friend class CallExpression;
std::map<std::string, CompilerContext*> functions;
struct LoopInfo
{
std::string Break;
std::string Continue;
int locals;//local index at which loop starts
};
std::vector<LoopInfo> loops;
struct LocalVariable
{
int local;
std::string name;
};
struct Scope
{
Scope* previous;
Scope* next;
int level;
std::vector<LocalVariable> localvars;
};
Scope* scope;//linked list starting at current scope
unsigned int localindex;//next open local index
bool vararg; bool isgenerator;
unsigned int closures;//number of closures we have
unsigned int arguments;//number of arguments we have
CompilerContext* parent;//parent scoping function
std::vector<IntermediateInstruction> out;//list of instructions generated
public:
CompilerContext(void);
~CompilerContext(void);
void PrintAssembly();
//used when generating functions
CompilerContext* AddFunction(std::string name, unsigned int args, bool vararg = false);
void FinalizeFunction(CompilerContext* c);
std::vector<IntermediateInstruction> Compile(BlockExpression* expr, const char* filename);
private:
void Compile()
{
//append functions to end here
for (auto fun: this->functions)
{
fun.second->Compile();
//need to set var with the function name and location
this->FunctionLabel(fun.first, fun.second->arguments, fun.second->localindex, fun.second->closures, fun.second->vararg, fun.second->isgenerator);
for (auto ins: fun.second->out)
this->out.push_back(ins);
//add code of functions recursively
}
}
void FunctionLabel(std::string name, int args, int locals, int upvals, bool vararg = false, bool isgenerator = false)
{
IntermediateInstruction ins = IntermediateInstruction(InstructionType::Function, name, args);
ins.a = args;
ins.b = locals;
ins.c = upvals;
ins.d = vararg + isgenerator*2;
out.push_back(ins);
}
public:
void PushScope()
{
Scope* s = new Scope;
this->scope->next = s;
s->level = this->scope->level + 1;
s->previous = this->scope;
s->next = 0;
this->scope = s;
}
void PopScope()
{
if (this->scope && this->scope->previous)
this->scope = this->scope->previous;
if (this->scope)
{
delete this->scope->next;
this->scope->next = 0;
}
}
void PushLoop(const std::string Break, const std::string Continue)
{
LoopInfo i;
i.Break = Break;
i.Continue = Continue;
i.locals = this->localindex;
loops.push_back(i);
}
void PopLoop()
{
//close ALL variables
//do a close if necessary
//whoops, need to have this in the blocks, not here
/*bool found = false;
for (auto& ii : this->scope->localvars)
{
if (ii.capture >= 0)
{
if (ii.uploaded == false)
{
out.push_back(IntermediateInstruction(InstructionType::CInit,ii.local, ii.capture));
ii.uploaded = true;
}
found = true;
}
}*/
//close all closures in the loop
//if (found)
out.push_back(IntermediateInstruction(InstructionType::Close, loops.back().locals));
loops.pop_back();
}
void Break()
{
if (this->loops.size() == 0)
throw CompilerException(this->filename, this->lastline, "Cannot use break outside of a loop!");
this->Jump(loops.back().Break.c_str());
}
void Continue()
{
if (this->loops.size() == 0)
throw CompilerException(this->filename, this->lastline, "Cannot use continue outside of a loop!");
this->Jump(loops.back().Continue.c_str());
}
void ForEach(const std::string& dest, const std::string& start, std::string& end)
{
/*int lpos = 0;
for (unsigned int i = 0; i < this->scope->localvars.size(); i++)
{
if (this->scope->localvars[i].name == dest)
{
lpos = this->scope->localvars[i].local;
break;
}
}
IntermediateInstruction inst(InstructionType::ForEach);
inst.second = lpos;
//copy the strings
char* s = new char[start.length()+1];
start.copy(s, start.length());
s[start.length()] = 0;
char* e = new char[end.length()+1];
end.copy(e, end.length());
e[end.length()] = 0;
inst.string = s;
inst.string2 = e;
this->out.push_back(inst);*/
}
bool RegisterLocal(const std::string name);//returns success
void BinaryOperation(TokenType operation);
void UnaryOperation(TokenType operation);
//stack operations
void Pop()
{
out.push_back(IntermediateInstruction(InstructionType::Pop));
}
void Duplicate()
{
out.push_back(IntermediateInstruction(InstructionType::Dup));
}
//load operations
void Null()
{
out.push_back(IntermediateInstruction(InstructionType::LdNull));
}
void Number(double value)
{
out.push_back(IntermediateInstruction(InstructionType::LdNum, 0, value));
}
void String(std::string string)
{
out.push_back(IntermediateInstruction(InstructionType::LdStr, string.c_str()));
}
//jumps
void JumpFalse(const char* pos)
{
out.push_back(IntermediateInstruction(InstructionType::JumpFalse, pos));
}
void JumpTrue(const char* pos)
{
out.push_back(IntermediateInstruction(InstructionType::JumpTrue, pos));
}
void JumpFalsePeek(const char* pos)
{
out.push_back(IntermediateInstruction(InstructionType::JumpFalsePeek, pos));
}
void JumpTruePeek(const char* pos)
{
out.push_back(IntermediateInstruction(InstructionType::JumpTruePeek, pos));
}
void Jump(const char* pos)
{
out.push_back(IntermediateInstruction(InstructionType::Jump, pos));
}
void Label(const std::string& name)
{
out.push_back(IntermediateInstruction(InstructionType::Label, name));
}
struct Capture
{
int localindex;
int level;
int captureindex;
bool uploaded;
Capture() {}
Capture(int l, int li, int ci) : localindex(li), level(l), captureindex(ci) {uploaded = false;}
};
std::map<std::string, Capture> captures;
void Store(const std::string variable);
void StoreLocal(const std::string variable)
{
//look up if I am local or global
this->Store(variable);
}
//this loads locals and globals atm
void Load(const std::string variable);
bool IsLocal(const std::string variable)
{
Scope* ptr = this->scope;
while (ptr)
{
//look for var in locals
for (unsigned int i = 0; i < ptr->localvars.size(); i++)
{
if (ptr->localvars[i].name == variable)
{
//printf("We found loading of a local var: %s at level %d, index %d\n", variable.c_str(), ptr->level, ptr->localvars[i].first);
//this->output += ".local " + variable + " " + ::std::to_string(i) + ";\n";
return true;//we found it
}
}
if (ptr)
ptr = ptr->previous;
}
auto cur = this->parent;
while(cur)
{
ptr = cur->scope;
while (ptr)
{
//look for var in locals
for (unsigned int i = 0; i < ptr->localvars.size(); i++)
{
if (ptr->localvars[i].name == variable)
{
//printf("We found loading of a captured var: %s at level %d, index %d\n", variable.c_str(), level, ptr->localvars[i].first);
//this->output += ".local " + variable + " " + ::std::to_string(i) + ";\n";
return true;
}
}
if (ptr)
ptr = ptr->previous;
}
cur = cur->parent;
}
return false;
}
void LoadFunction(const std::string name)
{
out.push_back(IntermediateInstruction(InstructionType::LoadFunction, name));
}
void Call(const std::string function, unsigned int args)
{
out.push_back(IntermediateInstruction(InstructionType::Call, function, args));
}
void ECall(unsigned int args)
{
out.push_back(IntermediateInstruction(InstructionType::ECall, args));
}
void LoadIndex(const char* index = 0)
{
out.push_back(IntermediateInstruction(InstructionType::LoadAt, index));
}
void StoreIndex(const char* index = 0)
{
out.push_back(IntermediateInstruction(InstructionType::StoreAt, index));
}
void NewArray(unsigned int number)
{
out.push_back(IntermediateInstruction(InstructionType::NewArray, number));
}
void NewObject(unsigned int number)
{
out.push_back(IntermediateInstruction(InstructionType::NewObject, number));
}
void Return()
{
//if (this->closures > 0)//close all open closures
out.push_back(IntermediateInstruction(InstructionType::Close));
out.push_back(IntermediateInstruction(InstructionType::Return));
}
void Yield()
{
out.push_back(IntermediateInstruction(InstructionType::Yield));
this->isgenerator = true;
}
void Resume()
{
out.push_back(IntermediateInstruction(InstructionType::Resume));
}
//debug info
std::string filename;
void SetFilename(const std::string filename)
{
this->filename = filename;
}
unsigned int lastline;
void Line(unsigned int line)
{
//need to avoid duplicates, because thats silly
if (lastline != line)
{
lastline = line;
out.push_back(IntermediateInstruction(InstructionType::DebugLine, filename, line));
}
}
private:
int uuid;
public:
std::string GetUUID()//use for autogenerated labels
{
return std::to_string(uuid++);
}
};
};