-
Notifications
You must be signed in to change notification settings - Fork 2
/
Expressions.cpp
359 lines (293 loc) · 8.97 KB
/
Expressions.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <jetscript/Expressions.h>
#include <jetscript/Compiler.h>
#include <jetscript/Parser.h>
using namespace Jet;
void PrefixExpression::Compile(CompilerContext* context)
{
context->Line(this->_operator.line);
right->Compile(context);
context->UnaryOperation(this->_operator.type);
switch (this->_operator.type)
{
case TokenType::BNot:
case TokenType::Minus:
{
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
break;
}
default://operators that also do a store, like ++ and --
{
auto location = dynamic_cast<IStorableExpression*>(this->right);
if (location)
{
if (dynamic_cast<BlockExpression*>(this->Parent) == 0)
context->Duplicate();
location->CompileStore(context);
}
else if (dynamic_cast<BlockExpression*>(this->Parent) != 0)
context->Pop();
}
}
}
void PostfixExpression::Compile(CompilerContext* context)
{
context->Line(this->_operator.line);
left->Compile(context);
if (dynamic_cast<BlockExpression*>(this->Parent) == 0 && dynamic_cast<IStorableExpression*>(this->left))
context->Duplicate();
context->UnaryOperation(this->_operator.type);
if (dynamic_cast<IStorableExpression*>(this->left))
dynamic_cast<IStorableExpression*>(this->left)->CompileStore(context);
else if (dynamic_cast<BlockExpression*>(this->Parent) != 0)
context->Pop();
}
void IndexExpression::Compile(CompilerContext* context)
{
context->Line(token.line);
left->Compile(context);
//if the index is constant compile to a special instruction carying that constant
if (auto string = dynamic_cast<StringExpression*>(index))
{
context->LoadIndex(string->GetValue().c_str());
}
else
{
index->Compile(context);
context->LoadIndex();
}
if (dynamic_cast<BlockExpression*>(this->Parent) != 0)
context->Pop();
}
void IndexExpression::CompileStore(CompilerContext* context)
{
context->Line(token.line);
left->Compile(context);
//if the index is constant compile to a special instruction carying that constant
if (auto string = dynamic_cast<StringExpression*>(index))
{
context->StoreIndex(string->GetValue().c_str());
}
else
{
index->Compile(context);
context->StoreIndex();
}
}
void ObjectExpression::Compile(CompilerContext* context)
{
int count = 0;
if (this->inits)
{
count = this->inits->size();
//set these up
for (auto ii: *this->inits)
{
context->String(ii.first);
ii.second->Compile(context);
}
}
context->NewObject(count);
//pop off if we dont need the result
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void ArrayExpression::Compile(CompilerContext* context)
{
int count = this->initializers.size();
for (auto i: this->initializers)
i->Compile(context);
context->NewArray(count);
//pop off if we dont need the result
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void StringExpression::Compile(CompilerContext* context)
{
context->String(this->value);
//pop off if we dont need the result
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void NullExpression::Compile(CompilerContext* context)
{
context->Null();
//pop off if we dont need the result
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void NumberExpression::Compile(CompilerContext* context)
{
context->Number(this->value);
//pop off if we dont need the result
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void SwapExpression::Compile(CompilerContext* context)
{
right->Compile(context);
left->Compile(context);
if (auto rstorable = dynamic_cast<IStorableExpression*>(this->right))
rstorable->CompileStore(context);
if (dynamic_cast<BlockExpression*>(this->Parent) == 0)
context->Duplicate();
if (auto lstorable = dynamic_cast<IStorableExpression*>(this->left))
lstorable->CompileStore(context);
}
void AssignExpression::Compile(CompilerContext* context)
{
this->right->Compile(context);
if (dynamic_cast<BlockExpression*>(this->Parent) == 0)
context->Duplicate();//if my parent is not block expression, we need the result, so push it
if (auto storable = dynamic_cast<IStorableExpression*>(this->left))
storable->CompileStore(context);
}
void CallExpression::Compile(CompilerContext* context)
{
context->Line(token.line);
//need to check if left is a local, or a captured value before looking at globals
if (dynamic_cast<NameExpression*>(left) && context->IsLocal(dynamic_cast<NameExpression*>(left)->GetName()) == false)
{
//push args onto stack
for (auto i: *args)
i->Compile(context);
context->Call(dynamic_cast<NameExpression*>(left)->GetName(), args->size());
}
else// if (dynamic_cast<IStorableExpression*>(left) != 0)
{
auto index = dynamic_cast<IndexExpression*>(left);
if (index && index->token.type == TokenType::Colon)//its a "self" call
{
index->left->Compile(context);//push object as the first argument
//push args onto stack
for (auto i: *args)
i->Compile(context);//pushes args
//compile left I guess?
left->Compile(context);//pushes function
//increase number of args
context->ECall(args->size()+1);
}
else
{
//push args onto stack
for (auto i: *args)
i->Compile(context);
//compile left I guess?
left->Compile(context);
context->ECall(args->size());
}
}
//else
//{
//throw ParserException(token.filename, token.line, "Error: Cannot call an expression that is not a name");
//}
//help, how should I handle this for multiple returns
//pop off return value if we dont need it
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();//if my parent is block expression, we dont the result, so pop it
}
void NameExpression::Compile(CompilerContext* context)
{
//add load variable instruction
//todo make me detect if this is a local or not
context->Load(name);
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void OperatorAssignExpression::Compile(CompilerContext* context)
{
context->Line(token.line);
//https://dl.dropboxusercontent.com/u/675786/ShareX/2015-02/08_22-33-22.png fix this
this->left->Compile(context);
this->right->Compile(context);
context->BinaryOperation(token.type);
//insert store here
if (dynamic_cast<BlockExpression*>(this->Parent) == 0)
context->Duplicate();//if my parent is not block expression, we need the result, so push it
if (auto storable = dynamic_cast<IStorableExpression*>(this->left))
storable->CompileStore(context);
}
void OperatorExpression::Compile(CompilerContext* context)
{
context->Line(this->_operator.line);
if (this->_operator.type == TokenType::And)
{
std::string label = "_endand"+context->GetUUID();
this->left->Compile(context);
context->JumpFalsePeek(label.c_str());//jump to endand if false
context->Pop();
this->right->Compile(context);
context->Label(label);//put endand label here
return;
}
if (this->_operator.type == TokenType::Or)
{
std::string label = "_endor"+context->GetUUID();
this->left->Compile(context);
context->JumpTruePeek(label.c_str());//jump to endor if true
context->Pop();
this->right->Compile(context);
context->Label(label);//put endor label here
return;
}
this->left->Compile(context);
this->right->Compile(context);
context->BinaryOperation(this->_operator.type);
//pop off if we dont need the result
if (dynamic_cast<BlockExpression*>(this->Parent))
context->Pop();
}
void FunctionExpression::Compile(CompilerContext* context)
{
context->Line(token.line);
std::string fname;
if (name)
fname = static_cast<NameExpression*>(name)->GetName();
else
fname = "_lambda_id_";
CompilerContext* function = context->AddFunction(fname, this->args->size(), this->varargs);
//ok, kinda hacky
int start = context->out.size();
//ok push locals, in opposite order
for (unsigned int i = 0; i < this->args->size(); i++)
{
auto aname = static_cast<NameExpression*>((*this->args)[i]);
function->RegisterLocal(aname->GetName());
}
if (this->varargs)
function->RegisterLocal(this->varargs->GetName());
block->Compile(function);
//if last instruction was a return, dont insert another one
if (block->statements.size() > 0)
{
if (dynamic_cast<ReturnExpression*>(block->statements.at(block->statements.size()-1)) == 0)
{
function->Null();//return nil
function->Return();
}
}
else
{
function->Null();//return nil
function->Return();
}
context->FinalizeFunction(function);
//only named functions need to be stored here
if (name)
context->Store(static_cast<NameExpression*>(name)->GetName());
//vm will pop off locals when it removes the call stack
}
void LocalExpression::Compile(CompilerContext* context)
{
context->Line((*_names)[0].line);
//add load variable instruction
for (auto ii: *this->_right)
ii->Compile(context);
//make sure to create identifier
for (auto _name: *this->_names)
if (context->RegisterLocal(_name.getText()) == false)
throw CompilerException(context->filename, _name.line, "Duplicate Local Variable '"+_name.text+"'");
//actually store if we have something to store
for (int i = _right->size()-1; i >= 0; i--)
context->StoreLocal((*_names)[i].text);
}