Skip to content

Commit ba73f67

Browse files
committed
VirtualMachine: Add support for procedure arguments
1 parent 7b83d47 commit ba73f67

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/engine/virtualmachine.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ unsigned int *VirtualMachine::run(unsigned int *pos)
154154
&&do_str_length,
155155
&&do_str_contains,
156156
&&do_exec,
157-
&&do_call_procedure
157+
&&do_init_procedure,
158+
&&do_call_procedure,
159+
&&do_add_arg,
160+
&&do_read_arg
158161
};
159162
unsigned int *loopStart;
160163
unsigned int *loopEnd;
@@ -171,6 +174,11 @@ unsigned int *VirtualMachine::run(unsigned int *pos)
171174
} else {
172175
pos = m_callTree.back();
173176
m_callTree.pop_back();
177+
m_procedureArgTree.pop_back();
178+
if (m_procedureArgTree.empty())
179+
m_procedureArgs = nullptr;
180+
else
181+
m_procedureArgs = &m_procedureArgTree.back();
174182
DISPATCH();
175183
}
176184

@@ -596,8 +604,26 @@ do_list_get_item : {
596604
FREE_REGS(m_functions[*++pos](this));
597605
DISPATCH();
598606

607+
do_init_procedure:
608+
m_procedureArgTree.push_back({});
609+
if (m_procedureArgTree.size() >= 2)
610+
m_procedureArgs = &m_procedureArgTree[m_procedureArgTree.size() - 2];
611+
m_nextProcedureArgs = &m_procedureArgTree.back();
612+
DISPATCH();
613+
599614
do_call_procedure:
600615
m_callTree.push_back(++pos);
616+
m_procedureArgs = m_nextProcedureArgs;
617+
m_nextProcedureArgs = nullptr;
601618
pos = m_procedures[*pos];
602619
DISPATCH();
620+
621+
do_add_arg:
622+
m_nextProcedureArgs->push_back(*READ_LAST_REG());
623+
FREE_REGS(1);
624+
DISPATCH();
625+
626+
do_read_arg:
627+
ADD_RET_VALUE(m_procedureArgs->operator[](*++pos));
628+
DISPATCH();
603629
}

src/engine/virtualmachine.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,20 @@ enum Opcode
5959
OP_LIST_DEL, /*!< Deletes the index (or item like "last" or "random") stored in the last register of the list with the index in the argument. */
6060
OP_LIST_DEL_ALL, /*!< Clears the list with the index in the argument. */
6161
OP_LIST_INSERT, /*!< Inserts the value from the second last register at the index (or item like "last" or "random") stored in the last register to the list with the index in the argument. */
62-
OP_LIST_REPLACE, /*!< Replaces the index (or item like "last" or "random") stored in the second last register with the value from the last register in the list with the index in the argument. */
63-
OP_LIST_GET_ITEM, /*!< Stores the value at the index (or item like "last" or "random") (of the list with the index in the argument) stored in the last register, in the last register. */
64-
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). */
65-
OP_LIST_LENGTH, /*!< Stores the length of the list with the index in the argument, in the last register. */
66-
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. */
67-
OP_STR_CONCAT, /*!< Concatenates the strings stored in the last 2 registers and stores the result in the last register, deleting the input registers. */
68-
OP_STR_AT, /*! Stores the character at index in the last register of the string in the second last register, in the last register. */
69-
OP_STR_LENGTH, /*! Stores the length of the string in the last register, in the last register. */
70-
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. */
71-
OP_EXEC, /*!< Calls the function with the index in the argument. */
72-
OP_CALL_PROCEDURE /*! Calls the procedure (custom block) with the index in the argument. */
62+
OP_LIST_REPLACE, /*!< Replaces the index (or item like "last" or "random") stored in the second last register with the value from the last register in the list with the index in the argument. */
63+
OP_LIST_GET_ITEM, /*!< Stores the value at the index (or item like "last" or "random") (of the list with the index in the argument) stored in the last register, in the last register. */
64+
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). */
65+
OP_LIST_LENGTH, /*!< Stores the length of the list with the index in the argument, in the last register. */
66+
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. */
67+
OP_STR_CONCAT, /*!< Concatenates the strings stored in the last 2 registers and stores the result in the last register, deleting the input registers. */
68+
OP_STR_AT, /*! Stores the character at index in the last register of the string in the second last register, in the last register. */
69+
OP_STR_LENGTH, /*! Stores the length of the string in the last register, in the last register. */
70+
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. */
71+
OP_EXEC, /*!< Calls the function with the index in the argument. */
72+
OP_INIT_PROCEDURE, /*!< Initializes the list of procedure (custom block) arguments. */
73+
OP_CALL_PROCEDURE, /*! Calls the procedure (custom block) with the index in the argument. */
74+
OP_ADD_ARG, /*!< Adds a procedure (custom block) argument with the value from the last register. */
75+
OP_READ_ARG /*!< Reads the procedure (custom block) argument with the index in the argument and stores the value in the last register. */
7376
};
7477

7578
}
@@ -113,7 +116,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
113116
unsigned int *run(unsigned int *pos);
114117

115118
static inline const unsigned int instruction_arg_count[] = {
116-
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, 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, 1
119+
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, 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, 0, 1, 1, 0
117120
};
118121

119122
typedef struct
@@ -131,6 +134,9 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
131134
bool m_atEnd = false;
132135
std::vector<Loop> m_loops;
133136
std::vector<unsigned int *> m_callTree;
137+
std::vector<std::vector<Value>> m_procedureArgTree;
138+
std::vector<Value> *m_procedureArgs = nullptr;
139+
std::vector<Value> *m_nextProcedureArgs = nullptr;
134140

135141
unsigned int **m_procedures = nullptr;
136142
std::vector<unsigned int *> m_proceduresVector;

0 commit comments

Comments
 (0)