Skip to content

Commit 39614bc

Browse files
committed
Compiler: Add breakAtomicScript method
1 parent 33029a8 commit 39614bc

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/engine/compiler.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void Compiler::compile(std::shared_ptr<Block> topLevelBlock)
2020
{
2121
m_bytecode.clear();
2222
m_procedurePrototype = nullptr;
23+
m_atomic = true;
2324

2425
// Add start instruction
2526
addInstruction(OP_START);
@@ -180,6 +181,15 @@ void Compiler::moveToSubstack(std::shared_ptr<Block> substack, SubstackType type
180181
moveToSubstack(substack, nullptr, type);
181182
}
182183

184+
/*!
185+
* Adds the vm::OP_BREAK_ATOMIC instruction at the end of the current loop.
186+
* This can be used for example in motion blocks.
187+
*/
188+
void Compiler::breakAtomicScript()
189+
{
190+
m_atomic = false;
191+
}
192+
183193
/*! Returns the input with the given ID. */
184194
Input *Compiler::input(int id) const
185195
{
@@ -260,6 +270,10 @@ void Compiler::substackEnd()
260270
auto parent = m_substackTree.back();
261271
switch (parent.second) {
262272
case SubstackType::Loop:
273+
if (!m_atomic) {
274+
addInstruction(OP_BREAK_ATOMIC);
275+
m_atomic = true;
276+
}
263277
addInstruction(OP_LOOP_END);
264278
break;
265279
case SubstackType::IfStatement:

src/engine/compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
4242
void addProcedureArg(std::string procCode, std::string argName);
4343
void moveToSubstack(std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2, SubstackType type);
4444
void moveToSubstack(std::shared_ptr<Block> substack, SubstackType type);
45+
void breakAtomicScript();
4546

4647
Input *input(int id) const;
4748
Field *field(int id) const;
@@ -73,6 +74,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
7374
std::vector<std::string> m_procedures;
7475
std::unordered_map<std::string, std::vector<std::string>> m_procedureArgs;
7576
BlockPrototype *m_procedurePrototype = nullptr;
77+
bool m_atomic;
7678
};
7779

7880
} // namespace libscratchcpp

0 commit comments

Comments
 (0)