Skip to content

Commit 92f53a6

Browse files
committed
fix #550: Merge Shadow and NoShadow compiler code
1 parent 58904c1 commit 92f53a6

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/engine/compiler.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,26 @@ void Compiler::addInput(Input *input)
174174
}
175175
switch (input->type()) {
176176
case Input::Type::Shadow:
177+
case Input::Type::NoShadow: {
177178
if (input->pointsToDropdownMenu())
178179
addInstruction(OP_CONST, { impl->constIndex(input->primaryValue(), true, input->selectedMenuItem()) });
179-
else
180-
addInstruction(OP_CONST, { impl->constIndex(input->primaryValue()) });
181-
break;
182-
183-
case Input::Type::NoShadow: {
184-
auto previousBlock = impl->block;
185-
impl->block = input->valueBlock();
186-
assert(impl->block);
187-
if (impl->block->compileFunction())
188-
impl->block->compile(this);
189180
else {
190-
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
191-
addInstruction(OP_NULL);
181+
auto previousBlock = impl->block;
182+
impl->block = input->valueBlock();
183+
184+
if (impl->block) {
185+
if (impl->block->compileFunction())
186+
impl->block->compile(this);
187+
else {
188+
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
189+
addInstruction(OP_NULL);
190+
}
191+
} else
192+
addInstruction(OP_CONST, { impl->constIndex(input->primaryValue()) });
193+
194+
impl->block = previousBlock;
192195
}
193-
impl->block = previousBlock;
196+
194197
break;
195198
}
196199

test/compiler/compiler_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ TEST_F(CompilerTest, AddNoShadowInput)
315315
compiler.addInstruction(vm::OP_PRINT);
316316

317317
Input input2("TEST_INPUT2", Input::Type::NoShadow);
318-
EXPECT_DEATH({ compiler.addInput(&input2); }, "(Assertion)([ ]+)(.+)([ ]+)(failed)");
318+
input2.setPrimaryValue("test");
319+
compiler.addInput(&input2);
319320

320321
Input input3("TEST_INPUT3", Input::Type::NoShadow);
321322
std::shared_ptr<Block> block2 = std::make_shared<Block>("", "test_block2");
@@ -324,7 +325,9 @@ TEST_F(CompilerTest, AddNoShadowInput)
324325
compiler.addInstruction(vm::OP_PRINT);
325326

326327
compiler.addInstruction(vm::OP_HALT);
327-
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CHECKPOINT, vm::OP_NULL, vm::OP_PRINT, vm::OP_NULL, vm::OP_PRINT, vm::OP_HALT }));
328+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CHECKPOINT, vm::OP_NULL, vm::OP_PRINT, vm::OP_CONST, 0, vm::OP_NULL, vm::OP_PRINT, vm::OP_HALT }));
329+
ASSERT_EQ(compiler.constValues().size(), 1);
330+
ASSERT_EQ(compiler.constValues()[0], "test");
328331
}
329332

330333
TEST_F(CompilerTest, ResolveInput)

0 commit comments

Comments
 (0)