@@ -39,6 +39,8 @@ std::shared_ptr<libscratchcpp::Block> Compiler::block() const
3939std::shared_ptr<ExecutableCode> Compiler::compile (std::shared_ptr<Block> startBlock)
4040{
4141 impl->builder = impl->builderFactory ->create (startBlock->id ());
42+ impl->substackTree .clear ();
43+ impl->substackHit = false ;
4244 impl->warp = false ;
4345 impl->block = startBlock;
4446
@@ -52,10 +54,13 @@ std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBl
5254 impl->unsupportedBlocks .insert (impl->block ->opcode ());
5355 }
5456
55- if (substacks != impl->substackTree .size ())
57+ if (impl->substackHit ) {
58+ impl->substackHit = false ;
5659 continue ;
60+ }
5761
58- impl->block = impl->block ->next ();
62+ if (impl->block )
63+ impl->block = impl->block ->next ();
5964
6065 if (!impl->block && !impl->substackTree .empty ())
6166 impl->substackEnd ();
@@ -100,18 +105,25 @@ void Compiler::addInput(const std::string &name)
100105/* ! Jumps to the given if substack. */
101106void Compiler::moveToIf (std::shared_ptr<Block> substack)
102107{
108+ if (!substack)
109+ return ; // ignore empty if statements
110+
111+ impl->substackHit = true ;
103112 impl->substackTree .push_back ({ { impl->block , nullptr }, CompilerPrivate::SubstackType::IfStatement });
104113 impl->block = substack;
105-
106- if (!impl->block )
107- impl->substackEnd ();
114+ impl->builder ->beginIfStatement ();
108115}
109116
110117/* ! Jumps to the given if/else substack. The second substack is used for the else branch. */
111118void Compiler::moveToIfElse (std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2)
112119{
120+ if (!substack1 && !substack2)
121+ return ; // ignore empty if statements
122+
123+ impl->substackHit = true ;
113124 impl->substackTree .push_back ({ { impl->block , substack2 }, CompilerPrivate::SubstackType::IfStatement });
114125 impl->block = substack1;
126+ impl->builder ->beginIfStatement ();
115127
116128 if (!impl->block )
117129 impl->substackEnd ();
@@ -120,6 +132,7 @@ void Compiler::moveToIfElse(std::shared_ptr<Block> substack1, std::shared_ptr<Bl
120132/* ! Jumps to the given loop substack. */
121133void Compiler::moveToLoop (std::shared_ptr<Block> substack)
122134{
135+ impl->substackHit = true ;
123136 impl->substackTree .push_back ({ { impl->block , nullptr }, CompilerPrivate::SubstackType::Loop });
124137 impl->block = substack;
125138
0 commit comments