Skip to content

Commit 632ebe5

Browse files
committed
Compiler: Add support for procedures
1 parent 54c0d50 commit 632ebe5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/engine/compiler.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ unsigned int Compiler::constIndex(InputValue *value)
229229
return m_constValues.size() - 1;
230230
}
231231

232+
/*! Returns the index of the procedure code of the given block. */
233+
unsigned int Compiler::procedureIndex(std::string proc)
234+
{
235+
auto it = std::find(m_procedures.begin(), m_procedures.end(), proc);
236+
if (it != m_procedures.end())
237+
return it - m_procedures.begin();
238+
m_procedures.push_back(proc);
239+
return m_procedures.size() - 1;
240+
}
241+
232242
void Compiler::substackEnd()
233243
{
234244
auto parent = m_substackTree.back();
@@ -251,3 +261,21 @@ void Compiler::substackEnd()
251261
if (!m_block && !m_substackTree.empty())
252262
substackEnd();
253263
}
264+
265+
/*! Returns the list of custom block prototypes. */
266+
const std::vector<std::string> &Compiler::procedures() const
267+
{
268+
return m_procedures;
269+
}
270+
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+
277+
/*! Returns the current block. */
278+
const std::shared_ptr<Block> &Compiler::block() const
279+
{
280+
return m_block;
281+
}

src/engine/compiler.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,25 @@ class LIBSCRATCHCPP_EXPORT Compiler
4848
unsigned int variableIndex(std::shared_ptr<IEntity> varEntity);
4949
unsigned int listIndex(std::shared_ptr<IEntity> listEntity);
5050
unsigned int constIndex(InputValue *value);
51+
unsigned int procedureIndex(std::string proc);
52+
53+
const std::vector<std::string> &procedures() const;
54+
void setProcedures(const std::vector<std::string> &newProcedures);
55+
56+
const std::shared_ptr<Block> &block() const;
5157

5258
private:
5359
void substackEnd();
5460

5561
Engine *m_engine;
56-
std::shared_ptr<Block>(m_block);
62+
std::shared_ptr<Block> m_block;
5763
std::vector<std::pair<std::pair<std::shared_ptr<Block>, std::shared_ptr<Block>>, SubstackType>> m_substackTree;
5864

5965
std::vector<unsigned int> m_bytecode;
6066
std::vector<InputValue *> m_constValues;
6167
std::vector<Variable *> m_variables;
6268
std::vector<List *> m_lists;
69+
std::vector<std::string> m_procedures;
6370
};
6471

6572
} // namespace libscratchcpp

0 commit comments

Comments
 (0)