Skip to content

Commit 25534cc

Browse files
committed
Compile selected items in dropdown menus
1 parent 3ced3d2 commit 25534cc

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

src/engine/compiler.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,14 @@ const std::vector<InputValue *> &Compiler::constInputValues() const
9898
std::vector<Value> Compiler::constValues() const
9999
{
100100
std::vector<Value> ret;
101-
for (auto value : impl->constValues)
102-
ret.push_back(value->value());
101+
for (auto value : impl->constValues) {
102+
const auto &menuInfo = impl->constValueMenuInfo.at(value);
103+
104+
if (menuInfo.first)
105+
ret.push_back(menuInfo.second);
106+
else
107+
ret.push_back(value->value());
108+
}
103109
return ret;
104110
}
105111

@@ -139,7 +145,10 @@ void Compiler::addInput(Input *input)
139145
}
140146
switch (input->type()) {
141147
case Input::Type::Shadow:
142-
addInstruction(OP_CONST, { constIndex(input->primaryValue()) });
148+
if (input->pointsToDropdownMenu())
149+
addInstruction(OP_CONST, { impl->constIndex(input->primaryValue(), true, input->selectedMenuItem()) });
150+
else
151+
addInstruction(OP_CONST, { impl->constIndex(input->primaryValue()) });
143152
break;
144153

145154
case Input::Type::NoShadow: {
@@ -275,11 +284,7 @@ unsigned int Compiler::listIndex(std::shared_ptr<Entity> listEntity)
275284
/*! Returns the index of the given constant input value. */
276285
unsigned int Compiler::constIndex(InputValue *value)
277286
{
278-
auto it = std::find(impl->constValues.begin(), impl->constValues.end(), value);
279-
if (it != impl->constValues.end())
280-
return it - impl->constValues.begin();
281-
impl->constValues.push_back(value);
282-
return impl->constValues.size() - 1;
287+
return impl->constIndex(value);
283288
}
284289

285290
/*! Returns the index of the procedure code of the given block. */

src/engine/compiler_p.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ void CompilerPrivate::addInstruction(vm::Opcode opcode, std::initializer_list<un
2020
bytecode.push_back(arg);
2121
}
2222

23+
unsigned int CompilerPrivate::constIndex(InputValue *value, bool pointsToDropdownMenu, const std::string &selectedMenuItem)
24+
{
25+
auto it = std::find(constValues.begin(), constValues.end(), value);
26+
if (it != constValues.end())
27+
return it - constValues.begin();
28+
constValues.push_back(value);
29+
constValueMenuInfo[value] = { pointsToDropdownMenu, selectedMenuItem };
30+
return constValues.size() - 1;
31+
}
32+
2333
void CompilerPrivate::substackEnd()
2434
{
2535
auto parent = substackTree.back();

src/engine/compiler_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct CompilerPrivate
1313

1414
void addInstruction(vm::Opcode opcode, std::initializer_list<unsigned int> args = {});
1515

16+
unsigned int constIndex(InputValue *value, bool pointsToDropdownMenu = false, const std::string &selectedMenuItem = "");
17+
1618
void substackEnd();
1719

1820
IEngine *engine;
@@ -23,6 +25,7 @@ struct CompilerPrivate
2325

2426
std::vector<unsigned int> bytecode;
2527
std::vector<InputValue *> constValues;
28+
std::unordered_map<InputValue *, std::pair<bool, std::string>> constValueMenuInfo; // input value, <whether the input points to a dropdown menu, selected menu item>
2629
std::vector<Variable *> variables;
2730
std::vector<List *> lists;
2831
std::vector<std::string> procedures;

test/compiler/compiler_test.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,32 @@ TEST_F(CompilerTest, ResolveInput)
329329
engine.registerSection(m_section);
330330

331331
auto block = std::make_shared<Block>("a", "test_block1");
332-
auto reporter = std::make_shared<Block>("a", "test_reporter");
333332
auto input = std::make_shared<Input>("INPUT1", Input::Type::Shadow);
334333
input->setPrimaryValue("test");
335334
input->setInputId(TestBlockSection::INPUT1);
336-
input->setValueBlock(reporter);
335+
block->addInput(input);
336+
block->updateInputMap();
337+
block->setCompileFunction(&TestBlockSection::compileTestBlock1);
338+
339+
compiler.compile(block);
340+
341+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_PRINT, vm::OP_HALT }));
342+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ "test" }));
343+
}
344+
345+
TEST_F(CompilerTest, ResolveDropdownMenuInput)
346+
{
347+
INIT_COMPILER(engine, compiler);
348+
349+
engine.registerSection(m_section);
350+
351+
auto block = std::make_shared<Block>("a", "test_block1");
352+
auto input = std::make_shared<Input>("INPUT1", Input::Type::Shadow);
353+
auto menu = std::make_shared<Block>("a", "test_menu");
354+
auto optionField = std::make_shared<Field>("OPTION", "test");
355+
input->setInputId(TestBlockSection::INPUT1);
356+
input->setValueBlock(menu);
357+
menu->addField(optionField);
337358
block->addInput(input);
338359
block->updateInputMap();
339360
block->setCompileFunction(&TestBlockSection::compileTestBlock1);

0 commit comments

Comments
 (0)