|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include "compiler.h" |
| 4 | +#include "engine.h" |
| 5 | + |
| 6 | +using namespace libscratchcpp; |
| 7 | +using namespace vm; |
| 8 | + |
| 9 | +/*! Constructs Compiler. */ |
| 10 | +Compiler::Compiler(Engine *engine, std::shared_ptr<Block> topLevelBlock) : |
| 11 | + m_engine(engine), |
| 12 | + m_block(topLevelBlock) |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +/*! Compiles the script. Use bytecode() to read the generated bytecode. */ |
| 17 | +void Compiler::compile() |
| 18 | +{ |
| 19 | + // Add start instruction |
| 20 | + addInstruction(OP_START); |
| 21 | + |
| 22 | + m_block = m_block->next(); |
| 23 | + while (m_block) { |
| 24 | + if (m_block->compileFunction()) |
| 25 | + m_block->compile(this); |
| 26 | + else |
| 27 | + std::cout << "warning: unsupported block: " << m_block->opcode() << std::endl; |
| 28 | + m_block = m_block->next(); |
| 29 | + } |
| 30 | + |
| 31 | + // Add end instruction (halt) |
| 32 | + addInstruction(OP_HALT); |
| 33 | +} |
| 34 | + |
| 35 | +/*! Returns the generated bytecode. */ |
| 36 | +const std::vector<unsigned int> &Compiler::bytecode() const |
| 37 | +{ |
| 38 | + return m_bytecode; |
| 39 | +} |
| 40 | + |
| 41 | +/*! Returns the list of constant input values. */ |
| 42 | +const std::vector<InputValue *> &Compiler::constInputValues() const |
| 43 | +{ |
| 44 | + return m_constValues; |
| 45 | +} |
| 46 | + |
| 47 | +/*! Returns the list of constant values. */ |
| 48 | +std::vector<Value> Compiler::constValues() const |
| 49 | +{ |
| 50 | + std::vector<Value> ret; |
| 51 | + for (auto value : m_constValues) |
| 52 | + ret.push_back(value->value()); |
| 53 | + return ret; |
| 54 | +} |
| 55 | + |
| 56 | +/*! Sets the list of constant input values. */ |
| 57 | +void Compiler::setConstInputValues(const std::vector<InputValue *> &values) |
| 58 | +{ |
| 59 | + m_constValues = values; |
| 60 | +} |
| 61 | + |
| 62 | +/*! Returns the list of variables. */ |
| 63 | +const std::vector<Variable *> &Compiler::variables() const |
| 64 | +{ |
| 65 | + return m_variables; |
| 66 | +} |
| 67 | + |
| 68 | +/*! Returns the list of pointers to variable values. */ |
| 69 | +std::vector<Value *> Compiler::variablePtrs() const |
| 70 | +{ |
| 71 | + std::vector<Value *> ret; |
| 72 | + for (auto var : m_variables) |
| 73 | + ret.push_back(var->valuePtr()); |
| 74 | + return ret; |
| 75 | +} |
| 76 | + |
| 77 | +/*! Sets the list of variables. */ |
| 78 | +void Compiler::setVariables(std::vector<Variable *> variables) |
| 79 | +{ |
| 80 | + m_variables = variables; |
| 81 | +} |
| 82 | + |
| 83 | +/*! Returns the list of lists. */ |
| 84 | +const std::vector<List *> &Compiler::lists() const |
| 85 | +{ |
| 86 | + return m_lists; |
| 87 | +} |
| 88 | + |
| 89 | +/*! Sets the list of lists. */ |
| 90 | +void Compiler::setLists(std::vector<List *> lists) |
| 91 | +{ |
| 92 | + m_lists = lists; |
| 93 | +} |
| 94 | + |
| 95 | +/*! Adds an instruction to the bytecode. */ |
| 96 | +void Compiler::addInstruction(Opcode opcode, std::initializer_list<unsigned int> args) |
| 97 | +{ |
| 98 | + m_bytecode.push_back(opcode); |
| 99 | + for (auto arg : args) |
| 100 | + m_bytecode.push_back(arg); |
| 101 | +} |
| 102 | + |
| 103 | +/*! Compiles the given input and adds it to the bytecode. */ |
| 104 | +void Compiler::addInput(Input *input) |
| 105 | +{ |
| 106 | + switch (input->type()) { |
| 107 | + case Input::Type::Shadow: |
| 108 | + addInstruction(OP_CONST, { constIndex(input->primaryValue()) }); |
| 109 | + break; |
| 110 | + |
| 111 | + case Input::Type::NoShadow: |
| 112 | + addInstruction(OP_NULL); |
| 113 | + break; |
| 114 | + |
| 115 | + case Input::Type::ObscuredShadow: |
| 116 | + // TODO |
| 117 | + break; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/*! Returns the input with the given ID. */ |
| 122 | +Input *Compiler::input(int id) const |
| 123 | +{ |
| 124 | + return m_block->findInputById(id); |
| 125 | +} |
| 126 | + |
| 127 | +/*! Returns the field with the given ID. */ |
| 128 | +Field *Compiler::field(int id) const |
| 129 | +{ |
| 130 | + return m_block->findFieldById(id); |
| 131 | +} |
| 132 | + |
| 133 | +/*! Returns the index of the given variable. */ |
| 134 | +unsigned int Compiler::variableIndex(std::shared_ptr<IEntity> varEntity) |
| 135 | +{ |
| 136 | + auto var = dynamic_cast<Variable *>(varEntity.get()); |
| 137 | + auto it = std::find(m_variables.begin(), m_variables.end(), var); |
| 138 | + if (it != m_variables.end()) |
| 139 | + return it - m_variables.begin(); |
| 140 | + m_variables.push_back(var); |
| 141 | + return m_variables.size() - 1; |
| 142 | +} |
| 143 | + |
| 144 | +unsigned int Compiler::constIndex(InputValue *value) |
| 145 | +{ |
| 146 | + auto it = std::find(m_constValues.begin(), m_constValues.end(), value); |
| 147 | + if (it != m_constValues.end()) |
| 148 | + return it - m_constValues.begin(); |
| 149 | + m_constValues.push_back(value); |
| 150 | + return m_constValues.size() - 1; |
| 151 | +} |
0 commit comments