Skip to content

Commit 4a75de2

Browse files
committed
Drop block implementation functions
1 parent 57986ab commit 4a75de2

31 files changed

+52
-887
lines changed

src/blocks/controlblocks.cpp

Lines changed: 6 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,15 @@
66
namespace libscratchcpp
77
{
88

9-
std::map<std::pair<RunningScript *, Block *>, std::pair<int, int>> ControlBlocks::repeatLoops;
10-
119
ControlBlocks::ControlBlocks()
1210
{
1311
// Blocks
14-
addCompileFunction("control_forever", &ControlBlocks::compileRepeatForever);
15-
addCompileFunction("control_repeat", &ControlBlocks::compileRepeat);
16-
addCompileFunction("control_repeat_until", &ControlBlocks::compileRepeatUntil);
17-
addCompileFunction("control_if", &ControlBlocks::compileIfStatement);
18-
addCompileFunction("control_if_else", &ControlBlocks::compileIfElseStatement);
19-
addCompileFunction("control_stop", &ControlBlocks::compileStop);
20-
addBlock("control_forever", &ControlBlocks::repeatForever);
21-
addBlock("control_repeat", &ControlBlocks::repeat);
22-
addBlock("control_repeat_until", &ControlBlocks::repeatUntil);
23-
addBlock("control_if", &ControlBlocks::ifStatement);
24-
addBlock("control_if_else", &ControlBlocks::ifElseStatement);
12+
addCompileFunction("control_forever", &compileRepeatForever);
13+
addCompileFunction("control_repeat", &compileRepeat);
14+
addCompileFunction("control_repeat_until", &compileRepeatUntil);
15+
addCompileFunction("control_if", &compileIfStatement);
16+
addCompileFunction("control_if_else", &compileIfElseStatement);
17+
addCompileFunction("control_stop", &compileStop);
2518

2619
// Inputs
2720
addInput("SUBSTACK", SUBSTACK);
@@ -141,105 +134,4 @@ unsigned int ControlBlocks::stopOtherScriptsInSprite(VirtualMachine *vm)
141134
return 0;
142135
}
143136

144-
Value ControlBlocks::repeatForever(const BlockArgs &args)
145-
{
146-
auto script = args.script();
147-
if (script->atCMouthEnd()) {
148-
// Return false to prevent breaking from a forever loop
149-
return false;
150-
} else {
151-
// Move to the C mouth
152-
script->moveToSubstack(args, SUBSTACK);
153-
return Value();
154-
}
155-
}
156-
157-
Value ControlBlocks::repeat(const BlockArgs &args)
158-
{
159-
auto script = args.script();
160-
if (script->atCMouthEnd()) {
161-
Block *cMouth = script->cMouth().get();
162-
auto loop = repeatLoops[{ script, cMouth }];
163-
int i = loop.first + 1;
164-
repeatLoops[{ script, cMouth }] = { i, loop.second };
165-
if (i >= loop.second) {
166-
repeatLoops.erase({ script, cMouth });
167-
return true;
168-
} else
169-
return false;
170-
} else {
171-
int count = args.input(TIMES)->value().toInt();
172-
if (count > 0) {
173-
auto substack = script->getSubstack(args, SUBSTACK);
174-
if (substack) {
175-
// Create the loop
176-
repeatLoops[{ script, script->currentBlock().get() }] = { 0, count };
177-
178-
script->moveToSubstack(substack);
179-
}
180-
}
181-
return Value();
182-
}
183-
}
184-
185-
Value ControlBlocks::repeatUntil(const BlockArgs &args)
186-
{
187-
auto script = args.script();
188-
bool cond = args.input(CONDITION)->value().toBool();
189-
if (script->atCMouthEnd())
190-
return cond;
191-
else {
192-
if (cond)
193-
script->skipSubstack();
194-
else
195-
script->moveToSubstack(args, SUBSTACK);
196-
return Value();
197-
}
198-
}
199-
200-
Value ControlBlocks::repeatWhile(const BlockArgs &args)
201-
{
202-
auto script = args.script();
203-
bool cond = args.input(CONDITION)->value().toBool();
204-
if (script->atCMouthEnd())
205-
return !cond;
206-
else {
207-
if (cond)
208-
script->moveToSubstack(args, SUBSTACK);
209-
else
210-
script->skipSubstack();
211-
return Value();
212-
}
213-
}
214-
215-
Value ControlBlocks::ifStatement(const BlockArgs &args)
216-
{
217-
auto script = args.script();
218-
bool cond = args.input(CONDITION)->value().toBool();
219-
if (script->atCMouthEnd())
220-
return true;
221-
else {
222-
if (cond)
223-
script->moveToSubstack(args, SUBSTACK);
224-
else
225-
script->skipSubstack();
226-
return Value();
227-
}
228-
}
229-
230-
Value ControlBlocks::ifElseStatement(const BlockArgs &args)
231-
{
232-
auto script = args.script();
233-
bool cond = args.input(CONDITION)->value().toBool();
234-
if (script->atCMouthEnd())
235-
return true;
236-
else {
237-
if (cond)
238-
script->moveToSubstack(args, SUBSTACK);
239-
else
240-
script->moveToSubstack(args, SUBSTACK2);
241-
return Value();
242-
}
243-
}
244-
245137
} // namespace libscratchcpp

