Skip to content

Commit 94de97e

Browse files
committed
Implement control_if
1 parent 30ae487 commit 94de97e

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
@@ -24,6 +24,7 @@ void ControlBlocks::registerBlocks(IEngine *engine)
2424
{
2525
engine->addCompileFunction(this, "control_forever", &compileForever);
2626
engine->addCompileFunction(this, "control_repeat", &compileRepeat);
27+
engine->addCompileFunction(this, "control_if", &compileIf);
2728
}
2829

2930
CompilerValue *ControlBlocks::compileForever(Compiler *compiler)
@@ -40,3 +41,10 @@ CompilerValue *ControlBlocks::compileRepeat(Compiler *compiler)
4041
compiler->moveToRepeatLoop(compiler->addInput("TIMES"), substack ? substack->valueBlock() : nullptr);
4142
return nullptr;
4243
}
44+
45+
CompilerValue *ControlBlocks::compileIf(Compiler *compiler)
46+
{
47+
auto substack = compiler->input("SUBSTACK");
48+
compiler->moveToIf(compiler->addInput("CONDITION"), substack ? substack->valueBlock() : nullptr);
49+
return nullptr;
50+
}

src/dev/blocks/controlblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ControlBlocks : public IExtension
1818
private:
1919
static CompilerValue *compileForever(Compiler *compiler);
2020
static CompilerValue *compileRepeat(Compiler *compiler);
21+
static CompilerValue *compileIf(Compiler *compiler);
2122
};
2223

2324
} // namespace libscratchcpp

test/dev/blocks/control_blocks_test.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,40 @@ TEST_F(ControlBlocksTest, Repeat)
105105
}
106106
}
107107
}
108+
109+
TEST_F(ControlBlocksTest, If)
110+
{
111+
auto target = std::make_shared<Sprite>();
112+
113+
{
114+
ScriptBuilder builder(m_extension.get(), m_engine, target);
115+
116+
builder.addBlock("control_if");
117+
auto substack = std::make_shared<Block>("", "test_print_test");
118+
builder.addValueInput("CONDITION", false);
119+
builder.addObscuredInput("SUBSTACK", substack);
120+
121+
builder.addBlock("control_if");
122+
substack = std::make_shared<Block>("", "test_print_test");
123+
builder.addValueInput("CONDITION", true);
124+
builder.addObscuredInput("SUBSTACK", substack);
125+
126+
builder.build();
127+
128+
testing::internal::CaptureStdout();
129+
builder.run();
130+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\n");
131+
}
132+
133+
m_engine->clear();
134+
target = std::make_shared<Sprite>();
135+
136+
{
137+
ScriptBuilder builder(m_extension.get(), m_engine, target);
138+
builder.addBlock("control_if");
139+
builder.addValueInput("CONDITION", true);
140+
141+
builder.build();
142+
builder.run();
143+
}
144+
}

0 commit comments

Comments
 (0)