|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 |
|
3 | 3 | #include "listblocks.h" |
| 4 | +#include "../engine/compiler.h" |
4 | 5 |
|
5 | 6 | using namespace libscratchcpp; |
6 | 7 |
|
7 | 8 | ListBlocks::ListBlocks() |
8 | 9 | { |
9 | 10 | // Blocks |
| 11 | + addCompileFunction("data_addtolist", &ListBlocks::compileAddToList); |
| 12 | + addCompileFunction("data_deleteoflist", &ListBlocks::compileDeleteFromList); |
| 13 | + addCompileFunction("data_deletealloflist", &ListBlocks::compileDeleteAllOfList); |
| 14 | + addCompileFunction("data_insertatlist", &ListBlocks::compileInsertToList); |
| 15 | + addCompileFunction("data_replaceitemoflist", &ListBlocks::compileReplaceItemOfList); |
| 16 | + addCompileFunction("data_itemoflist", &ListBlocks::compileItemOfList); |
| 17 | + addCompileFunction("data_itemnumoflist", &ListBlocks::compileItemNumberInList); |
| 18 | + addCompileFunction("data_lengthoflist", &ListBlocks::compileLengthOfList); |
| 19 | + addCompileFunction("data_listcontainsitem", &ListBlocks::compileListContainsItem); |
10 | 20 | addBlock("data_addtolist", &ListBlocks::addToList); |
11 | 21 | addBlock("data_deleteoflist", &ListBlocks::deleteFromList); |
12 | 22 | addBlock("data_deletealloflist", &ListBlocks::deleteAllOfList); |
@@ -35,6 +45,60 @@ bool ListBlocks::categoryVisible() const |
35 | 45 | return false; |
36 | 46 | } |
37 | 47 |
|
| 48 | +void ListBlocks::compileAddToList(Compiler *compiler) |
| 49 | +{ |
| 50 | + compiler->addInput(ITEM); |
| 51 | + compiler->addInstruction(vm::OP_LIST_APPEND, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 52 | +} |
| 53 | + |
| 54 | +void ListBlocks::compileDeleteFromList(Compiler *compiler) |
| 55 | +{ |
| 56 | + compiler->addInput(INDEX); |
| 57 | + compiler->addInstruction(vm::OP_LIST_DEL, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 58 | +} |
| 59 | + |
| 60 | +void ListBlocks::compileDeleteAllOfList(Compiler *compiler) |
| 61 | +{ |
| 62 | + compiler->addInstruction(vm::OP_LIST_DEL_ALL, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 63 | +} |
| 64 | + |
| 65 | +void ListBlocks::compileInsertToList(Compiler *compiler) |
| 66 | +{ |
| 67 | + compiler->addInput(ITEM); |
| 68 | + compiler->addInput(INDEX); |
| 69 | + compiler->addInstruction(vm::OP_LIST_INSERT, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 70 | +} |
| 71 | + |
| 72 | +void ListBlocks::compileReplaceItemOfList(Compiler *compiler) |
| 73 | +{ |
| 74 | + compiler->addInput(INDEX); |
| 75 | + compiler->addInput(ITEM); |
| 76 | + compiler->addInstruction(vm::OP_LIST_REPLACE, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 77 | +} |
| 78 | + |
| 79 | +void ListBlocks::compileItemOfList(Compiler *compiler) |
| 80 | +{ |
| 81 | + compiler->addInput(INDEX); |
| 82 | + compiler->addInstruction(vm::OP_LIST_GET_ITEM, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 83 | +} |
| 84 | + |
| 85 | +void ListBlocks::compileItemNumberInList(Compiler *compiler) |
| 86 | +{ |
| 87 | + compiler->addInput(ITEM); |
| 88 | + compiler->addInstruction(vm::OP_LIST_INDEX_OF, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 89 | +} |
| 90 | + |
| 91 | +void ListBlocks::compileLengthOfList(Compiler *compiler) |
| 92 | +{ |
| 93 | + compiler->addInstruction(vm::OP_LIST_LENGTH, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 94 | +} |
| 95 | + |
| 96 | +void ListBlocks::compileListContainsItem(Compiler *compiler) |
| 97 | +{ |
| 98 | + compiler->addInput(ITEM); |
| 99 | + compiler->addInstruction(vm::OP_LIST_LENGTH, { compiler->listIndex(compiler->field(LIST)->valuePtr()) }); |
| 100 | +} |
| 101 | + |
38 | 102 | Value ListBlocks::addToList(const BlockArgs &args) |
39 | 103 | { |
40 | 104 | auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr()); |
|
0 commit comments