4 * Created on: Oct 13, 2012
8 #include "ScriptRunner.h"
11 #include "ScriptHost.h"
15 using geometry::Vector;
19 ScriptRunner::ScriptRunner()
33 ScriptRunner::~ScriptRunner() {
38 bool ScriptRunner::Running() const {
39 return script && cursor < script->textlen;
42 void ScriptRunner::Run(ScriptHost &h, const Script &s) {
46 while (cursor < script->textlen) {
47 unsigned char code(script->text[cursor]);
55 void ScriptRunner::Reset() {
61 vector0 = Vector<int>(0, 0);
62 vector1 = Vector<int>(0, 0);
65 void ScriptRunner::Exec(unsigned char code) {
67 case Script::CODE_MOVE_A0:
68 address0 = PopAddress();
70 case Script::CODE_MOVE_A1:
71 address1 = PopAddress();
73 case Script::CODE_MOVE_I0:
76 case Script::CODE_MOVE_I1:
79 case Script::CODE_MOVE_V0:
80 vector0 = PopVector();
82 case Script::CODE_MOVE_V1:
83 vector1 = PopVector();
85 case Script::CODE_ADD_I0:
88 case Script::CODE_ADD_I1:
91 case Script::CODE_ADD_V0:
92 vector0 += PopVector();
94 case Script::CODE_ADD_V1:
95 vector1 += PopVector();
97 case Script::CODE_MOD_I0:
100 case Script::CODE_MOD_I1:
101 integer1 %= PopInt();
103 case Script::CODE_RAND_I0:
104 integer0 = std::rand();
106 case Script::CODE_RAND_I1:
107 integer1 = std::rand();
109 case Script::CODE_CMP_I0:
110 Compare(integer0, PopInt());
112 case Script::CODE_CMP_I1:
113 Compare(integer1, PopInt());
115 case Script::CODE_CMP_I0_I1:
116 Compare(integer0, integer1);
118 case Script::CODE_JUMP:
121 case Script::CODE_JUMP_EQUAL: {
123 if (compare == COMPARE_EQUAL) {
128 case Script::CODE_JUMP_NOT_EQUAL: {
130 if (compare != COMPARE_EQUAL) {
135 case Script::CODE_JUMP_LESS: {
137 if (compare == COMPARE_LESS) {
142 case Script::CODE_JUMP_LESS_EQUAL: {
144 if (compare != COMPARE_GREATER) {
149 case Script::CODE_JUMP_GREATER: {
151 if (compare == COMPARE_GREATER) {
156 case Script::CODE_JUMP_GREATER_EQUAL: {
158 if (compare != COMPARE_LESS) {
163 case Script::CODE_SYSCALL:
164 host->HandleSyscall(*this);
169 void *ScriptRunner::PopAddress() {
170 void *const *addr(reinterpret_cast<void *const *>(script->text + cursor));
171 cursor += sizeof(void *);
175 int ScriptRunner::PopInt() {
176 const int *i(reinterpret_cast<const int *>(script->text + cursor));
177 cursor += sizeof(int);
181 const Vector<int> &ScriptRunner::PopVector() {
182 const Vector<int> *vec(reinterpret_cast<const Vector<int> *>(script->text + cursor));
183 cursor += sizeof(Vector<int>);
187 void ScriptRunner::Compare(int lhs, int rhs) {
189 compare = COMPARE_LESS;
190 } else if (lhs > rhs) {
191 compare = COMPARE_GREATER;
193 compare = COMPARE_EQUAL;