Skip to content

Commit 7b83d47

Browse files
committed
Use Compiler for whole targets
1 parent 9298cf0 commit 7b83d47

File tree

3 files changed

+11
-51
lines changed

3 files changed

+11
-51
lines changed

src/engine/compiler.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ using namespace libscratchcpp;
99
using namespace vm;
1010

1111
/*! Constructs Compiler. */
12-
Compiler::Compiler(Engine *engine, std::shared_ptr<Block> topLevelBlock) :
13-
m_engine(engine),
14-
m_block(topLevelBlock)
12+
Compiler::Compiler(Engine *engine) :
13+
m_engine(engine)
1514
{
1615
assert(engine);
17-
assert(topLevelBlock);
1816
}
1917

2018
/*! Compiles the script. Use bytecode() to read the generated bytecode. */
21-
void Compiler::compile()
19+
void Compiler::compile(std::shared_ptr<Block> topLevelBlock)
2220
{
2321
// Add start instruction
2422
addInstruction(OP_START);
2523

24+
m_block = topLevelBlock;
2625
while (m_block) {
2726
size_t substacks = m_substackTree.size();
2827

@@ -66,12 +65,6 @@ std::vector<Value> Compiler::constValues() const
6665
return ret;
6766
}
6867

69-
/*! Sets the list of constant input values. */
70-
void Compiler::setConstInputValues(const std::vector<InputValue *> &values)
71-
{
72-
m_constValues = values;
73-
}
74-
7568
/*! Returns the list of variables. */
7669
const std::vector<Variable *> &Compiler::variables() const
7770
{
@@ -87,24 +80,12 @@ std::vector<Value *> Compiler::variablePtrs() const
8780
return ret;
8881
}
8982

90-
/*! Sets the list of variables. */
91-
void Compiler::setVariables(std::vector<Variable *> variables)
92-
{
93-
m_variables = variables;
94-
}
95-
9683
/*! Returns the list of lists. */
9784
const std::vector<List *> &Compiler::lists() const
9885
{
9986
return m_lists;
10087
}
10188

102-
/*! Sets the list of lists. */
103-
void Compiler::setLists(std::vector<List *> lists)
104-
{
105-
m_lists = lists;
106-
}
107-
10889
/*! Adds an instruction to the bytecode. */
10990
void Compiler::addInstruction(Opcode opcode, std::initializer_list<unsigned int> args)
11091
{
@@ -268,12 +249,6 @@ const std::vector<std::string> &Compiler::procedures() const
268249
return m_procedures;
269250
}
270251

271-
/*! Sets the list of custom block prototypes. */
272-
void Compiler::setProcedures(const std::vector<std::string> &newProcedures)
273-
{
274-
m_procedures = newProcedures;
275-
}
276-
277252
/*! Returns the current block. */
278253
const std::shared_ptr<Block> &Compiler::block() const
279254
{

src/engine/compiler.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace libscratchcpp
99
{
1010

11-
/*! \brief The Compiler class provides an API for compiling Scratch scripts to bytecode. */
11+
/*! \brief The Compiler class provides an API for compiling scripts of targets to bytecode. */
1212
class LIBSCRATCHCPP_EXPORT Compiler
1313
{
1414
public:
@@ -18,23 +18,20 @@ class LIBSCRATCHCPP_EXPORT Compiler
1818
IfStatement
1919
};
2020

21-
Compiler(Engine *engine, std::shared_ptr<Block> topLevelBlock);
21+
Compiler(Engine *engine);
2222
Compiler(const Compiler &) = delete;
2323

24-
void compile();
24+
void compile(std::shared_ptr<Block> topLevelBlock);
2525

2626
const std::vector<unsigned int> &bytecode() const;
2727

2828
const std::vector<InputValue *> &constInputValues() const;
2929
std::vector<Value> constValues() const;
30-
void setConstInputValues(const std::vector<InputValue *> &values);
3130

3231
const std::vector<Variable *> &variables() const;
3332
std::vector<Value *> variablePtrs() const;
34-
void setVariables(std::vector<Variable *> variables);
3533

3634
const std::vector<List *> &lists() const;
37-
void setLists(std::vector<List *> lists);
3835

3936
void addInstruction(vm::Opcode opcode, std::initializer_list<unsigned int> args = {});
4037
void addInput(int id);
@@ -51,7 +48,6 @@ class LIBSCRATCHCPP_EXPORT Compiler
5148
unsigned int procedureIndex(std::string proc);
5249

5350
const std::vector<std::string> &procedures() const;
54-
void setProcedures(const std::vector<std::string> &newProcedures);
5551

5652
const std::shared_ptr<Block> &block() const;
5753

src/engine/engine.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,20 @@ void Engine::compile()
6969
// Compile scripts to bytecode
7070
for (auto target : m_targets) {
7171
std::cout << "Compiling scripts in target " << target->name() << "..." << std::endl;
72-
std::vector<Variable *> variables;
73-
std::vector<List *> lists;
74-
std::vector<InputValue *> constInputValues;
75-
std::vector<std::string> procedures;
7672
std::unordered_map<std::string, unsigned int *> procedureBytecodeMap;
73+
Compiler compiler(this);
7774
auto blocks = target->blocks();
7875
for (auto block : blocks) {
7976
if (block->topLevel()) {
8077
auto section = blockSection(block->opcode());
8178
if (section) {
82-
Compiler compiler(this, block);
83-
compiler.setConstInputValues(constInputValues);
84-
compiler.setVariables(variables);
85-
compiler.setLists(lists);
86-
compiler.setProcedures(procedures);
87-
compiler.compile();
88-
variables = compiler.variables();
89-
lists = compiler.lists();
90-
constInputValues = compiler.constInputValues();
91-
procedures = compiler.procedures();
79+
compiler.compile(block);
9280

9381
auto vm = std::make_shared<VirtualMachine>(target.get(), this);
9482
vm->setFunctions(m_functions);
9583
vm->setConstValues(compiler.constValues());
9684
vm->setVariables(compiler.variablePtrs());
97-
vm->setLists(lists);
85+
vm->setLists(compiler.lists());
9886
vm->setBytecode(compiler.bytecode());
9987
if (block->opcode() == "procedures_definition") {
10088
auto b = block->inputAt(block->findInput("custom_block"))->valueBlock();
@@ -106,6 +94,7 @@ void Engine::compile()
10694
}
10795
}
10896

97+
const std::vector<std::string> &procedures = compiler.procedures();
10998
std::vector<unsigned int *> procedureBytecodes;
11099
for (const std::string &code : procedures)
111100
procedureBytecodes.push_back(procedureBytecodeMap[code]);

0 commit comments

Comments
 (0)