Skip to content

Commit 05b2e78

Browse files
committed
VariableBlocks: Add compile functions
1 parent 9de58ae commit 05b2e78

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/blocks/variableblocks.cpp

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

33
#include "variableblocks.h"
4+
#include "../engine/compiler.h"
45

56
using namespace libscratchcpp;
67

78
VariableBlocks::VariableBlocks()
89
{
910
// Blocks
11+
addCompileFunction("data_setvariableto", &VariableBlocks::compileSetVariable);
12+
addCompileFunction("data_changevariableby", &VariableBlocks::compileChangeVariableBy);
1013
addBlock("data_setvariableto", &VariableBlocks::setVariable);
1114
addBlock("data_changevariableby", &VariableBlocks::changeVariableBy);
1215

@@ -22,6 +25,18 @@ std::string VariableBlocks::name() const
2225
return "Variables";
2326
}
2427

28+
void VariableBlocks::compileSetVariable(Compiler *compiler)
29+
{
30+
compiler->addInput(VALUE);
31+
compiler->addInstruction(vm::OP_SET_VAR, { compiler->variableIndex(compiler->field(VARIABLE)->valuePtr()) });
32+
}
33+
34+
void VariableBlocks::compileChangeVariableBy(Compiler *compiler)
35+
{
36+
compiler->addInput(VALUE);
37+
compiler->addInstruction(vm::OP_CHANGE_VAR, { compiler->variableIndex(compiler->field(VARIABLE)->valuePtr()) });
38+
}
39+
2540
Value VariableBlocks::setVariable(const BlockArgs &args)
2641
{
2742
auto variable = std::static_pointer_cast<Variable>(args.field(VARIABLE)->valuePtr());

src/blocks/variableblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class LIBSCRATCHCPP_EXPORT VariableBlocks : public IBlockSection
2525

2626
std::string name() const override;
2727

28+
static void compileSetVariable(Compiler *compiler);
29+
static void compileChangeVariableBy(Compiler *compiler);
30+
2831
static Value setVariable(const BlockArgs &args);
2932
static Value changeVariableBy(const BlockArgs &args);
3033
};

0 commit comments

Comments
 (0)