Skip to content

Commit 331fd98

Browse files
authored
Merge pull request #247 from scratchcpp/compiler_target_property
Add target property to Compiler
2 parents 7f4bd5b + 40e7101 commit 331fd98

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

include/scratchcpp/compiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
3434
IfStatement
3535
};
3636

37-
Compiler(IEngine *engine);
37+
Compiler(IEngine *engine, Target *target = nullptr);
3838
Compiler(const Compiler &) = delete;
3939

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

4646
IEngine *engine() const;
47+
Target *target() const;
4748

4849
const std::vector<InputValue *> &constInputValues() const;
4950
std::vector<Value> constValues() const;

src/engine/compiler.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ using namespace libscratchcpp;
1414
using namespace vm;
1515

1616
/*! Constructs Compiler. */
17-
Compiler::Compiler(IEngine *engine) :
18-
impl(spimpl::make_unique_impl<CompilerPrivate>(engine))
17+
Compiler::Compiler(IEngine *engine, Target *target) :
18+
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
1919
{
2020
}
2121

@@ -87,6 +87,11 @@ IEngine *Compiler::engine() const
8787
return impl->engine;
8888
}
8989

90+
Target *Compiler::target() const
91+
{
92+
return impl->target;
93+
}
94+
9095
/*! Returns the list of constant input values. */
9196
const std::vector<InputValue *> &Compiler::constInputValues() const
9297
{

src/engine/compiler_p.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
using namespace libscratchcpp;
88
using namespace vm;
99

10-
CompilerPrivate::CompilerPrivate(IEngine *engine) :
11-
engine(engine)
10+
CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
11+
engine(engine),
12+
target(target)
1213
{
1314
assert(engine);
1415
}

src/engine/compiler_p.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace libscratchcpp
1111

1212
struct CompilerPrivate
1313
{
14-
CompilerPrivate(IEngine *engine);
14+
CompilerPrivate(IEngine *engine, Target *target);
1515
CompilerPrivate(const CompilerPrivate &) = delete;
1616

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

2121
void substackEnd();
2222

23-
IEngine *engine;
23+
IEngine *engine = nullptr;
24+
Target *target = nullptr;
2425
std::shared_ptr<Block> block;
2526
std::vector<std::pair<std::pair<std::shared_ptr<Block>, std::shared_ptr<Block>>, Compiler::SubstackType>> substackTree;
2627

src/engine/internal/engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void Engine::compile()
8484
for (auto target : m_targets) {
8585
std::cout << "Compiling scripts in target " << target->name() << "..." << std::endl;
8686
std::unordered_map<std::string, unsigned int *> procedureBytecodeMap;
87-
Compiler compiler(this);
87+
Compiler compiler(this, target.get());
8888
auto blocks = target->blocks();
8989
for (auto block : blocks) {
9090
if (block->topLevel()) {

test/compiler/compiler_test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ class CompilerTest : public testing::Test
4444
TEST_F(CompilerTest, Constructors)
4545
{
4646
Engine engine;
47-
Compiler compiler(&engine);
48-
ASSERT_EQ(compiler.engine(), &engine);
47+
Compiler compiler1(&engine);
48+
ASSERT_EQ(compiler1.engine(), &engine);
49+
ASSERT_EQ(compiler1.target(), nullptr);
50+
51+
Sprite sprite;
52+
Compiler compiler2(&engine, &sprite);
53+
ASSERT_EQ(compiler2.engine(), &engine);
54+
ASSERT_EQ(compiler2.target(), &sprite);
4955
}
5056

5157
TEST_F(CompilerTest, Block)

0 commit comments

Comments
 (0)