Skip to content

Commit c308ab9

Browse files
committed
VirtualMachine: Add modulo operator
1 parent 74f769f commit c308ab9

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

docs/Virtual machine.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ OP_LOOP_END
8989
- \link libscratchcpp::vm::OP_SUBTRACT OP_SUBTRACT \endlink
9090
- \link libscratchcpp::vm::OP_MULTIPLY OP_MULTIPLY \endlink
9191
- \link libscratchcpp::vm::OP_DIVIDE OP_DIVIDE \endlink
92+
- \link libscratchcpp::vm::OP_MOD OP_MOD \endlink
9293

9394
### Relational operators
9495
- \link libscratchcpp::vm::OP_GREATER_THAN OP_GREATER_THAN \endlink

src/engine/virtualmachine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ unsigned int *VirtualMachine::run(unsigned int *pos)
106106
&&do_subtract,
107107
&&do_multiply,
108108
&&do_divide,
109+
&&do_mod,
109110
&&do_random,
110111
&&do_greater_than,
111112
&&do_less_than,
@@ -233,6 +234,12 @@ unsigned int *VirtualMachine::run(unsigned int *pos)
233234
FREE_REGS(1);
234235
DISPATCH();
235236

237+
do_mod:
238+
REPLACE_RET_VALUE(*READ_REG(0, 2), 2);
239+
READ_REG(0, 2)->mod(*READ_REG(1, 2));
240+
FREE_REGS(1);
241+
DISPATCH();
242+
236243
do_random:
237244
REPLACE_RET_VALUE(randint<long>(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2);
238245
FREE_REGS(1);

src/engine/virtualmachine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum Opcode
3232
OP_SUBTRACT, /*!< Subtracts the values stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
3333
OP_MULTIPLY, /*!< Multiplies the values stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
3434
OP_DIVIDE, /*!< Divides the values stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
35+
OP_MOD, /*!< Calculates module of the values stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
3536
OP_RANDOM, /*!< Generates a random value in the range stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
3637
OP_GREATER_THAN, /*!< Compares (>) the values stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
3738
OP_LESS_THAN, /*!< Compares (<) the values stored in the last 2 registers and stores the result in the last registry, deleting the input registers. */
@@ -94,7 +95,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
9495
private:
9596
unsigned int *run(unsigned int *pos);
9697

97-
static inline const unsigned int instruction_arg_count[] = { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
98+
static inline const unsigned int instruction_arg_count[] = { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
9899

99100
unsigned int *m_bytecode;
100101
std::vector<unsigned int> m_bytecodeVector;

0 commit comments

Comments
 (0)