Skip to content

Commit a573ce6

Browse files
authored
Merge pull request #137 from scratchcpp/compiler_test
Complete Compiler test
2 parents ec7848c + 37640d7 commit a573ce6

File tree

11 files changed

+349
-40
lines changed

11 files changed

+349
-40
lines changed

include/scratchcpp/compiler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ class LIBSCRATCHCPP_EXPORT Compiler
5151

5252
const std::vector<List *> &lists() const;
5353

54-
void addInstruction(vm::Opcode opcode, std::initializer_list<unsigned int> args = {});
54+
void addInstruction(vm::Opcode opcode, const std::initializer_list<unsigned int> &args = {});
5555
void addInput(Input *input);
5656
void addInput(int id);
5757
void addFunctionCall(BlockFunc f);
58-
void addProcedureArg(std::string procCode, std::string argName);
58+
void addProcedureArg(const std::string &procCode, const std::string &argName);
5959
void moveToSubstack(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2, SubstackType type);
6060
void moveToSubstack(std::shared_ptr<Block> substack, SubstackType type);
6161
void breakAtomicScript();
@@ -67,8 +67,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
6767
unsigned int variableIndex(std::shared_ptr<Entity> varEntity);
6868
unsigned int listIndex(std::shared_ptr<Entity> listEntity);
6969
unsigned int constIndex(InputValue *value);
70-
unsigned int procedureIndex(std::string proc);
71-
long procedureArgIndex(std::string procCode, std::string argName);
70+
unsigned int procedureIndex(const std::string &proc);
71+
long procedureArgIndex(const std::string &procCode, const std::string &argName);
7272

7373
const std::vector<std::string> &procedures() const;
7474

src/engine/compiler.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const std::vector<List *> &Compiler::lists() const
103103
}
104104

105105
/*! Adds an instruction to the bytecode. */
106-
void Compiler::addInstruction(Opcode opcode, std::initializer_list<unsigned int> args)
106+
void Compiler::addInstruction(Opcode opcode, const std::initializer_list<unsigned int> &args)
107107
{
108108
impl->addInstruction(opcode, args);
109109
}
@@ -165,8 +165,15 @@ void Compiler::addFunctionCall(BlockFunc f)
165165
}
166166

167167
/*! Adds an argument to a procedure (custom block). */
168-
void Compiler::addProcedureArg(std::string procCode, std::string argName)
168+
void Compiler::addProcedureArg(const std::string &procCode, const std::string &argName)
169169
{
170+
if (impl->procedureArgs.find(procCode) != impl->procedureArgs.cend()) {
171+
const auto &procedure = impl->procedureArgs[procCode];
172+
173+
if (std::find(procedure.begin(), procedure.end(), argName) != procedure.end())
174+
return;
175+
}
176+
170177
impl->procedureArgs[procCode].push_back(argName);
171178
}
172179

@@ -254,7 +261,7 @@ unsigned int Compiler::constIndex(InputValue *value)
254261
}
255262

256263
/*! Returns the index of the procedure code of the given block. */
257-
unsigned int Compiler::procedureIndex(std::string proc)
264+
unsigned int Compiler::procedureIndex(const std::string &proc)
258265
{
259266
auto it = std::find(impl->procedures.begin(), impl->procedures.end(), proc);
260267
if (it != impl->procedures.end())
@@ -264,7 +271,7 @@ unsigned int Compiler::procedureIndex(std::string proc)
264271
}
265272

266273
/*! Returns the index of the argument of the given procedure (custom block). */
267-
long Compiler::procedureArgIndex(std::string procCode, std::string argName)
274+
long Compiler::procedureArgIndex(const std::string &procCode, const std::string &argName)
268275
{
269276
if (impl->procedureArgs.count(procCode) == 0) {
270277
std::cout << "warning: could not find custom block '" << procCode << "'" << std::endl;

src/engine/compiler_p.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ void CompilerPrivate::substackEnd()
4242
addInstruction(OP_ENDIF);
4343
break;
4444
}
45-
block = parent.first.first->next();
45+
46+
auto parentBlock = parent.first.first;
47+
48+
if (parentBlock)
49+
block = parentBlock->next();
50+
else
51+
block = nullptr;
52+
4653
substackTree.pop_back();
4754
if (!block && !substackTree.empty())
4855
substackEnd();

test/compiler/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
add_executable(
22
compiler_test
33
compiler_test.cpp
4+
testblocksection.cpp
5+
testblocksection.h
46
)
57

68
target_link_libraries(

0 commit comments

Comments
 (0)