1 #include "ScriptRunner.h"
4 #include "ScriptHost.h"
12 ScriptRunner::ScriptRunner()
20 ScriptRunner::~ScriptRunner() {
25 bool ScriptRunner::Running() const {
26 return script && cursor < script->textlen;
29 void ScriptRunner::Run(ScriptHost &h, const Script &s) {
33 while (cursor < script->textlen) {
40 void ScriptRunner::Reset() {
42 for (int i(0); i < numRegisters; ++i) {
45 vector[i] = Vector<int>(0, 0);
49 void ScriptRunner::Exec(Script::Code code) {
50 switch (code.command) {
51 case Script::COMMAND_NOOP:
54 case Script::COMMAND_MOVE:
55 if (code.reg1 >= numRegisters) {
59 case Script::TYPE_ADDRESS: {
60 void *value(code.reg2 < numRegisters ? address[code.reg2] : PopAddress());
61 address[code.reg1] = value;
64 case Script::TYPE_INTEGER: {
65 int value(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
66 integer[code.reg1] = value;
69 case Script::TYPE_VECTOR: {
70 Vector<int> value(code.reg2 < numRegisters ? vector[code.reg2] : PopVector());
71 vector[code.reg1] = value;
77 case Script::COMMAND_ADD:
78 if (code.reg1 >= numRegisters) {
82 case Script::TYPE_INTEGER: {
83 int value(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
84 integer[code.reg1] += value;
87 case Script::TYPE_VECTOR: {
88 Vector<int> value(code.reg2 < numRegisters ? vector[code.reg2] : PopVector());
89 vector[code.reg1] += value;
95 case Script::COMMAND_MOD: {
96 if (code.reg1 >= numRegisters) {
99 int value(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
100 integer[code.reg1] %= value;
104 case Script::COMMAND_RAND:
105 if (code.reg1 >= numRegisters) {
108 integer[code.reg1] = std::rand();
111 case Script::COMMAND_CMP: {
112 int lhs(code.reg1 < numRegisters ? integer[code.reg1] : PopInt());
113 int rhs(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
118 case Script::COMMAND_JMP:
121 case Script::COMMAND_JEQ: {
123 if (compare == COMPARE_EQUAL) {
128 case Script::COMMAND_JNE: {
130 if (compare != COMPARE_EQUAL) {
135 case Script::COMMAND_JL: {
137 if (compare == COMPARE_LESS) {
142 case Script::COMMAND_JLE: {
144 if (compare != COMPARE_GREATER) {
149 case Script::COMMAND_JG: {
151 if (compare == COMPARE_GREATER) {
156 case Script::COMMAND_JGE: {
158 if (compare != COMPARE_LESS) {
163 case Script::COMMAND_SYSC:
164 host->HandleSyscall(*this);
169 Script::Code ScriptRunner::PopCode() {
170 const Script::Code *i(reinterpret_cast<const Script::Code *>(script->text + cursor));
171 cursor += sizeof(Script::Code);
175 void *ScriptRunner::PopAddress() {
176 void *const *addr(reinterpret_cast<void *const *>(script->text + cursor));
177 cursor += sizeof(void *);
181 int ScriptRunner::PopInt() {
182 const int *i(reinterpret_cast<const int *>(script->text + cursor));
183 cursor += sizeof(int);
187 const Vector<int> &ScriptRunner::PopVector() {
188 const Vector<int> *vec(reinterpret_cast<const Vector<int> *>(script->text + cursor));
189 cursor += sizeof(Vector<int>);
193 void ScriptRunner::Compare(int lhs, int rhs) {
195 compare = COMPARE_LESS;
196 } else if (lhs > rhs) {
197 compare = COMPARE_GREATER;
199 compare = COMPARE_EQUAL;