-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cpp
441 lines (394 loc) · 13.2 KB
/
Engine.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
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
//
// Created by pnsv0 on 04.12.2022.
//
#include "Engine.h"
#define NATIVE_TEMPLATED_FUNCTION(func) ( \
{ \
Function *f = new Function(); \
f->is_native = true; \
f->native_templated_func = [](std::vector<Value *> vals, Engine *self) { \
return self->func(vals); \
}; \
f; \
})
void SEQL::Scope::drop_local_variables()
{
// for(auto & element : this->local_variables)
// {
// delete element.second;
// element.second = nullptr;
// }
}
std::string SEQL::Engine::stringifyValue(Value *value)
{
return str(value)->result;
}
void SEQL::Engine::execute_file(const std::string &path)
{
FILE *fp;
fp = fopen(path.c_str(), "r");
popin = fp;
poplex();
this->ast_creator->tokens = __toks;
#ifdef DEBUG_LEXER
for (auto &element : __toks)
{
std::cout << "Token: " << element.value << " With type " << (int)element.type << std::endl;
}
#endif
this->ast_creator->create_ast();
if (this->ast_creator->state == ASTState::BROKEN)
{
delete this->ast_creator;
return;
}
this->functions = this->ast_creator->declared_functions;
#ifdef DEBUG_LOGGING
std::cout << "Finished creating AST tree..." << std::endl;
#endif
auto statement = new Statement();
statement->is_composed = true;
statement->composed_statements = this->ast_creator->as_tree;
this->load_default_functions();
this->scopes.push_back(new Scope());
this->execute_statement(statement);
this->run_function("some1", {new Value(1), new Value(2)});
drop_last_scope();
delete this->ast_creator;
}
void SEQL::Engine::register_channel(CPPChannel *channel)
{
this->channels.push_back(channel);
}
void SEQL::Engine::run_function(std::string fun_name, std::vector<Value *> vals)
{
Function *fun = this->functions[fun_name];
if (fun == nullptr)
{
sprintf(error.message, "There is no function with name %s\n", fun_name);
error.is_critical = true;
raise_error();
}
if (fun->is_native)
{
auto result = fun->native_templated_func(vals, this);
}
else
{
std::vector<Variable *> created_variables;
auto args = fun->function_args->composed_statements;
Scope *scope = new Scope();
for (int i = 0; i < args.size(); i++)
{
auto var_frag = (VariableReferenceFragment *)(args[i]->ast_root);
auto var = new Variable();
var->value = vals[i];
scope->local_variables[var_frag->name] = var;
}
this->scopes.push_back(scope);
this->execute_statement(fun->function_body);
drop_last_scope();
}
for(auto & element : vals)
{
delete element;
}
}
void SEQL::Engine::load_default_functions()
{
this->functions["int"] = NATIVE_TEMPLATED_FUNCTION(to_int);
this->functions["str"] = NATIVE_TEMPLATED_FUNCTION(str);
this->functions["typeof"] = NATIVE_TEMPLATED_FUNCTION(type_of);
this->functions["println"] = NATIVE_TEMPLATED_FUNCTION(println);
this->functions["channel"] = NATIVE_TEMPLATED_FUNCTION(channel_to_cpp);
this->functions["request"] = NATIVE_TEMPLATED_FUNCTION(make_request);
this->functions["print"] = NATIVE_TEMPLATED_FUNCTION(print);
this->functions["format"] = NATIVE_TEMPLATED_FUNCTION(format);
this->functions["obj"] = NATIVE_TEMPLATED_FUNCTION(obj);
}
void SEQL::Engine::execute_statement(Statement *statement)
{
if (statement == nullptr)
return;
for (const auto &element : statement->composed_statements)
{
if (element->type == StatementType::FOR_STATEMENT)
{
make_new_scope();
for (eval(element->on_init); evals_to_true(element->condition); eval(element->each))
{
if (break_requested)
{
break_requested = false;
break;
}
else if (continue_requested)
{
continue_requested = false;
continue;
}
make_new_scope();
execute_statement(element->child_statement);
drop_last_scope();
}
drop_last_scope();
}
else if (return_requested)
{
this->return_requested = false;
continue;
}
// Give control to the for loop?
else if (break_requested || continue_requested)
{
return;
}
else if (element->type == StatementType::ARRAY_STATEMENT)
{
for (const auto &sub_statement : element->child_statement->composed_statements)
{
this->execute_statement(sub_statement);
}
}
else if (element->type == StatementType::IF_STATEMENT)
{
if (evals_to_true(element->condition))
{
make_new_scope();
execute_statement(element->child_statement);
drop_last_scope();
continue;
}
else if (element->continued_statement != nullptr)
{
Statement *if_frag = element->continued_statement;
while (if_frag != nullptr)
{
if (if_frag->type == StatementType::ELSE_STATEMENT)
{
make_new_scope();
execute_statement(if_frag->child_statement);
drop_last_scope();
break;
}
else if (
if_frag->type == StatementType::ELSE_IF_STATEMENT &&
evals_to_true((if_frag->condition)))
{
make_new_scope();
execute_statement(if_frag->child_statement);
drop_last_scope();
break;
}
if_frag = if_frag->continued_statement;
}
}
}
else
{
this->eval(element->ast_root);
}
}
this->return_requested = false;
this->break_requested = false;
this->continue_requested = false;
}
void SEQL::Engine::make_new_scope()
{
this->scopes.push_back(new Scope());
}
void SEQL::Engine::drop_last_scope()
{
auto &last_scope = this->scopes[scopes.size() - 1];
last_scope->drop_local_variables();
this->scopes.pop_back();
std::vector<Value *> roots;
for (size_t i = 0; i < scopes.size(); i++)
{
Scope *scope = scopes[i];
for (auto &element : scope->local_variables)
{
roots.push_back(element.second->value);
}
}
if (stored_value != nullptr)
{
roots.push_back(stored_value);
}
gc->set_roots(roots);
gc->run();
// delete last_scope;
}
SEQL::Value *SEQL::Engine::eval(Fragment *fragment)
{
if (fragment->type == FragmentType::OPERATOR)
{
return handle_operator((OperatorFragment *)(fragment));
}
else if (fragment->type == FragmentType::KEYWORD)
{
return handle_keyword((KeywordFragment *)(fragment));
}
else if (fragment->type == FragmentType::ARRAY)
{
ArrayFragment *link = (ArrayFragment *)(fragment);
Value *array_value = new Value();
array_value->is_mature = false;
array_value->value_type = ValueType::ARRAY;
array_value->array_statement = link->statement;
if (link->statement != nullptr)
{
if (array_value->array_values == nullptr)
{
array_value->array_values = new std::vector<Value *>();
for (const auto &element : link->statement->composed_statements)
{
Value *evaluated = this->eval(element->ast_root);
evaluated->is_mature = false;
array_value->array_values->push_back(evaluated);
}
}
}
else
{
sprintf(error.message, "Failed to resolve array.");
raise_error();
}
for (auto &element : *array_value->array_values)
{
element->is_mature = true;
}
array_value->is_mature = true;
return array_value;
}
else if (fragment->type == FragmentType::PARENTHESES)
{
ParenthesesFragment *p_frag = (ParenthesesFragment *)fragment;
return this->eval(p_frag->inner_frag);
}
else if (fragment->type == FragmentType::ARRAY_ACCESS)
{
ArrayAccessFragment *arr_access = (ArrayAccessFragment *)fragment;
auto l = eval(arr_access->array_frag);
auto r = eval(arr_access->index_expr->ast_root);
Value *result = nullptr;
if (l->array_values != nullptr)
{
auto index = bytes_to_int(r->result);
auto deref = *l->array_values;
result = deref[index];
}
else if (l->mapped_values != nullptr)
{
std::string index = r->result;
std::map<std::string, Value *> &deref = *l->mapped_values;
if (deref.count(index) == 0)
{
deref[index] = NEW_VALUE();
}
result = deref[index];
}
else
{
sprintf(this->error.message, "Cannot access non array/obj value");
error.is_critical = true;
raise_error();
}
return result;
}
else if (fragment->type == FragmentType::DO_FRAGMENT)
{
auto do_frag = (DoFragment *)fragment;
execute_statement(do_frag->stmt);
if (this->stored_value != nullptr)
{
return this->stored_value;
}
else
return NEW_VALUE("");
}
else if (fragment->type == FragmentType::FUNCTION_CALL)
{
FunctionCallFragment *function_call = (FunctionCallFragment *)(fragment);
Function *fun = this->functions[function_call->function_name];
std::vector<Statement *> vals = function_call->args->composed_statements;
if (fun == nullptr)
{
sprintf(error.message, "There is no function with name %s\n", function_call->function_name);
error.is_critical = true;
raise_error();
}
if (fun->is_native)
{
std::vector<Value *> evaluated;
for (int i = 0; i < vals.size(); i++)
{
Value *e = eval(vals[i]->ast_root);
evaluated.push_back(e);
}
auto result = fun->native_templated_func(evaluated, this);
return result;
}
else
{
auto args = fun->function_args->composed_statements;
std::vector<Variable *> created_variables;
Scope *scope = new Scope();
for (int i = 0; i < args.size(); i++)
{
auto var_frag = (VariableReferenceFragment *)(args[i]->ast_root);
auto val = eval(vals[i]->ast_root);
val->is_mature = false;
val->dispose = false;
auto var = new Variable();
var->value = val;
scope->local_variables[var_frag->name] = var;
}
this->scopes.push_back(scope);
for (auto &element : scope->local_variables)
{
element.second->value->is_mature = true;
}
this->execute_statement(fun->function_body);
drop_last_scope();
Value *result = stored_value != nullptr ? stored_value : nullptr;
stored_value = nullptr;
return result;
}
}
else if (fragment->type == FragmentType::VARIABLE)
{
auto variable = (VariableReferenceFragment *)fragment;
// reverse iterate through last scopes;
for (int i = scopes.size() - 1; i >= 0; i--)
{
auto &element = scopes[i];
if (element->local_variables.count(variable->name) != 0)
{
auto var_ref = element->local_variables[variable->name];
auto var_value = var_ref->value;
return var_value;
}
}
this->error.is_critical = true;
sprintf(this->error.message, "Failed to find variable with name %s \n", variable->name.c_str());
raise_error();
}
else if (fragment->type == FragmentType::VALUE)
{
// Duplicate value, very important!
Value *new_value = NEW_VALUE((Value *)fragment);
return new_value;
}
// non matched
return nullptr;
}
bool SEQL::Engine::evals_to_true(Fragment *condition_fragment)
{
Value *value = this->eval(condition_fragment);
if (value != nullptr && value->value_type == ValueType::BOOL && value->result[0] == 1)
{
return true;
}
return false;
}