Skip to content

Commit e3ad40f

Browse files
committed
Compile scripts to bytecode
1 parent b115f84 commit e3ad40f

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/engine/engine.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../scratchconfiguration.h"
66
#include "../scratch/runningscript.h"
77
#include "iblocksection.h"
8+
#include "compiler.h"
89
#include <cassert>
910
#include <iostream>
1011
#include <thread>
@@ -26,12 +27,12 @@ void Engine::clear()
2627
}
2728

2829
/*!
29-
* Turns ID references into pointers to make projects run faster.\n
30-
* It also resolves all block opcodes and saves them as function pointers.
30+
* Compiles all scripts to bytecode.
31+
* \see Compiler
3132
*/
3233
void Engine::compile()
3334
{
34-
// TODO: Compiling takes lots of time, so compile blocks when they're added by the project loader.
35+
// Resolve entities by ID
3536
for (auto target : m_targets) {
3637
auto blocks = target->blocks();
3738
for (auto block : blocks) {
@@ -66,6 +67,33 @@ void Engine::compile()
6667
block->updateFieldMap();
6768
}
6869
}
70+
71+
// Compile scripts to bytecode
72+
for (auto target : m_targets) {
73+
std::vector<Variable *> variables;
74+
std::vector<List *> lists;
75+
std::vector<InputValue *> constInputValues;
76+
auto blocks = target->blocks();
77+
for (auto block : blocks) {
78+
if (block->topLevel()) {
79+
auto section = blockSection(block->opcode());
80+
Compiler compiler(this, block);
81+
compiler.setConstInputValues(constInputValues);
82+
compiler.setVariables(variables);
83+
compiler.setLists(lists);
84+
compiler.compile();
85+
variables = compiler.variables();
86+
lists = compiler.lists();
87+
auto vm = std::make_shared<VirtualMachine>();
88+
vm->setFunctions(m_functions);
89+
vm->setConstValues(compiler.constValues());
90+
vm->setVariables(compiler.variablePtrs());
91+
vm->setLists(lists);
92+
vm->setBytecode(compiler.bytecode());
93+
m_scripts[block] = vm;
94+
}
95+
}
96+
}
6997
}
7098

7199
/*!

src/engine/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class LIBSCRATCHCPP_EXPORT Engine
7070
std::vector<std::shared_ptr<Broadcast>> m_broadcasts;
7171
std::vector<std::string> m_extensions;
7272
std::vector<std::shared_ptr<RunningScript>> m_runningScripts;
73+
std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<VirtualMachine>> m_scripts;
7374
bool m_breakFrame = false;
7475
bool m_stayOnCurrentBlock = false;
7576
bool m_atomic = true;

src/engine/virtualmachine.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ void VirtualMachine::setConstValues(const std::vector<Value> &values)
4747
m_constValues = m_constValuesVector.data();
4848
}
4949

50+
/*! Sets the list of variables. */
51+
void VirtualMachine::setVariables(const std::vector<Value *> &variables)
52+
{
53+
m_variablesVector = variables;
54+
m_variables = m_variablesVector.data();
55+
}
56+
57+
/*! Sets the list of lists. */
58+
void VirtualMachine::setLists(const std::vector<List *> &lists)
59+
{
60+
m_listsVector = lists;
61+
m_lists = m_listsVector.data();
62+
}
63+
5064
/*! Sets the bytecode of the script. */
5165
void VirtualMachine::setBytecode(const std::vector<unsigned int> &code)
5266
{

src/engine/virtualmachine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
6464

6565
void setFunctions(const std::vector<BlockFunc> &functions);
6666
void setConstValues(const std::vector<Value> &values);
67+
void setVariables(const std::vector<Value *> &variables);
68+
void setLists(const std::vector<List *> &lists);
6769
void setBytecode(const std::vector<unsigned int> &code);
6870

6971
const Value *getInput(unsigned int index, unsigned int argCount) const { return m_regs[m_regCount - argCount + index]; };
@@ -95,8 +97,12 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
9597

9698
Value **m_regs = nullptr;
9799
size_t m_regCount = 0;
100+
98101
Value **m_variables = nullptr;
102+
std::vector<Value *> m_variablesVector;
103+
99104
List **m_lists = nullptr;
105+
std::vector<List *> m_listsVector;
100106
};
101107

102108
} // namespace libscratchcpp

0 commit comments

Comments
 (0)