Skip to content

Commit ec325a3

Browse files
committed
Compiler: Implement if statements
1 parent 51e5450 commit ec325a3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/dev/engine/compiler.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ std::shared_ptr<libscratchcpp::Block> Compiler::block() const
3939
std::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. */
101106
void 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. */
111118
void 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. */
121133
void 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

src/dev/engine/compiler_p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
1919

2020
void CompilerPrivate::substackEnd()
2121
{
22-
auto parent = substackTree.back();
22+
auto &parent = substackTree.back();
2323

2424
switch (parent.second) {
2525
case SubstackType::Loop:

src/dev/engine/compiler_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct CompilerPrivate
3232

3333
std::shared_ptr<Block> block;
3434
std::vector<std::pair<std::pair<std::shared_ptr<Block>, std::shared_ptr<Block>>, SubstackType>> substackTree;
35+
bool substackHit = false;
3536
bool warp = false;
3637

3738
static inline ICodeBuilderFactory *builderFactory = nullptr;

0 commit comments

Comments
 (0)