Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/scratchcpp/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
IfStatement
};

Compiler(IEngine *engine);
Compiler(IEngine *engine, Target *target = nullptr);
Compiler(const Compiler &) = delete;

void init();
Expand All @@ -44,6 +44,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
const std::vector<unsigned int> &bytecode() const;

IEngine *engine() const;
Target *target() const;

const std::vector<InputValue *> &constInputValues() const;
std::vector<Value> constValues() const;
Expand Down
9 changes: 7 additions & 2 deletions src/engine/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ using namespace libscratchcpp;
using namespace vm;

/*! Constructs Compiler. */
Compiler::Compiler(IEngine *engine) :
impl(spimpl::make_unique_impl<CompilerPrivate>(engine))
Compiler::Compiler(IEngine *engine, Target *target) :
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
{
}

Expand Down Expand Up @@ -87,6 +87,11 @@ IEngine *Compiler::engine() const
return impl->engine;
}

Target *Compiler::target() const
{
return impl->target;
}

/*! Returns the list of constant input values. */
const std::vector<InputValue *> &Compiler::constInputValues() const
{
Expand Down
5 changes: 3 additions & 2 deletions src/engine/compiler_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
using namespace libscratchcpp;
using namespace vm;

CompilerPrivate::CompilerPrivate(IEngine *engine) :
engine(engine)
CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
engine(engine),
target(target)
{
assert(engine);
}
Expand Down
5 changes: 3 additions & 2 deletions src/engine/compiler_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace libscratchcpp

struct CompilerPrivate
{
CompilerPrivate(IEngine *engine);
CompilerPrivate(IEngine *engine, Target *target);
CompilerPrivate(const CompilerPrivate &) = delete;

void addInstruction(vm::Opcode opcode, std::initializer_list<unsigned int> args = {});
Expand All @@ -20,7 +20,8 @@ struct CompilerPrivate

void substackEnd();

IEngine *engine;
IEngine *engine = nullptr;
Target *target = nullptr;
std::shared_ptr<Block> block;
std::vector<std::pair<std::pair<std::shared_ptr<Block>, std::shared_ptr<Block>>, Compiler::SubstackType>> substackTree;

Expand Down
2 changes: 1 addition & 1 deletion src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void Engine::compile()
for (auto target : m_targets) {
std::cout << "Compiling scripts in target " << target->name() << "..." << std::endl;
std::unordered_map<std::string, unsigned int *> procedureBytecodeMap;
Compiler compiler(this);
Compiler compiler(this, target.get());
auto blocks = target->blocks();
for (auto block : blocks) {
if (block->topLevel()) {
Expand Down
10 changes: 8 additions & 2 deletions test/compiler/compiler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ class CompilerTest : public testing::Test
TEST_F(CompilerTest, Constructors)
{
Engine engine;
Compiler compiler(&engine);
ASSERT_EQ(compiler.engine(), &engine);
Compiler compiler1(&engine);
ASSERT_EQ(compiler1.engine(), &engine);
ASSERT_EQ(compiler1.target(), nullptr);

Sprite sprite;
Compiler compiler2(&engine, &sprite);
ASSERT_EQ(compiler2.engine(), &engine);
ASSERT_EQ(compiler2.target(), &sprite);
}

TEST_F(CompilerTest, Block)
Expand Down