Skip to content

Commit a57075c

Browse files
committed
Add hatPredicateCompileFunction property to Block
1 parent 746f508 commit a57075c

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

include/scratchcpp/block.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
7070
BlockComp compileFunction() const;
7171
void setCompileFunction(BlockComp newCompileFunction);
7272

73+
BlockComp hatPredicateCompileFunction() const;
74+
void setHatPredicateCompileFunction(HatPredicateCompileFunc newHatPredicateCompileFunction);
75+
7376
bool mutationHasNext() const;
7477
void setMutationHasNext(bool newMutationHasNext);
7578

include/scratchcpp/global.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ using MonitorNameFunc = const std::string &(*)(Block *);
6262
*/
6363
using MonitorChangeFunc = void (*)(Block *, const Value &newValue);
6464

65+
/*!
66+
* \typedef HatPredicateCompileFunc
67+
*
68+
* HatPredicateCompileFunc is a function pointer for functions which are used to compile edge-activated hat predicates to bytecode.
69+
*/
70+
using HatPredicateCompileFunc = void (*)(Compiler *vm);
71+
6572
} // namespace libscratchcpp
6673

6774
#endif // LIBSCRATCHCPP_GLOBAL_H

src/scratch/block.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scratchcpp/input.h>
55
#include <scratchcpp/field.h>
66
#include <scratchcpp/comment.h>
7+
#include <iostream>
78

89
#include "block_p.h"
910

@@ -41,6 +42,24 @@ void Block::setCompileFunction(BlockComp newCompileFunction)
4142
impl->compileFunction = newCompileFunction;
4243
}
4344

45+
/*! Returns the edge-activated hat predicate compile function. \see <a href="blockSections.html">Block sections</a> */
46+
BlockComp Block::hatPredicateCompileFunction() const
47+
{
48+
return impl->hatPredicateCompileFunction;
49+
}
50+
51+
/*! Sets the edge-activated hat predicate compile function. \see <a href="blockSections.html">Block sections</a> */
52+
void Block::setHatPredicateCompileFunction(HatPredicateCompileFunc newHatPredicateCompileFunction)
53+
{
54+
if (newHatPredicateCompileFunction && !topLevel()) {
55+
std::cerr << "error: predicate compile functions can be only used on top-level blocks" << std::endl;
56+
assert(false);
57+
return;
58+
}
59+
60+
impl->hatPredicateCompileFunction = newHatPredicateCompileFunction;
61+
}
62+
4463
/*! Returns true if the block can have a block following it. */
4564
bool Block::mutationHasNext() const
4665
{

src/scratch/block_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct BlockPrivate
2323

2424
std::string opcode;
2525
BlockComp compileFunction = nullptr;
26+
HatPredicateCompileFunc hatPredicateCompileFunction = nullptr;
2627
std::shared_ptr<Block> next = nullptr;
2728
std::string nextId;
2829
std::shared_ptr<Block> parent = nullptr;

test/scratch_classes/block_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ TEST_F(BlockTest, CompileFunction)
247247
ASSERT_EQ(block.compileFunction(), &compileTest);
248248
}
249249

250+
TEST_F(BlockTest, HatPredicateCompileFunction)
251+
{
252+
Block block("", "");
253+
ASSERT_EQ(block.hatPredicateCompileFunction(), nullptr);
254+
255+
block.setHatPredicateCompileFunction(&compileTest);
256+
ASSERT_EQ(block.hatPredicateCompileFunction(), &compileTest);
257+
}
258+
250259
TEST_F(BlockTest, Compile)
251260
{
252261
Block block("", "");

0 commit comments

Comments
 (0)