-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcompiler.cpp
More file actions
501 lines (419 loc) · 12.9 KB
/
compiler.cpp
File metadata and controls
501 lines (419 loc) · 12.9 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// SPDX-License-Identifier: Apache-2.0
#include <scratchcpp/dev/compiler.h>
#include <scratchcpp/block.h>
#include <scratchcpp/input.h>
#include <scratchcpp/inputvalue.h>
#include "compiler_p.h"
#include "internal/icodebuilderfactory.h"
#include "internal/icodebuilder.h"
using namespace libscratchcpp;
/*! Constructs Compiler. */
Compiler::Compiler(IEngine *engine, Target *target) :
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
{
}
/*! Returns the Engine of the project. */
IEngine *Compiler::engine() const
{
return impl->engine;
}
/*! Returns the Target of this compiler. */
Target *Compiler::target() const
{
return impl->target;
}
/*! Returns currently compiled block. */
std::shared_ptr<libscratchcpp::Block> Compiler::block() const
{
return impl->block;
}
/*! Compiles the script starting with the given block. */
std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBlock)
{
impl->builder = impl->builderFactory->create(impl->target, startBlock->id(), false);
impl->substackTree.clear();
impl->substackHit = false;
impl->warp = false;
impl->block = startBlock;
while (impl->block) {
if (impl->block->compileFunction()) {
assert(impl->customIfStatementCount == 0);
impl->block->compile(this);
if (impl->customIfStatementCount > 0) {
std::cerr << "error: if statement created by block '" << impl->block->opcode() << "' not terminated" << std::endl;
assert(false);
}
} else {
std::cout << "warning: unsupported block: " << impl->block->opcode() << std::endl;
impl->unsupportedBlocks.insert(impl->block->opcode());
}
if (impl->substackHit) {
impl->substackHit = false;
continue;
}
if (impl->block)
impl->block = impl->block->next();
if (!impl->block && !impl->substackTree.empty())
impl->substackEnd();
}
return impl->builder->finalize();
}
/*!
* Adds a call to the given function.\n
* For example: extern "C" bool some_block(Target *target, double arg1, const char *arg2)
*/
void Compiler::addFunctionCall(const std::string &functionName, StaticType returnType, const std::vector<StaticType> &argTypes)
{
impl->builder->addFunctionCall(functionName, returnType, argTypes);
}
/*! Adds a constant value to the compiled code. */
void Compiler::addConstValue(const Value &value)
{
impl->builder->addConstValue(value);
}
/*! Adds the given variable to the code (to read it). */
void Compiler::addVariableValue(Variable *variable)
{
impl->builder->addVariableValue(variable);
}
/*! Adds the given list to the code (to read its string representation). */
void Compiler::addListContents(List *list)
{
impl->builder->addListContents(list);
}
/*! Adds the item with index from the last value of the given list to the code. */
void Compiler::addListItem(List *list)
{
impl->builder->addListItem(list);
}
/*! Adds the index of the item from the last value of the given list to the code. */
void Compiler::addListItemIndex(List *list)
{
impl->builder->addListItemIndex(list);
}
/*! Adds the result of a list contains item from the check to the code. */
void Compiler::addListContains(List *list)
{
impl->builder->addListContains(list);
}
/*! Adds the length of the given list to the code. */
void Compiler::addListSize(List *list)
{
impl->builder->addListSize(list);
}
/*! Compiles the given input (resolved by name) and adds it to the compiled code. */
void Compiler::addInput(const std::string &name)
{
addInput(impl->block->inputAt(impl->block->findInput(name)).get());
}
/*! Creates an add instruction from the last 2 values. */
void Compiler::createAdd()
{
impl->builder->createAdd();
}
/*! Creates a subtract instruction from the last 2 values. */
void Compiler::createSub()
{
impl->builder->createSub();
}
/*! Creates a multiply instruction from the last 2 values. */
void Compiler::createMul()
{
impl->builder->createMul();
}
/*! Creates a divide instruction from the last 2 values. */
void Compiler::createDiv()
{
impl->builder->createDiv();
}
/*! Creates an equality comparison instruction using the last 2 values. */
void Compiler::createCmpEQ()
{
impl->builder->createCmpEQ();
}
/*! Creates a greater than comparison instruction using the last 2 values. */
void Compiler::createCmpGT()
{
impl->builder->createCmpGT();
}
/*! Creates a lower than comparison instruction using the last 2 values. */
void Compiler::createCmpLT()
{
impl->builder->createCmpLT();
}
/*! Creates an AND operation using the last 2 values. */
void Compiler::createAnd()
{
impl->builder->createAnd();
}
/*! Creates an OR operation using the last 2 values. */
void Compiler::createOr()
{
impl->builder->createOr();
}
/*! Creates a NOT operation using the last value. */
void Compiler::createNot()
{
impl->builder->createNot();
}
/*! Creates a remainder operation using the last 2 values. */
void Compiler::createMod()
{
impl->builder->createMod();
}
/*! Creates a round operation using the last value. */
void Compiler::createRound()
{
impl->builder->createRound();
}
/*! Creates an abs operation using the last value. */
void Compiler::createAbs()
{
impl->builder->createAbs();
}
/*! Creates a floor operation using the last value. */
void Compiler::createFloor()
{
impl->builder->createFloor();
}
/*! Creates a ceiling operation using the last value. */
void Compiler::createCeil()
{
impl->builder->createCeil();
}
/*! Creates a square root operation using the last value. */
void Compiler::createSqrt()
{
impl->builder->createSqrt();
}
/*! Creates a sin operation using the last value. */
void Compiler::createSin()
{
impl->builder->createSin();
}
/*! Creates a cos operation using the last value. */
void Compiler::createCos()
{
impl->builder->createCos();
}
/*! Creates a tan operation using the last value. */
void Compiler::createTan()
{
impl->builder->createTan();
}
/*! Creates an asin operation using the last value. */
void Compiler::createAsin()
{
impl->builder->createAsin();
}
/*! Creates an acos operation using the last value. */
void Compiler::createAcos()
{
impl->builder->createAcos();
}
/*! Creates an atan operation using the last value. */
void Compiler::createAtan()
{
impl->builder->createAtan();
}
/*! Creates an ln operation using the last value. */
void Compiler::createLn()
{
impl->builder->createLn();
}
/*! Creates a log10 operation using the last value. */
void Compiler::createLog10()
{
impl->builder->createLog10();
}
/*! Creates an e^x operation using the last value. */
void Compiler::createExp()
{
impl->builder->createExp();
}
/*! Creates a 10^x operation using the last value. */
void Compiler::createExp10()
{
impl->builder->createExp10();
}
/*! Creates a variable write operation using the last value. */
void Compiler::createVariableWrite(Variable *variable)
{
impl->builder->createVariableWrite(variable);
}
/*! Creates a clear list operation. */
void Compiler::createListClear(List *list)
{
impl->builder->createListClear(list);
}
/*!
* Creates a remove item from list operation.
* \note The index starts with 0 and is cast to number, special strings like "last" are not handled.
*/
void Compiler::createListRemove(List *list)
{
impl->builder->createListRemove(list);
}
/*! Creates a list append operation using the last value. */
void Compiler::createListAppend(List *list)
{
impl->builder->createListAppend(list);
}
/*! Creates a list insert operation using the last 2 values (index, value). */
void Compiler::createListInsert(List *list)
{
impl->builder->createListInsert(list);
}
/*! Creates a list replace operation using the last 2 values (index, value). */
void Compiler::createListReplace(List *list)
{
impl->builder->createListReplace(list);
}
/*!
* Starts a custom if statement.
* \note The if statement must be terminated using endIf() after compiling your block.
*/
void Compiler::beginIfStatement()
{
impl->builder->beginIfStatement();
impl->customIfStatementCount++;
}
/*! Starts the else branch of custom if statement. */
void Compiler::beginElseBranch()
{
impl->builder->beginElseBranch();
}
/*! Ends custom if statement. */
void Compiler::endIf()
{
if (impl->customIfStatementCount == 0) {
std::cerr << "error: called Compiler::endIf() without an if statement";
assert(false);
return;
}
impl->builder->endIf();
impl->customIfStatementCount--;
}
/*! Jumps to the given if substack. */
void Compiler::moveToIf(std::shared_ptr<Block> substack)
{
if (!substack)
return; // ignore empty if statements
impl->substackHit = true;
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::IfStatement });
impl->block = substack;
impl->builder->beginIfStatement();
}
/*! Jumps to the given if/else substack. The second substack is used for the else branch. */
void Compiler::moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2)
{
if (!substack1 && !substack2)
return; // ignore empty if statements
impl->substackHit = true;
impl->substackTree.push_back({ { impl->block, substack2 }, CompilerPrivate::SubstackType::IfStatement });
impl->block = substack1;
impl->builder->beginIfStatement();
if (!impl->block)
impl->substackEnd();
}
/*! Jumps to the given repeat loop substack. */
void Compiler::moveToRepeatLoop(std::shared_ptr<Block> substack)
{
impl->substackHit = true;
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::Loop });
impl->block = substack;
impl->builder->beginRepeatLoop();
if (!impl->block)
impl->substackEnd();
}
/*! Jumps to the given while loop substack. */
void Compiler::moveToWhileLoop(std::shared_ptr<Block> substack)
{
impl->substackHit = true;
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::Loop });
impl->block = substack;
impl->builder->beginWhileLoop();
if (!impl->block)
impl->substackEnd();
}
/*! Jumps to the given until loop substack. */
void Compiler::moveToRepeatUntilLoop(std::shared_ptr<Block> substack)
{
impl->substackHit = true;
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::Loop });
impl->block = substack;
impl->builder->beginRepeatUntilLoop();
if (!impl->block)
impl->substackEnd();
}
/*! Begins a while/until loop condition. */
void Compiler::beginLoopCondition()
{
impl->builder->beginLoopCondition();
}
/*! Makes current script run without screen refresh. */
void Compiler::warp()
{
impl->warp = true;
}
/*! Convenience method which returns the field with the given name. */
Input *Compiler::input(const std::string &name) const
{
return impl->block->inputAt(impl->block->findInput(name)).get();
}
/*! Convenience method which returns the field with the given name. */
Field *Compiler::field(const std::string &name) const
{
return impl->block->fieldAt(impl->block->findField(name)).get();
}
/*! Returns unsupported block opcodes which were found when compiling. */
const std::unordered_set<std::string> &Compiler::unsupportedBlocks() const
{
return impl->unsupportedBlocks;
}
void Compiler::addInput(Input *input)
{
if (!input) {
addConstValue(Value());
return;
}
switch (input->type()) {
case Input::Type::Shadow:
case Input::Type::NoShadow: {
if (input->pointsToDropdownMenu())
addConstValue(input->selectedMenuItem());
else {
auto previousBlock = impl->block;
impl->block = input->valueBlock();
if (impl->block) {
if (impl->block->compileFunction())
impl->block->compile(this);
else {
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
impl->unsupportedBlocks.insert(impl->block->opcode());
addConstValue(Value());
}
} else
addConstValue(input->primaryValue()->value());
impl->block = previousBlock;
}
break;
}
case Input::Type::ObscuredShadow: {
auto previousBlock = impl->block;
impl->block = input->valueBlock();
if (impl->block) {
if (impl->block->compileFunction())
impl->block->compile(this);
else {
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
impl->unsupportedBlocks.insert(impl->block->opcode());
addConstValue(Value());
}
} else
input->primaryValue()->compile(this);
impl->block = previousBlock;
break;
}
}
}