Skip to content

Commit 9de58ae

Browse files
committed
Compiler: Add inputs by ID
1 parent 6f43234 commit 9de58ae

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/engine/compiler.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ void Compiler::addInstruction(Opcode opcode, std::initializer_list<unsigned int>
103103
m_bytecode.push_back(arg);
104104
}
105105

106-
/*! Compiles the given input and adds it to the bytecode. */
107-
void Compiler::addInput(Input *input)
106+
/*! Compiles the given input (resolved by ID) and adds it to the bytecode. */
107+
void Compiler::addInput(int id)
108108
{
109-
switch (input->type()) {
109+
auto in = input(id);
110+
switch (in->type()) {
110111
case Input::Type::Shadow:
111-
addInstruction(OP_CONST, { constIndex(input->primaryValue()) });
112+
addInstruction(OP_CONST, { constIndex(in->primaryValue()) });
112113
break;
113114

114115
case Input::Type::NoShadow:
@@ -150,6 +151,17 @@ unsigned int Compiler::variableIndex(std::shared_ptr<IEntity> varEntity)
150151
return m_variables.size() - 1;
151152
}
152153

154+
/*! Returns the index of the given list. */
155+
unsigned int Compiler::listIndex(std::shared_ptr<IEntity> listEntity)
156+
{
157+
auto list = dynamic_cast<List *>(listEntity.get());
158+
auto it = std::find(m_lists.begin(), m_lists.end(), list);
159+
if (it != m_lists.end())
160+
return it - m_lists.begin();
161+
m_lists.push_back(list);
162+
return m_lists.size() - 1;
163+
}
164+
153165
unsigned int Compiler::constIndex(InputValue *value)
154166
{
155167
auto it = std::find(m_constValues.begin(), m_constValues.end(), value);

src/engine/compiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ class LIBSCRATCHCPP_EXPORT Compiler
3131
void setLists(std::vector<List *> lists);
3232

3333
void addInstruction(vm::Opcode opcode, std::initializer_list<unsigned int> args = {});
34-
void addInput(Input *input);
34+
void addInput(int id);
3535
void addFunctionCall(BlockFunc f);
3636

3737
Input *input(int id) const;
3838
Field *field(int id) const;
3939
unsigned int variableIndex(std::shared_ptr<IEntity> varEntity);
40+
unsigned int listIndex(std::shared_ptr<IEntity> listEntity);
4041

4142
private:
4243
unsigned int constIndex(InputValue *value);

0 commit comments

Comments
 (0)