Skip to content

Commit 0261dc4

Browse files
committed
Add initial implementation of operator blocks
* operator_add * operator_subtract * operator_multiply * operator_divide * operator_random * operator_lt * operator_equals * operator_gt * operator_and * operator_or * operator_not
1 parent dea2218 commit 0261dc4

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/blocks/operatorblocks.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,85 @@ using namespace libscratchcpp;
66

77
OperatorBlocks::OperatorBlocks()
88
{
9+
// Blocks
10+
addBlock("operator_add", &OperatorBlocks::add);
11+
addBlock("operator_subtract", &OperatorBlocks::subtract);
12+
addBlock("operator_multiply", &OperatorBlocks::multiply);
13+
addBlock("operator_divide", &OperatorBlocks::divide);
14+
addBlock("operator_random", &OperatorBlocks::pickRandom);
15+
addBlock("operator_lt", &OperatorBlocks::lessThan);
16+
addBlock("operator_equals", &OperatorBlocks::equals);
17+
addBlock("operator_gt", &OperatorBlocks::greaterThan);
18+
addBlock("operator_and", &OperatorBlocks::andGate);
19+
addBlock("operator_or", &OperatorBlocks::orGate);
20+
addBlock("operator_not", &OperatorBlocks::notGate);
21+
22+
// Inputs
23+
addInput("NUM1", NUM1);
24+
addInput("NUM2", NUM2);
25+
addInput("FROM", FROM);
26+
addInput("TO", TO);
27+
addInput("OPERAND1", OPERAND1);
28+
addInput("OPERAND2", OPERAND2);
29+
addInput("OPERAND", OPERAND);
930
}
1031

1132
std::string OperatorBlocks::name() const
1233
{
1334
return "Operators";
1435
}
36+
37+
Value OperatorBlocks::add(const BlockArgs &args)
38+
{
39+
return args.input(NUM1)->value() + args.input(NUM2)->value();
40+
}
41+
42+
Value OperatorBlocks::subtract(const BlockArgs &args)
43+
{
44+
return args.input(NUM1)->value() - args.input(NUM2)->value();
45+
}
46+
47+
Value OperatorBlocks::multiply(const BlockArgs &args)
48+
{
49+
return args.input(NUM1)->value() * args.input(NUM2)->value();
50+
}
51+
52+
Value OperatorBlocks::divide(const BlockArgs &args)
53+
{
54+
return args.input(NUM1)->value() / args.input(NUM2)->value();
55+
}
56+
57+
Value OperatorBlocks::pickRandom(const BlockArgs &args)
58+
{
59+
return randint(args.input(FROM)->value().toNumber(), args.input(TO)->value().toNumber());
60+
}
61+
62+
Value OperatorBlocks::lessThan(const BlockArgs &args)
63+
{
64+
return args.input(OPERAND1)->value() < args.input(OPERAND2)->value();
65+
}
66+
67+
Value OperatorBlocks::equals(const BlockArgs &args)
68+
{
69+
return args.input(OPERAND1)->value() == args.input(OPERAND2)->value();
70+
}
71+
72+
Value OperatorBlocks::greaterThan(const BlockArgs &args)
73+
{
74+
return args.input(OPERAND1)->value() > args.input(OPERAND2)->value();
75+
}
76+
77+
Value OperatorBlocks::andGate(const BlockArgs &args)
78+
{
79+
return args.input(OPERAND1)->value().toBool() && args.input(OPERAND2)->value().toBool();
80+
}
81+
82+
Value OperatorBlocks::orGate(const BlockArgs &args)
83+
{
84+
return args.input(OPERAND1)->value().toBool() || args.input(OPERAND2)->value().toBool();
85+
}
86+
87+
Value OperatorBlocks::notGate(const BlockArgs &args)
88+
{
89+
return !args.input(OPERAND)->value().toBool();
90+
}

src/blocks/operatorblocks.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,32 @@ namespace libscratchcpp
1111
class LIBSCRATCHCPP_EXPORT OperatorBlocks : public IBlockSection
1212
{
1313
public:
14+
enum Inputs
15+
{
16+
NUM1,
17+
NUM2,
18+
FROM,
19+
TO,
20+
OPERAND1,
21+
OPERAND2,
22+
OPERAND
23+
};
24+
1425
OperatorBlocks();
1526

1627
std::string name() const override;
28+
29+
static Value add(const BlockArgs &args);
30+
static Value subtract(const BlockArgs &args);
31+
static Value multiply(const BlockArgs &args);
32+
static Value divide(const BlockArgs &args);
33+
static Value pickRandom(const BlockArgs &args);
34+
static Value lessThan(const BlockArgs &args);
35+
static Value equals(const BlockArgs &args);
36+
static Value greaterThan(const BlockArgs &args);
37+
static Value andGate(const BlockArgs &args);
38+
static Value orGate(const BlockArgs &args);
39+
static Value notGate(const BlockArgs &args);
1740
};
1841

1942
} // namespace libscratchcpp

0 commit comments

Comments
 (0)