Skip to content

Commit 297e841

Browse files
committed
ListBlocks: Add compile functions
1 parent 3af11d7 commit 297e841

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/blocks/listblocks.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include "listblocks.h"
4+
#include "../engine/compiler.h"
45

56
using namespace libscratchcpp;
67

78
ListBlocks::ListBlocks()
89
{
910
// 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);
1020
addBlock("data_addtolist", &ListBlocks::addToList);
1121
addBlock("data_deleteoflist", &ListBlocks::deleteFromList);
1222
addBlock("data_deletealloflist", &ListBlocks::deleteAllOfList);
@@ -35,6 +45,60 @@ bool ListBlocks::categoryVisible() const
3545
return false;
3646
}
3747

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+
38102
Value ListBlocks::addToList(const BlockArgs &args)
39103
{
40104
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());

src/blocks/listblocks.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ class LIBSCRATCHCPP_EXPORT ListBlocks : public IBlockSection
2727
std::string name() const override;
2828
bool categoryVisible() const override;
2929

30+
static void compileAddToList(Compiler *compiler);
31+
static void compileDeleteFromList(Compiler *compiler);
32+
static void compileDeleteAllOfList(Compiler *compiler);
33+
static void compileInsertToList(Compiler *compiler);
34+
static void compileReplaceItemOfList(Compiler *compiler);
35+
static void compileItemOfList(Compiler *compiler);
36+
static void compileItemNumberInList(Compiler *compiler);
37+
static void compileLengthOfList(Compiler *compiler);
38+
static void compileListContainsItem(Compiler *compiler);
3039
static Value addToList(const BlockArgs &args);
3140
static Value deleteFromList(const BlockArgs &args);
3241
static Value deleteAllOfList(const BlockArgs &args);

0 commit comments

Comments
 (0)