src/blocks/controlblocks.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ class LIBSCRATCHCPP_EXPORT ControlBlocks : public IBlockSection
4545

4646
static unsigned int stopAll(VirtualMachine *vm);
4747
static unsigned int stopOtherScriptsInSprite(VirtualMachine *vm);
48-
static Value repeatForever(const BlockArgs &args);
49-
static Value repeat(const BlockArgs &args);
50-
static Value repeatUntil(const BlockArgs &args);
51-
static Value repeatWhile(const BlockArgs &args);
52-
static Value ifStatement(const BlockArgs &args);
53-
static Value ifElseStatement(const BlockArgs &args);
54-
55-
private:
56-
// pair<script, C mouth>, pair<current index, repeat count>
57-
static std::map<std::pair<RunningScript *, Block *>, std::pair<int, int>> repeatLoops;
5848
};
5949

6050
} // namespace libscratchcpp

src/blocks/listblocks.cpp

Lines changed: 9 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,15 @@ using namespace libscratchcpp;
88
ListBlocks::ListBlocks()
99
{
1010
// 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);
20-
addBlock("data_addtolist", &ListBlocks::addToList);
21-
addBlock("data_deleteoflist", &ListBlocks::deleteFromList);
22-
addBlock("data_deletealloflist", &ListBlocks::deleteAllOfList);
23-
addBlock("data_insertatlist", &ListBlocks::insertToList);
24-
addBlock("data_replaceitemoflist", &ListBlocks::replaceItemOfList);
25-
addBlock("data_itemoflist", &ListBlocks::itemOfList);
26-
addBlock("data_itemnumoflist", &ListBlocks::itemNumberInList);
27-
addBlock("data_lengthoflist", &ListBlocks::lengthOfList);
28-
addBlock("data_listcontainsitem", &ListBlocks::listContainsItem);
11+
addCompileFunction("data_addtolist", &compileAddToList);
12+
addCompileFunction("data_deleteoflist", &compileDeleteFromList);
13+
addCompileFunction("data_deletealloflist", &compileDeleteAllOfList);
14+
addCompileFunction("data_insertatlist", &compileInsertToList);
15+
addCompileFunction("data_replaceitemoflist", &compileReplaceItemOfList);
16+
addCompileFunction("data_itemoflist", &compileItemOfList);
17+
addCompileFunction("data_itemnumoflist", &compileItemNumberInList);
18+
addCompileFunction("data_lengthoflist", &compileLengthOfList);
19+
addCompileFunction("data_listcontainsitem", &compileListContainsItem);
2920

3021
// Inputs
3122
addInput("ITEM", ITEM);
@@ -98,133 +89,3 @@ void ListBlocks::compileListContainsItem(Compiler *compiler)
9889
compiler->addInput(ITEM);
9990
compiler->addInstruction(vm::OP_LIST_LENGTH, { compiler->listIndex(compiler->field(LIST)->valuePtr()) });
10091
}
101-
102-
Value ListBlocks::addToList(const BlockArgs &args)
103-
{
104-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
105-
list->push_back(args.input(ITEM)->value());
106-
return Value();
107-
}
108-
109-
Value ListBlocks::deleteFromList(const BlockArgs &args)
110-
{
111-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
112-
Value value = args.input(INDEX)->value();
113-
size_t index;
114-
if (value.isString()) {
115-
std::string str = value.toString();
116-
if (str == "last")
117-
index = list->size();
118-
else if (str == "all") {
119-
list->clear();
120-
return Value();
121-
} else if (str == "random") {
122-
index = list->size() == 0 ? 0 : randint<size_t>(1, list->size());
123-
} else
124-
return Value();
125-
} else
126-
index = validateIndex(value.toInt(), list->size());
127-
if (index == 0)
128-
return Value();
129-
list->removeAt(index - 1);
130-
return Value();
131-
}
132-
133-
Value ListBlocks::deleteAllOfList(const BlockArgs &args)
134-
{
135-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
136-
list->clear();
137-
return Value();
138-
}
139-
140-
Value ListBlocks::insertToList(const BlockArgs &args)
141-
{
142-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
143-
Value value = args.input(INDEX)->value();
144-
int index;
145-
if (value.isString()) {
146-
std::string str = value.toString();
147-
if (str == "last") {
148-
list->push_back(args.input(ITEM)->value());
149-
return Value();
150-
} else if (str == "random") {
151-
index = list->size() == 0 ? 1 : randint<size_t>(1, list->size());
152-
} else
153-
return Value();
154-
} else {
155-
index = validateIndex(value.toInt(), list->size());
156-
if (index == 0)
157-
return Value();
158-
}
159-
list->insert(index - 1, args.input(ITEM)->value());
160-
return Value();
161-
}
162-
163-
Value ListBlocks::replaceItemOfList(const BlockArgs &args)
164-
{
165-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
166-
Value value = args.input(INDEX)->value();
167-
int index;
168-
if (value.isString()) {
169-
std::string str = value.toString();
170-
if (str == "last")
171-
index = list->size();
172-
else if (str == "random") {
173-
index = list->size() == 0 ? 0 : randint<size_t>(1, list->size());
174-
} else
175-
return Value();
176-
} else
177-
index = validateIndex(value.toInt(), list->size());
178-
if (index == 0)
179-
return Value();
180-
list->replace(index - 1, args.input(ITEM)->value());
181-
return Value();
182-
}
183-
184-
Value ListBlocks::itemOfList(const BlockArgs &args)
185-
{
186-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
187-
Value value = args.input(INDEX)->value();
188-
int index;
189-
if (value.isString()) {
190-
std::string str = value.toString();
191-
if (str == "last")
192-
index = list->size();
193-
else if (str == "random") {
194-
index = list->size() == 0 ? 0 : randint<size_t>(1, list->size());
195-
} else
196-
return "";
197-
} else
198-
index = validateIndex(value.toInt(), list->size());
199-
if (index == 0)
200-
return "";
201-
return list->at(index - 1);
202-
}
203-
204-
Value ListBlocks::itemNumberInList(const BlockArgs &args)
205-
{
206-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
207-
return list->indexOf(args.input(ITEM)->value()) + 1;
208-
}
209-
210-
Value ListBlocks::lengthOfList(const BlockArgs &args)
211-
{
212-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
213-
return list->size();
214-
}
215-
216-
Value ListBlocks::listContainsItem(const BlockArgs &args)
217-
{
218-
auto list = std::static_pointer_cast<List>(args.field(LIST)->valuePtr());
219-
return list->contains(args.input(ITEM)->value());
220-
}
221-
222-
int ListBlocks::validateIndex(size_t index, size_t listLength)
223-
{
224-
if (listLength == 0) {
225-
if (index != 1)
226-
return 0;
227-
} else if ((index < 1) || (index > listLength))
228-
return 0;
229-
return index;
230-
}

src/blocks/listblocks.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ class LIBSCRATCHCPP_EXPORT ListBlocks : public IBlockSection
3636
static void compileItemNumberInList(Compiler *compiler);
3737
static void compileLengthOfList(Compiler *compiler);
3838
static void compileListContainsItem(Compiler *compiler);
39-
static Value addToList(const BlockArgs &args);
40-
static Value deleteFromList(const BlockArgs &args);
41-
static Value deleteAllOfList(const BlockArgs &args);
42-
static Value insertToList(const BlockArgs &args);
43-
static Value replaceItemOfList(const BlockArgs &args);
44-
static Value itemOfList(const BlockArgs &args);
45-
static Value itemNumberInList(const BlockArgs &args);
46-
static Value lengthOfList(const BlockArgs &args);
47-
static Value listContainsItem(const BlockArgs &args);
4839

4940
private:
5041
static int validateIndex(size_t index, size_t listLength);

0 commit comments

Comments
 (0)