Skip to content

Commit e155fea

Browse files
committed
VirtualMachine: Add string operators
1 parent c308ab9 commit e155fea

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/engine/virtualmachine.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ unsigned int *VirtualMachine::run(unsigned int *pos)
127127
&&do_list_index_of,
128128
&&do_list_length,
129129
&&do_list_contains,
130+
&&do_str_concat,
131+
&&do_str_at,
132+
&&do_str_length,
133+
&&do_str_contains,
130134
&&do_exec
131135
};
132136
unsigned int *loopStart;
@@ -417,6 +421,25 @@ do_list_get_item : {
417421
REPLACE_RET_VALUE(m_lists[*++pos]->contains(*READ_LAST_REG()), 1);
418422
DISPATCH();
419423

424+
do_str_concat:
425+
REPLACE_RET_VALUE(READ_REG(0, 2)->toString() + READ_REG(1, 2)->toString(), 2);
426+
FREE_REGS(1);
427+
DISPATCH();
428+
429+
do_str_at:
430+
REPLACE_RET_VALUE(READ_REG(0, 2)->toUtf16()[READ_REG(1, 2)->toLong()], 2);
431+
FREE_REGS(1);
432+
DISPATCH();
433+
434+
do_str_length:
435+
REPLACE_RET_VALUE(static_cast<long>(READ_REG(0, 1)->toUtf16().size()), 1);
436+
DISPATCH();
437+
438+
do_str_contains:
439+
REPLACE_RET_VALUE(READ_REG(0, 2)->toUtf16().find(READ_REG(1, 2)->toUtf16()) != std::u16string::npos, 2);
440+
FREE_REGS(1);
441+
DISPATCH();
442+
420443
do_exec:
421444
FREE_REGS(m_functions[*++pos](this));
422445
DISPATCH();

src/engine/virtualmachine.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ enum Opcode
5353
OP_LIST_INDEX_OF, /*!< Stores the index of the value from the last register in the last register (of the list with the index in the argument). */
5454
OP_LIST_LENGTH, /*!< Stores the length of the list with the index in the argument, in the last register. */
5555
OP_LIST_CONTAINS, /*!< Stores true in the last register if the list with the index in the argument contains the value from the last register. */
56+
OP_STR_CONCAT, /*!< Concatenates the strings stored in the last 2 registers and stores the result in the last register, deleting the input registers. */
57+
OP_STR_AT, /*! Stores the character at index in the last register of the string in the second last register, in the last register. */
58+
OP_STR_LENGTH, /*! Stores the length of the string in the last register, in the last register. */
59+
OP_STR_CONTAINS, /*! Stores true in the last register if the string stored in the second last register contains the substring in the last register. */
5660
OP_EXEC /*!< Calls the function with the index in the argument. */
5761
};
5862

@@ -95,7 +99,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
9599
private:
96100
unsigned int *run(unsigned int *pos);
97101

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 };
102+
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, 0, 0, 0, 0, 1 };
99103

100104
unsigned int *m_bytecode;
101105
std::vector<unsigned int> m_bytecodeVector;

0 commit comments

Comments
 (0)