Skip to content

Commit cc2aca9

Browse files
committed
Compiler: Add addLoopIndex() method
1 parent f274cf6 commit cc2aca9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
5151
CompilerValue *addTargetFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
5252
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
5353
CompilerConstant *addConstValue(const Value &value);
54+
CompilerValue *addLoopIndex();
5455
CompilerValue *addVariableValue(Variable *variable);
5556
CompilerValue *addListContents(List *list);
5657
CompilerValue *addListItem(List *list, CompilerValue *index);

src/dev/engine/compiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ CompilerConstant *Compiler::addConstValue(const Value &value)
115115
return static_cast<CompilerConstant *>(impl->builder->addConstValue(value));
116116
}
117117

118+
/*! Adds the index of the current repeat loop to the compiled code. */
119+
CompilerValue *Compiler::addLoopIndex()
120+
{
121+
return impl->builder->addLoopIndex();
122+
}
123+
118124
/*! Adds the value of the given variable to the code. */
119125
CompilerValue *Compiler::addVariableValue(Variable *variable)
120126
{

test/dev/compiler/compiler_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ TEST_F(CompilerTest, AddConstValue)
161161
compile(compiler, block);
162162
}
163163

164+
TEST_F(CompilerTest, AddLoopIndex)
165+
{
166+
Compiler compiler(&m_engine, &m_target);
167+
auto block = std::make_shared<Block>("a", "");
168+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
169+
CompilerValue ret(Compiler::StaticType::Unknown);
170+
171+
EXPECT_CALL(*m_builder, addLoopIndex()).WillOnce(Return(&ret));
172+
EXPECT_EQ(compiler->addLoopIndex(), &ret);
173+
174+
EXPECT_CALL(*m_builder, addLoopIndex()).WillOnce(Return(nullptr));
175+
EXPECT_EQ(compiler->addLoopIndex(), nullptr);
176+
177+
return nullptr;
178+
});
179+
180+
compile(compiler, block);
181+
}
182+
164183
TEST_F(CompilerTest, AddVariableValue)
165184
{
166185
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)