Skip to content

Commit a74d0bd

Browse files
committed
Compile obscured inputs
1 parent 3b84a15 commit a74d0bd

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

src/engine/compiler.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,31 @@ void Compiler::addInput(int id)
126126
addInstruction(OP_CONST, { constIndex(in->primaryValue()) });
127127
break;
128128

129-
case Input::Type::NoShadow:
130-
addInstruction(OP_NULL);
129+
case Input::Type::NoShadow: {
130+
auto previousBlock = m_block;
131+
m_block = in->valueBlock();
132+
assert(m_block);
133+
if (m_block->compileFunction())
134+
m_block->compile(this);
135+
else
136+
std::cout << "warning: unsupported reporter block: " << m_block->opcode() << std::endl;
137+
m_block = previousBlock;
131138
break;
132-
133-
case Input::Type::ObscuredShadow:
134-
// TODO
139+
}
140+
141+
case Input::Type::ObscuredShadow: {
142+
auto previousBlock = m_block;
143+
m_block = in->valueBlock();
144+
if (m_block) {
145+
if (m_block->compileFunction())
146+
m_block->compile(this);
147+
else
148+
std::cout << "warning: unsupported reporter block: " << m_block->opcode() << std::endl;
149+
} else
150+
in->primaryValue()->compile(this);
151+
m_block = previousBlock;
135152
break;
153+
}
136154
}
137155
}
138156

src/engine/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class LIBSCRATCHCPP_EXPORT Compiler
4747
std::shared_ptr<Block> inputBlock(int id) const;
4848
unsigned int variableIndex(std::shared_ptr<IEntity> varEntity);
4949
unsigned int listIndex(std::shared_ptr<IEntity> listEntity);
50+
unsigned int constIndex(InputValue *value);
5051

5152
private:
52-
unsigned int constIndex(InputValue *value);
5353
void substackEnd();
5454

5555
Engine *m_engine;

src/scratch/inputvalue.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include "inputvalue.h"
4+
#include "../engine/compiler.h"
45
#include <map>
56

67
using namespace libscratchcpp;
@@ -22,6 +23,32 @@ InputValue::InputValue(Type type) :
2223
{
2324
}
2425

26+
/*! Compiles the input value. */
27+
void InputValue::compile(Compiler *compiler)
28+
{
29+
switch (m_type) {
30+
case Type::Color:
31+
// TODO: Add support for colors
32+
break;
33+
34+
case Type::Broadcast:
35+
// TODO: Add support for broadcasts
36+
break;
37+
38+
case Type::Variable:
39+
compiler->addInstruction(vm::OP_READ_VAR, { compiler->variableIndex(m_valuePtr) });
40+
break;
41+
42+
case Type::List:
43+
compiler->addInstruction(vm::OP_READ_LIST, { compiler->listIndex(m_valuePtr) });
44+
break;
45+
46+
default:
47+
compiler->addInstruction(vm::OP_CONST, { compiler->constIndex(this) });
48+
break;
49+
}
50+
}
51+
2552
/*! Returns the type of the value. */
2653
InputValue::Type InputValue::type() const
2754
{

src/scratch/inputvalue.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace libscratchcpp
1010
{
1111

1212
class LIBSCRATCHCPP_EXPORT Block;
13+
class LIBSCRATCHCPP_EXPORT Compiler;
1314

1415
/*! \brief The InputValue class provides methods for the value of an Input. */
1516
class LIBSCRATCHCPP_EXPORT InputValue
@@ -32,6 +33,8 @@ class LIBSCRATCHCPP_EXPORT InputValue
3233
InputValue();
3334
InputValue(Type type);
3435

36+
void compile(Compiler *compiler);
37+
3538
Type type() const;
3639
void setType(Type newType);
3740

0 commit comments

Comments
 (0)