Skip to content

Commit 8fbb31c

Browse files
committed
Compiler: Add methods for local variables
1 parent 8b5f06a commit 8fbb31c

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Target;
1616
class ExecutableCode;
1717
class CompilerValue;
1818
class CompilerConstant;
19+
class CompilerLocalVariable;
1920
class Variable;
2021
class List;
2122
class Input;
@@ -52,6 +53,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
5253
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
5354
CompilerConstant *addConstValue(const Value &value);
5455
CompilerValue *addLoopIndex();
56+
CompilerValue *addLocalVariableValue(CompilerLocalVariable *variable);
5557
CompilerValue *addVariableValue(Variable *variable);
5658
CompilerValue *addListContents(List *list);
5759
CompilerValue *addListItem(List *list, CompilerValue *index);
@@ -95,6 +97,9 @@ class LIBSCRATCHCPP_EXPORT Compiler
9597

9698
CompilerValue *createSelect(CompilerValue *cond, CompilerValue *trueValue, CompilerValue *falseValue, Compiler::StaticType valueType);
9799

100+
CompilerLocalVariable *createLocalVariable(Compiler::StaticType type);
101+
void createLocalVariableWrite(CompilerLocalVariable *variable, CompilerValue *value);
102+
98103
void createVariableWrite(Variable *variable, CompilerValue *value);
99104

100105
void createListClear(List *list);

src/dev/engine/compiler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ CompilerValue *Compiler::addLoopIndex()
127127
return impl->builder->addLoopIndex();
128128
}
129129

130+
/*! Adds the value of the given local variable to the code. */
131+
CompilerValue *Compiler::addLocalVariableValue(CompilerLocalVariable *variable)
132+
{
133+
return impl->builder->addLocalVariableValue(variable);
134+
}
135+
130136
/*! Adds the value of the given variable to the code. */
131137
CompilerValue *Compiler::addVariableValue(Variable *variable)
132138
{
@@ -346,6 +352,18 @@ CompilerValue *Compiler::createSelect(CompilerValue *cond, CompilerValue *trueVa
346352
return impl->builder->createSelect(cond, trueValue, falseValue, valueType);
347353
}
348354

355+
/*! Creates a local variable with the given type. */
356+
CompilerLocalVariable *Compiler::createLocalVariable(StaticType type)
357+
{
358+
return impl->builder->createLocalVariable(type);
359+
}
360+
361+
/*! Creates a local variable write operation. */
362+
void Compiler::createLocalVariableWrite(CompilerLocalVariable *variable, CompilerValue *value)
363+
{
364+
impl->builder->createLocalVariableWrite(variable, value);
365+
}
366+
349367
/*! Creates a variable write operation. */
350368
void Compiler::createVariableWrite(Variable *variable, CompilerValue *value)
351369
{

test/dev/compiler/compiler_test.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <scratchcpp/dev/compiler.h>
22
#include <scratchcpp/dev/compilerconstant.h>
3+
#include <scratchcpp/dev/compilerlocalvariable.h>
34
#include <scratchcpp/block.h>
45
#include <scratchcpp/variable.h>
56
#include <scratchcpp/list.h>
@@ -180,6 +181,29 @@ TEST_F(CompilerTest, AddLoopIndex)
180181
compile(compiler, block);
181182
}
182183

184+
TEST_F(CompilerTest, AddLocalVariableValue)
185+
{
186+
Compiler compiler(&m_engine, &m_target);
187+
auto block = std::make_shared<Block>("a", "");
188+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
189+
CompilerValue ret(Compiler::StaticType::Number);
190+
CompilerValue ptr1(Compiler::StaticType::Number);
191+
CompilerValue ptr2(Compiler::StaticType::Bool);
192+
CompilerLocalVariable var1(&ptr1);
193+
CompilerLocalVariable var2(&ptr2);
194+
195+
EXPECT_CALL(*m_builder, addLocalVariableValue(&var1)).WillOnce(Return(&ret));
196+
EXPECT_EQ(compiler->addLocalVariableValue(&var1), &ret);
197+
198+
EXPECT_CALL(*m_builder, addLocalVariableValue(&var2)).WillOnce(Return(nullptr));
199+
EXPECT_EQ(compiler->addLocalVariableValue(&var2), nullptr);
200+
201+
return nullptr;
202+
});
203+
204+
compile(compiler, block);
205+
}
206+
183207
TEST_F(CompilerTest, AddVariableValue)
184208
{
185209
Compiler compiler(&m_engine, &m_target);
@@ -905,6 +929,46 @@ TEST_F(CompilerTest, CreateSelect)
905929
compile(compiler, block);
906930
}
907931

932+
TEST_F(CompilerTest, CreateLocalVariable)
933+
{
934+
Compiler compiler(&m_engine, &m_target);
935+
auto block = std::make_shared<Block>("", "");
936+
937+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
938+
CompilerValue ptr1(Compiler::StaticType::Number);
939+
CompilerLocalVariable var1(&ptr1);
940+
EXPECT_CALL(*m_builder, createLocalVariable(var1.type())).WillOnce(Return(&var1));
941+
EXPECT_EQ(compiler->createLocalVariable(var1.type()), &var1);
942+
943+
CompilerValue ptr2(Compiler::StaticType::Number);
944+
CompilerLocalVariable var2(&ptr2);
945+
EXPECT_CALL(*m_builder, createLocalVariable(var2.type())).WillOnce(Return(&var2));
946+
EXPECT_EQ(compiler->createLocalVariable(var2.type()), &var2);
947+
948+
return nullptr;
949+
});
950+
951+
compile(compiler, block);
952+
}
953+
954+
TEST_F(CompilerTest, CreateLocalVariableWrite)
955+
{
956+
Compiler compiler(&m_engine, &m_target);
957+
auto block = std::make_shared<Block>("", "");
958+
959+
block->setCompileFunction([](Compiler *compiler) -> CompilerValue * {
960+
CompilerValue ptr(Compiler::StaticType::Number);
961+
CompilerLocalVariable var(&ptr);
962+
CompilerValue arg(Compiler::StaticType::Number);
963+
EXPECT_CALL(*m_builder, createLocalVariableWrite(&var, &arg));
964+
compiler->createLocalVariableWrite(&var, &arg);
965+
966+
return nullptr;
967+
});
968+
969+
compile(compiler, block);
970+
}
971+
908972
TEST_F(CompilerTest, CreateVariableWrite)
909973
{
910974
Compiler compiler(&m_engine, &m_target);

0 commit comments

Comments
 (0)