Skip to content

Commit aec52a0

Browse files
committed
Compiler: Make addInput(Input*) public
1 parent 8341dda commit aec52a0

File tree

3 files changed

+59
-59
lines changed

3 files changed

+59
-59
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
6363
CompilerValue *addListContains(List *list, CompilerValue *item);
6464
CompilerValue *addListSize(List *list);
6565
CompilerValue *addInput(const std::string &name);
66+
CompilerValue *addInput(Input *input);
6667

6768
CompilerValue *createAdd(CompilerValue *operand1, CompilerValue *operand2);
6869
CompilerValue *createSub(CompilerValue *operand1, CompilerValue *operand2);
@@ -137,8 +138,6 @@ class LIBSCRATCHCPP_EXPORT Compiler
137138
static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target);
138139

139140
private:
140-
CompilerValue *addInput(Input *input);
141-
142141
spimpl::unique_impl_ptr<CompilerPrivate> impl;
143142
};
144143

src/dev/engine/compiler.cpp

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,62 @@ CompilerValue *Compiler::addInput(const std::string &name)
182182
return addInput(impl->block->inputAt(impl->block->findInput(name)).get());
183183
}
184184

185+
/*! Compiles the given input and adds it to the compiled code. */
186+
CompilerValue *Compiler::addInput(Input *input)
187+
{
188+
if (!input)
189+
return addConstValue(Value());
190+
191+
switch (input->type()) {
192+
case Input::Type::Shadow:
193+
case Input::Type::NoShadow: {
194+
if (input->pointsToDropdownMenu())
195+
return addConstValue(input->selectedMenuItem());
196+
else {
197+
CompilerValue *ret = nullptr;
198+
auto previousBlock = impl->block;
199+
impl->block = input->valueBlock();
200+
201+
if (impl->block) {
202+
if (impl->block->compileFunction())
203+
ret = impl->block->compile(this);
204+
else {
205+
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
206+
impl->unsupportedBlocks.insert(impl->block->opcode());
207+
ret = addConstValue(Value());
208+
}
209+
} else
210+
ret = addConstValue(input->primaryValue()->value());
211+
212+
impl->block = previousBlock;
213+
return ret;
214+
}
215+
}
216+
217+
case Input::Type::ObscuredShadow: {
218+
CompilerValue *ret = nullptr;
219+
auto previousBlock = impl->block;
220+
impl->block = input->valueBlock();
221+
222+
if (impl->block) {
223+
if (impl->block->compileFunction())
224+
ret = impl->block->compile(this);
225+
else {
226+
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
227+
impl->unsupportedBlocks.insert(impl->block->opcode());
228+
ret = addConstValue(Value());
229+
}
230+
} else
231+
ret = input->primaryValue()->compile(this);
232+
233+
impl->block = previousBlock;
234+
return ret;
235+
}
236+
}
237+
238+
return nullptr;
239+
}
240+
185241
/*! Creates an add instruction. */
186242
CompilerValue *Compiler::createAdd(CompilerValue *operand1, CompilerValue *operand2)
187243
{
@@ -583,58 +639,3 @@ std::shared_ptr<CompilerContext> Compiler::createContext(IEngine *engine, Target
583639
CompilerPrivate::initBuilderFactory();
584640
return CompilerPrivate::builderFactory->createCtx(engine, target);
585641
}
586-
587-
CompilerValue *Compiler::addInput(Input *input)
588-
{
589-
if (!input)
590-
return addConstValue(Value());
591-
592-
switch (input->type()) {
593-
case Input::Type::Shadow:
594-
case Input::Type::NoShadow: {
595-
if (input->pointsToDropdownMenu())
596-
return addConstValue(input->selectedMenuItem());
597-
else {
598-
CompilerValue *ret = nullptr;
599-
auto previousBlock = impl->block;
600-
impl->block = input->valueBlock();
601-
602-
if (impl->block) {
603-
if (impl->block->compileFunction())
604-
ret = impl->block->compile(this);
605-
else {
606-
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
607-
impl->unsupportedBlocks.insert(impl->block->opcode());
608-
ret = addConstValue(Value());
609-
}
610-
} else
611-
ret = addConstValue(input->primaryValue()->value());
612-
613-
impl->block = previousBlock;
614-
return ret;
615-
}
616-
}
617-
618-
case Input::Type::ObscuredShadow: {
619-
CompilerValue *ret = nullptr;
620-
auto previousBlock = impl->block;
621-
impl->block = input->valueBlock();
622-
623-
if (impl->block) {
624-
if (impl->block->compileFunction())
625-
ret = impl->block->compile(this);
626-
else {
627-
std::cout << "warning: unsupported reporter block: " << impl->block->opcode() << std::endl;
628-
impl->unsupportedBlocks.insert(impl->block->opcode());
629-
ret = addConstValue(Value());
630-
}
631-
} else
632-
ret = input->primaryValue()->compile(this);
633-
634-
impl->block = previousBlock;
635-
return ret;
636-
}
637-
}
638-
639-
return nullptr;
640-
}

test/dev/compiler/compiler_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ TEST_F(CompilerTest, AddInput)
418418
EXPECT_EQ(compiler->addInput("OBSCURED_SHADOW"), &constRet);
419419

420420
EXPECT_CALL(*m_builder, addVariableValue(m_testVar.get())).WillOnce(Return(&ret));
421-
EXPECT_EQ(compiler->addInput("OBSCURED_SHADOW_VARIABLE"), &ret);
421+
EXPECT_EQ(compiler->addInput(compiler->input("OBSCURED_SHADOW_VARIABLE")), &ret);
422422

423423
EXPECT_CALL(*m_builder, addConstValue(Value("value block"))).WillOnce(Return(&constRet));
424-
EXPECT_EQ(compiler->addInput("OBSCURED_SHADOW_BLOCK"), &constRet);
424+
EXPECT_EQ(compiler->addInput(compiler->input("OBSCURED_SHADOW_BLOCK")), &constRet);
425425

426426
return nullptr;
427427
});

0 commit comments

Comments
 (0)