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;
79 case Script::COMMAND_ADD:
80 if (code.reg1 >= numRegisters) {
84 case Script::TYPE_INTEGER: {
85 int value(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
86 integer[code.reg1] += value;
89 case Script::TYPE_VECTOR: {
90 Vector<int> value(code.reg2 < numRegisters ? vector[code.reg2] : PopVector());
91 vector[code.reg1] += value;
99 case Script::COMMAND_MOD: {
100 if (code.reg1 >= numRegisters) {
103 int value(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
104 integer[code.reg1] %= value;
108 case Script::COMMAND_RAND:
109 if (code.reg1 >= numRegisters) {
112 integer[code.reg1] = std::rand();
115 case Script::COMMAND_CMP: {
116 int lhs(code.reg1 < numRegisters ? integer[code.reg1] : PopInt());
117 int rhs(code.reg2 < numRegisters ? integer[code.reg2] : PopInt());
122 case Script::COMMAND_JMP:
125 case Script::COMMAND_JEQ: {
127 if (compare == COMPARE_EQUAL) {
132 case Script::COMMAND_JNE: {
134 if (compare != COMPARE_EQUAL) {
139 case Script::COMMAND_JL: {
141 if (compare == COMPARE_LESS) {
146 case Script::COMMAND_JLE: {
148 if (compare != COMPARE_GREATER) {
153 case Script::COMMAND_JG: {
155 if (compare == COMPARE_GREATER) {
160 case Script::COMMAND_JGE: {
162 if (compare != COMPARE_LESS) {
167 case Script::COMMAND_SYSC:
168 host->HandleSyscall(*this);
173 Script::Code ScriptRunner::PopCode() {
174 const Script::Code *i(reinterpret_cast<const Script::Code *>(script->text + cursor));
175 cursor += sizeof(Script::Code);
179 void *ScriptRunner::PopAddress() {
180 void *const *addr(reinterpret_cast<void *const *>(script->text + cursor));
181 cursor += sizeof(void *);
185 int ScriptRunner::PopInt() {
186 const int *i(reinterpret_cast<const int *>(script->text + cursor));
187 cursor += sizeof(int);
191 const Vector<int> &ScriptRunner::PopVector() {
192 const Vector<int> *vec(reinterpret_cast<const Vector<int> *>(script->text + cursor));
193 cursor += sizeof(Vector<int>);
197 void ScriptRunner::Compare(int lhs, int rhs) {
199 compare = COMPARE_LESS;
200 } else if (lhs > rhs) {
201 compare = COMPARE_GREATER;
203 compare = COMPARE_EQUAL;