Skip to content

Commit eec4126

Browse files
committed
Compiler: Add createSelect() method
1 parent 0e05e37 commit eec4126

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
8787
CompilerValue *createExp(CompilerValue *num);
8888
CompilerValue *createExp10(CompilerValue *num);
8989

90+
CompilerValue *createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType);
91+
9092
void createVariableWrite(Variable *variable, CompilerValue *value);
9193

9294
void createListClear(List *list);

src/dev/engine/compiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ CompilerValue *Compiler::createExp10(CompilerValue *num)
288288
return impl->builder->createExp10(num);
289289
}
290290

291+
/*! Creates a select instruction (ternary operator). */
292+
CompilerValue *Compiler::createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, StaticType valueType)
293+
{
294+
return impl->builder->createSelect(cond, trueValue, falseValue, valueType);
295+
}
296+
291297
/*! Creates a variable write operation. */
292298
void Compiler::createVariableWrite(Variable *variable, CompilerValue *value)
293299
{

test/dev/compiler/compiler_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,29 @@ TEST_F(CompilerTest, CreateExp10)
777777
compile(compiler, block);
778778
}
779779

780+
TEST_F(CompilerTest, CreateSelect)
781+
{
782+
Compiler compiler(&m_engine, &m_target);
783+
auto block = std::make_shared<Block>("", "");
784+
785+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
786+
CompilerValue arg1(Compiler::StaticType::Unknown);
787+
CompilerValue arg2(Compiler::StaticType::Unknown);
788+
CompilerValue arg3(Compiler::StaticType::Unknown);
789+
CompilerValue ret(Compiler::StaticType::Unknown);
790+
791+
EXPECT_CALL(*m_builder, createSelect(&arg1, &arg2, &arg3, Compiler::StaticType::Number)).WillOnce(Return(&ret));
792+
EXPECT_EQ(compiler->createSelect(&arg1, &arg2, &arg3, Compiler::StaticType::Number), &ret);
793+
794+
EXPECT_CALL(*m_builder, createSelect(&arg1, &arg2, &arg3, Compiler::StaticType::Unknown)).WillOnce(Return(nullptr));
795+
EXPECT_EQ(compiler->createSelect(&arg1, &arg2, &arg3, Compiler::StaticType::Unknown), nullptr);
796+
797+
return nullptr;
798+
});
799+
800+
compile(compiler, block);
801+
}
802+
780803
TEST_F(CompilerTest, CreateVariableWrite)
781804
{
782805
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)