Skip to content

Commit d2200b9

Browse files
committed
Implement control_if_else
1 parent 6784aaf commit d2200b9

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

src/dev/blocks/controlblocks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void ControlBlocks::registerBlocks(IEngine *engine)
2525
engine->addCompileFunction(this, "control_forever", &compileForever);
2626
engine->addCompileFunction(this, "control_repeat", &compileRepeat);
2727
engine->addCompileFunction(this, "control_if", &compileIf);
28+
engine->addCompileFunction(this, "control_if_else", &compileIfElse);
2829
}
2930

3031
CompilerValue *ControlBlocks::compileForever(Compiler *compiler)
@@ -48,3 +49,11 @@ CompilerValue *ControlBlocks::compileIf(Compiler *compiler)
4849
compiler->moveToIf(compiler->addInput("CONDITION"), substack ? substack->valueBlock() : nullptr);
4950
return nullptr;
5051
}
52+
53+
CompilerValue *ControlBlocks::compileIfElse(Compiler *compiler)
54+
{
55+
auto substack = compiler->input("SUBSTACK");
56+
auto substack2 = compiler->input("SUBSTACK2");
57+
compiler->moveToIfElse(compiler->addInput("CONDITION"), substack ? substack->valueBlock() : nullptr, substack2 ? substack2->valueBlock() : nullptr);
58+
return nullptr;
59+
}

src/dev/blocks/controlblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ControlBlocks : public IExtension
1919
static CompilerValue *compileForever(Compiler *compiler);
2020
static CompilerValue *compileRepeat(Compiler *compiler);
2121
static CompilerValue *compileIf(Compiler *compiler);
22+
static CompilerValue *compileIfElse(Compiler *compiler);
2223
};
2324

2425
} // namespace libscratchcpp

test/dev/blocks/control_blocks_test.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,101 @@ TEST_F(ControlBlocksTest, If)
152152
builder.run();
153153
}
154154
}
155+
156+
TEST_F(ControlBlocksTest, IfElse)
157+
{
158+
auto target = std::make_shared<Sprite>();
159+
160+
{
161+
ScriptBuilder builder(m_extension.get(), m_engine, target);
162+
163+
builder.addBlock("control_if_else");
164+
builder.addValueInput("CONDITION", false);
165+
auto substack = std::make_shared<Block>("", "test_print_test");
166+
builder.addObscuredInput("SUBSTACK", substack);
167+
auto substack2 = std::make_shared<Block>("", "test_print_test2");
168+
builder.addObscuredInput("SUBSTACK2", substack2);
169+
170+
builder.addBlock("control_if_else");
171+
builder.addNullObscuredInput("CONDITION");
172+
substack = std::make_shared<Block>("", "test_print_test");
173+
builder.addObscuredInput("SUBSTACK", substack);
174+
substack2 = std::make_shared<Block>("", "test_print_test2");
175+
builder.addObscuredInput("SUBSTACK2", substack2);
176+
177+
builder.addBlock("control_if_else");
178+
builder.addValueInput("CONDITION", true);
179+
substack = std::make_shared<Block>("", "test_print_test");
180+
builder.addObscuredInput("SUBSTACK", substack);
181+
substack2 = std::make_shared<Block>("", "test_print_test2");
182+
builder.addObscuredInput("SUBSTACK2", substack2);
183+
184+
builder.build();
185+
186+
testing::internal::CaptureStdout();
187+
builder.run();
188+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test2\ntest2\ntest\n");
189+
}
190+
191+
m_engine->clear();
192+
target = std::make_shared<Sprite>();
193+
194+
{
195+
ScriptBuilder builder(m_extension.get(), m_engine, target);
196+
197+
builder.addBlock("control_if_else");
198+
builder.addValueInput("CONDITION", false);
199+
auto substack2 = std::make_shared<Block>("", "test_print_test2");
200+
builder.addObscuredInput("SUBSTACK2", substack2);
201+
202+
builder.addBlock("control_if_else");
203+
builder.addValueInput("CONDITION", true);
204+
substack2 = std::make_shared<Block>("", "test_print_test2");
205+
builder.addObscuredInput("SUBSTACK2", substack2);
206+
207+
builder.build();
208+
209+
testing::internal::CaptureStdout();
210+
builder.run();
211+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test2\n");
212+
}
213+
214+
m_engine->clear();
215+
target = std::make_shared<Sprite>();
216+
217+
{
218+
ScriptBuilder builder(m_extension.get(), m_engine, target);
219+
220+
builder.addBlock("control_if_else");
221+
builder.addValueInput("CONDITION", false);
222+
auto substack = std::make_shared<Block>("", "test_print_test");
223+
builder.addObscuredInput("SUBSTACK", substack);
224+
225+
builder.addBlock("control_if_else");
226+
builder.addValueInput("CONDITION", true);
227+
substack = std::make_shared<Block>("", "test_print_test");
228+
builder.addObscuredInput("SUBSTACK", substack);
229+
230+
builder.build();
231+
232+
testing::internal::CaptureStdout();
233+
builder.run();
234+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\n");
235+
}
236+
237+
m_engine->clear();
238+
target = std::make_shared<Sprite>();
239+
240+
{
241+
ScriptBuilder builder(m_extension.get(), m_engine, target);
242+
243+
builder.addBlock("control_if_else");
244+
builder.addValueInput("CONDITION", false);
245+
246+
builder.addBlock("control_if_else");
247+
builder.addValueInput("CONDITION", true);
248+
249+
builder.build();
250+
builder.run();
251+
}
252+
}

test/dev/blocks/util.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ void registerBlocks(IEngine *engine, IExtension *extension)
2222
compiler->addFunctionCall("test_print", Compiler::StaticType::Void, { Compiler::StaticType::String }, { input });
2323
return nullptr;
2424
});
25+
26+
engine->addCompileFunction(extension, "test_print_test2", [](Compiler *compiler) -> CompilerValue * {
27+
auto input = compiler->addConstValue("test2");
28+
compiler->addFunctionCall("test_print", Compiler::StaticType::Void, { Compiler::StaticType::String }, { input });
29+
return nullptr;
30+
});
2531
}
2632

2733
extern "C" void test_print(const char *str)

0 commit comments

Comments
 (0)