Skip to content

Commit 30ae487

Browse files
committed
Implement control_repeat
1 parent cf1730c commit 30ae487

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/dev/blocks/controlblocks.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ std::string ControlBlocks::description() const
2323
void ControlBlocks::registerBlocks(IEngine *engine)
2424
{
2525
engine->addCompileFunction(this, "control_forever", &compileForever);
26+
engine->addCompileFunction(this, "control_repeat", &compileRepeat);
2627
}
2728

2829
CompilerValue *ControlBlocks::compileForever(Compiler *compiler)
@@ -32,3 +33,10 @@ CompilerValue *ControlBlocks::compileForever(Compiler *compiler)
3233
compiler->moveToWhileLoop(compiler->addConstValue(true), substack ? substack->valueBlock() : nullptr);
3334
return nullptr;
3435
}
36+
37+
CompilerValue *ControlBlocks::compileRepeat(Compiler *compiler)
38+
{
39+
auto substack = compiler->input("SUBSTACK");
40+
compiler->moveToRepeatLoop(compiler->addInput("TIMES"), substack ? substack->valueBlock() : nullptr);
41+
return nullptr;
42+
}

src/dev/blocks/controlblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ControlBlocks : public IExtension
1717

1818
private:
1919
static CompilerValue *compileForever(Compiler *compiler);
20+
static CompilerValue *compileRepeat(Compiler *compiler);
2021
};
2122

2223
} // namespace libscratchcpp

test/dev/blocks/control_blocks_test.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,40 @@ TEST_F(ControlBlocksTest, Forever)
6868
}
6969
}
7070
}
71+
72+
TEST_F(ControlBlocksTest, Repeat)
73+
{
74+
auto target = std::make_shared<Sprite>();
75+
76+
{
77+
ScriptBuilder builder(m_extension.get(), m_engine, target);
78+
79+
builder.addBlock("control_repeat");
80+
auto substack = std::make_shared<Block>("", "test_print_test");
81+
builder.addObscuredInput("SUBSTACK", substack);
82+
builder.addValueInput("TIMES", 5);
83+
84+
builder.build();
85+
86+
testing::internal::CaptureStdout();
87+
builder.run();
88+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\ntest\ntest\ntest\ntest\n");
89+
}
90+
91+
m_engine->clear();
92+
target = std::make_shared<Sprite>();
93+
94+
{
95+
ScriptBuilder builder(m_extension.get(), m_engine, target);
96+
builder.addBlock("control_repeat");
97+
builder.addValueInput("TIMES", "Infinity");
98+
99+
builder.build();
100+
m_engine->start();
101+
102+
for (int i = 0; i < 2; i++) {
103+
m_engine->step();
104+
ASSERT_TRUE(m_engine->isRunning());
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)