Skip to content

Commit 22cf854

Browse files
committed
Implement data_variable block
1 parent 0fcabea commit 22cf854

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/blocks/variableblocks.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ std::string VariableBlocks::name() const
1616
void VariableBlocks::registerBlocks(IEngine *engine)
1717
{
1818
// Blocks
19+
engine->addCompileFunction(this, "data_variable", &compileVariable);
1920
engine->addCompileFunction(this, "data_setvariableto", &compileSetVariable);
2021
engine->addCompileFunction(this, "data_changevariableby", &compileChangeVariableBy);
2122

@@ -26,6 +27,12 @@ void VariableBlocks::registerBlocks(IEngine *engine)
2627
engine->addField(this, "VARIABLE", VARIABLE);
2728
}
2829

30+
void VariableBlocks::compileVariable(Compiler *compiler)
31+
{
32+
// NOTE: This block is only used by variable monitors
33+
compiler->addInstruction(vm::OP_READ_VAR, { compiler->variableIndex(compiler->field(VARIABLE)->valuePtr()) });
34+
}
35+
2936
void VariableBlocks::compileSetVariable(Compiler *compiler)
3037
{
3138
compiler->addInput(VALUE);

src/blocks/variableblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class VariableBlocks : public IBlockSection
2727

2828
void registerBlocks(IEngine *engine) override;
2929

30+
static void compileVariable(Compiler *compiler);
3031
static void compileSetVariable(Compiler *compiler);
3132
static void compileChangeVariableBy(Compiler *compiler);
3233
};

test/blocks/variable_blocks_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ class VariableBlocksTest : public testing::Test
3737
return block;
3838
}
3939

40+
// For read variable
41+
std::shared_ptr<Block> createVariableBlock(const std::string &id, const std::string &opcode, std::shared_ptr<Variable> variable) const
42+
{
43+
auto block = std::make_shared<Block>(id, opcode);
44+
45+
auto variableField = std::make_shared<Field>("VARIABLE", Value(), variable);
46+
variableField->setFieldId(VariableBlocks::VARIABLE);
47+
block->addField(variableField);
48+
49+
return block;
50+
}
51+
4052
std::unique_ptr<IBlockSection> m_section;
4153
EngineMock m_engineMock;
4254
Engine m_engine;
@@ -55,6 +67,7 @@ TEST_F(VariableBlocksTest, CategoryVisible)
5567
TEST_F(VariableBlocksTest, RegisterBlocks)
5668
{
5769
// Blocks
70+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_variable", &VariableBlocks::compileVariable)).Times(1);
5871
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_setvariableto", &VariableBlocks::compileSetVariable)).Times(1);
5972
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_changevariableby", &VariableBlocks::compileChangeVariableBy)).Times(1);
6073

@@ -67,6 +80,36 @@ TEST_F(VariableBlocksTest, RegisterBlocks)
6780
m_section->registerBlocks(&m_engineMock);
6881
}
6982

83+
TEST_F(VariableBlocksTest, Variable)
84+
{
85+
Compiler compiler(&m_engine);
86+
87+
// [var1]
88+
auto var1 = std::make_shared<Variable>("b", "var1");
89+
auto block1 = createVariableBlock("a", "data_variable", var1);
90+
91+
// [var2]
92+
auto var2 = std::make_shared<Variable>("d", "var2");
93+
auto block2 = createVariableBlock("c", "data_variable", var2);
94+
95+
compiler.init();
96+
compiler.setBlock(block1);
97+
VariableBlocks::compileVariable(&compiler);
98+
compiler.setBlock(block2);
99+
VariableBlocks::compileVariable(&compiler);
100+
compiler.end();
101+
102+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_READ_VAR, 0, vm::OP_READ_VAR, 1, vm::OP_HALT }));
103+
ASSERT_TRUE(compiler.constValues().empty());
104+
ASSERT_EQ(
105+
compiler.variables(),
106+
std::vector<Variable *>({
107+
var1.get(),
108+
var2.get(),
109+
}));
110+
ASSERT_TRUE(compiler.lists().empty());
111+
}
112+
70113
TEST_F(VariableBlocksTest, SetVariableTo)
71114
{
72115
Compiler compiler(&m_engine);

0 commit comments

Comments
 (0)