Skip to content

Commit f6f130d

Browse files
committed
VirtualMachine: Add forever loop
1 parent 05b2e78 commit f6f130d

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/engine/virtualmachine.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,44 @@ unsigned int *VirtualMachine::run(RunningScript *script)
7777
unsigned int *VirtualMachine::run(unsigned int *pos, RunningScript *script)
7878
{
7979
static const void *dispatch_table[] = {
80-
nullptr, &&do_halt, &&do_const, &&do_null, &&do_if, &&do_else, &&do_endif, &&do_repeat_loop, &&do_until_loop, &&do_begin_until_loop,
81-
&&do_loop_end, &&do_print, &&do_add, &&do_subtract, &&do_multiply, &&do_divide, &&do_random, &&do_greater_than, &&do_less_than, &&do_equals,
82-
&&do_and, &&do_or, &&do_not, &&do_set_var, &&do_change_var, &&do_read_var, &&do_read_list, &&do_list_append, &&do_list_del, &&do_list_del_all,
83-
&&do_list_insert, &&do_list_replace, &&do_list_get_item, &&do_list_index_of, &&do_list_length, &&do_list_contains, &&do_exec
80+
nullptr,
81+
&&do_halt,
82+
&&do_const,
83+
&&do_null,
84+
&&do_if,
85+
&&do_else,
86+
&&do_endif,
87+
&&do_forever_loop,
88+
&&do_repeat_loop,
89+
&&do_until_loop,
90+
&&do_begin_until_loop,
91+
&&do_loop_end,
92+
&&do_print,
93+
&&do_add,
94+
&&do_subtract,
95+
&&do_multiply,
96+
&&do_divide,
97+
&&do_random,
98+
&&do_greater_than,
99+
&&do_less_than,
100+
&&do_equals,
101+
&&do_and,
102+
&&do_or,
103+
&&do_not,
104+
&&do_set_var,
105+
&&do_change_var,
106+
&&do_read_var,
107+
&&do_read_list,
108+
&&do_list_append,
109+
&&do_list_del,
110+
&&do_list_del_all,
111+
&&do_list_insert,
112+
&&do_list_replace,
113+
&&do_list_get_item,
114+
&&do_list_index_of,
115+
&&do_list_length,
116+
&&do_list_contains,
117+
&&do_exec
84118
};
85119
unsigned int *loopStart;
86120
unsigned int *loopEnd;
@@ -113,6 +147,10 @@ unsigned int *VirtualMachine::run(unsigned int *pos, RunningScript *script)
113147
do_endif:
114148
DISPATCH();
115149

150+
do_forever_loop:
151+
while (true)
152+
run(pos, script);
153+
116154
do_repeat_loop:
117155
FREE_REGS(1);
118156
loopCount = READ_LAST_REG()->toLong();

src/engine/virtualmachine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum Opcode
2020
OP_IF, /*!< Jumps to the next instruction if the last register holds "true". If it's false, jumps to OP_ELSE or OP_ENDIF. */
2121
OP_ELSE, /*!< Jumps to OP_ENDIF. This instruction is typically reached when the if statement condition was "true". */
2222
OP_ENDIF, /*!< Doesn't do anything, but is used by OP_IF and OP_ELSE. */
23+
OP_FOREVER_LOOP, /*!< Runs a forever loop. */
2324
OP_REPEAT_LOOP, /*!< Runs a repeat loop with the number of periods stored in the last register. */
2425
OP_UNTIL_LOOP, /*!< Evaluates the condition before OP_BEGIN_UNTIL_LOOP and runs a repeat until loop. */
2526
OP_BEGIN_UNTIL_LOOP, /*!< Used by OP_UNTIL_LOOP. */
@@ -79,7 +80,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
7980
private:
8081
unsigned int *run(unsigned int *pos, RunningScript *script);
8182

82-
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
83+
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 };
8384

8485
unsigned int *m_bytecode;
8586
std::vector<unsigned int> m_bytecodeVector;

0 commit comments

Comments
 (0)