@@ -6,9 +6,85 @@ using namespace libscratchcpp;
66
77OperatorBlocks::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
1132std::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+ }
0 commit comments