Script.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. #include <stdarg.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "Operation.h"
  7. #include "Script.h"
  8. static void sError(Script* sc, const char* format, ...) {
  9. va_list args;
  10. va_start(args, format);
  11. vsnprintf(sc->error, SCRIPT_ERROR_SIZE, format, args);
  12. va_end(args);
  13. }
  14. static bool sPrinter(Object* o) {
  15. switch(o->type) {
  16. case OT_INT: printf("%d\n", o->data.intValue); return false;
  17. case OT_FLOAT: printf("%.2f\n", o->data.floatValue); return false;
  18. case OT_CONST_STRING: printf("%s\n", o->data.stringValue); return false;
  19. case OT_NULL: printf("null\n"); return false;
  20. case OT_BOOL: printf(o->data.intValue ? "true\n" : "false\n"); return false;
  21. case OT_ARRAY: printf("array\n"); return false;
  22. default: return true;
  23. }
  24. }
  25. static ObjectPrinter printer = sPrinter;
  26. static bool sRead(Script* sc, void* buffer, int length) {
  27. if(sc->readIndex + length > sc->code->length) {
  28. sError(sc, "cannot read expected %d bytes of data from bytecode on line %d", sc->line);
  29. return true;
  30. }
  31. memcpy(buffer, sc->code->code + sc->readIndex, length);
  32. sc->readIndex += length;
  33. return false;
  34. }
  35. static Operation sReadOperation(Script* sc) {
  36. unsigned char c;
  37. if(sRead(sc, &c, 1)) {
  38. return OP_NOTHING;
  39. }
  40. return c;
  41. }
  42. static bool sReadInt(Script* sc, int* i) {
  43. return !sRead(sc, i, sizeof(int));
  44. }
  45. static bool sCheckType(Script* sc, Object* o, ObjectType ot) {
  46. if(o->type == ot) {
  47. return true;
  48. }
  49. sError(sc, "object is not of type %s on line %d", oGetName(ot), sc->line);
  50. return false;
  51. }
  52. static bool sPush(Script* sc, Object* o) {
  53. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  54. sError(sc, "stack overflow on line %d", sc->line);
  55. return false;
  56. }
  57. sc->stack[sc->stackIndex++] = *o;
  58. return true;
  59. }
  60. static bool sPop(Script* sc, Object* o) {
  61. if(sc->stackIndex <= 0) {
  62. sError(sc, "stack underflow on line %d", sc->line);
  63. return true;
  64. }
  65. *o = sc->stack[--sc->stackIndex];
  66. return false;
  67. }
  68. static void sPopEmpty(Script* sc) {
  69. Object o;
  70. sPop(sc, &o);
  71. }
  72. static Object* sPeek(Script* sc) {
  73. if(sc->stackIndex <= 0) {
  74. sError(sc, "stack underflow on line %d", sc->line);
  75. return NULL;
  76. }
  77. return sc->stack + (sc->stackIndex - 1);
  78. }
  79. static bool sPushInt(Script* sc, int value) {
  80. Object o = {.type = OT_INT, .data.intValue = value};
  81. return sPush(sc, &o);
  82. }
  83. static void sPushFloat(Script* sc, float value) {
  84. Object o = {.type = OT_FLOAT, .data.floatValue = value};
  85. sPush(sc, &o);
  86. }
  87. static void sPushNull(Script* sc) {
  88. Object o = {.type = OT_NULL};
  89. sPush(sc, &o);
  90. }
  91. static void sPushBool(Script* sc, bool value) {
  92. Object o = {.type = OT_BOOL, .data.intValue = value};
  93. sPush(sc, &o);
  94. }
  95. static void sPushReference(Script* sc, int pointer, int index) {
  96. Object o = {.type = OT_REFERENCE, .data.reference.pointer = pointer, .data.reference.index = index};
  97. sPush(sc, &o);
  98. }
  99. static void sPushVars(Script* sc) {
  100. int vars = 0;
  101. int offset = 0;
  102. if(sReadInt(sc, &vars) && sReadInt(sc, &offset)) {
  103. int stackVarIndex = sc->stackVarIndex;
  104. sc->stackVarIndex = sc->stackIndex - offset;
  105. for(int i = 0; i < vars - offset; i++) {
  106. sPushNull(sc);
  107. }
  108. sPushInt(sc, stackVarIndex);
  109. }
  110. }
  111. static void sPopVars(Script* sc) {
  112. int value = 0;
  113. Object o;
  114. if(sReadInt(sc, &value) && !sPop(sc, &o) && sCheckType(sc, &o, OT_INT)) {
  115. sc->stackVarIndex = o.data.intValue;
  116. if(sc->stackIndex < value) {
  117. sError(sc, "stack underflow on line %d", sc->line);
  118. } else {
  119. sc->stackIndex -= value;
  120. }
  121. }
  122. }
  123. static void sReferenceFromVar(Script* sc) {
  124. int value = 0;
  125. if(sReadInt(sc, &value)) {
  126. sPushReference(sc, -1, value + sc->stackVarIndex);
  127. }
  128. }
  129. static void sReferenceFromArray(Script* sc) {
  130. int value = 0;
  131. Object o;
  132. if(sReadInt(sc, &value) && !sPop(sc, &o) && sCheckType(sc, &o, OT_INT)) {
  133. sPushReference(sc, value, o.data.intValue);
  134. }
  135. }
  136. static Object* sDereference(Script* sc, Object* reference) {
  137. int pointer = reference->data.reference.pointer;
  138. int index = reference->data.reference.index;
  139. if(pointer == -1) {
  140. if(index < 0 || index >= sc->stackIndex) {
  141. sError(sc, "variable reference index exceeds stack on line %d", sc->line);
  142. return NULL;
  143. }
  144. return sc->stack + index;
  145. } else {
  146. if(pointer < 0 || pointer >= sc->allocator.capacity) {
  147. sError(sc, "array reference pointer is out of range on line %d\n", sc->line);
  148. return NULL;
  149. } else if(index < 0 || index >= sc->allocator.data[pointer].length) {
  150. sError(sc, "array reference index is out of bounds on line %d\n", sc->line);
  151. return NULL;
  152. }
  153. return sc->allocator.data[pointer].data + index;
  154. }
  155. }
  156. static Object* sPopAndDereference(Script* sc) {
  157. Object o;
  158. if(!sPop(sc, &o) && sCheckType(sc, &o, OT_REFERENCE)) {
  159. return sDereference(sc, &o);
  160. }
  161. return NULL;
  162. }
  163. static void sDereferencePush(Script* sc) {
  164. Object* o = sPopAndDereference(sc);
  165. if(o != NULL) {
  166. sPush(sc, o);
  167. }
  168. }
  169. static void sSet(Script* sc) {
  170. Object value;
  171. if(sPop(sc, &value)) {
  172. return;
  173. }
  174. Object* o = sPopAndDereference(sc);
  175. if(o != NULL) {
  176. *o = value;
  177. }
  178. }
  179. static void sAllocateArray(Script* sc) {
  180. Object length;
  181. if(!sPop(sc, &length) && sCheckType(sc, &length, OT_INT)) {
  182. if(length.data.intValue < 0) {
  183. sError(sc, "negative array length on line %d", sc->line);
  184. return;
  185. }
  186. Object o = {.type = OT_ARRAY, .data.intValue = aAllocate(&sc->allocator, length.data.intValue)};
  187. sPush(sc, &o);
  188. }
  189. }
  190. static void sArrayLength(Script* sc) {
  191. Object* o = sPopAndDereference(sc);
  192. if(o != NULL && sCheckType(sc, o, OT_ARRAY)) {
  193. int arrayPointer = o->data.intValue;
  194. if(arrayPointer < 0 || arrayPointer >= sc->allocator.capacity) {
  195. sError(sc, "array pointer is out of range on line %d\n", sc->line);
  196. return;
  197. }
  198. sPushInt(sc, sc->allocator.data[arrayPointer].length);
  199. }
  200. }
  201. static void sPreChange(Script* sc, int change) {
  202. Object* o = sPopAndDereference(sc);
  203. if(o != NULL) {
  204. if(o->type == OT_INT) {
  205. o->data.intValue += change;
  206. } else if(o->type == OT_FLOAT) {
  207. o->data.floatValue += change;
  208. } else {
  209. sError(sc, "variable is '%s' not a number on line %d", oGetName(o->type), sc->line);
  210. return;
  211. }
  212. sPush(sc, o);
  213. }
  214. }
  215. static void sPreIncrement(Script* sc) {
  216. sPreChange(sc, 1);
  217. }
  218. static void sPreDecrement(Script* sc) {
  219. sPreChange(sc, -1);
  220. }
  221. static void sPostChange(Script* sc, int change) {
  222. Object* o = sPopAndDereference(sc);
  223. if(o != NULL) {
  224. if(o->type == OT_INT) {
  225. sPush(sc, o);
  226. o->data.intValue += change;
  227. } else if(o->type == OT_FLOAT) {
  228. sPush(sc, o);
  229. o->data.floatValue += change;
  230. } else {
  231. sError(sc, "variable is '%s' not a number on line %d", oGetName(o->type), sc->line);
  232. }
  233. }
  234. }
  235. static void sPostIncrement(Script* sc) {
  236. sPostChange(sc, 1);
  237. }
  238. static void sPostDecrement(Script* sc) {
  239. sPostChange(sc, -1);
  240. }
  241. static void sPushCodeInt(Script* sc) {
  242. int value = 0;
  243. if(sReadInt(sc, &value)) {
  244. sPushInt(sc, value);
  245. }
  246. }
  247. static void sPushCodeFloat(Script* sc) {
  248. float value = 0;
  249. if(sRead(sc, &value, sizeof(float))) {
  250. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  251. return;
  252. }
  253. sPushFloat(sc, value);
  254. }
  255. static void sPushCodeString(Script* sc) {
  256. int value = 0;
  257. if(sReadInt(sc, &value)) {
  258. char* s = (char*)(sc->code->code + sc->readIndex);
  259. sc->readIndex += value;
  260. Object o = {.type = OT_CONST_STRING, .data.stringValue = s};
  261. sPush(sc, &o);
  262. }
  263. }
  264. static void sNumberBinary(Script* sc, int (*fInt)(Script*, int, int), float (*fFloat)(float, float)) {
  265. Object o[2];
  266. if(sPop(sc, o) || sPop(sc, o + 1)) {
  267. return;
  268. } else if(o[0].type == OT_INT && o[1].type == OT_INT) {
  269. sPushInt(sc, fInt(sc, o[0].data.intValue, o[1].data.intValue));
  270. } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) {
  271. sPushFloat(sc, fFloat(o[0].data.floatValue, o[1].data.intValue));
  272. } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) {
  273. sPushFloat(sc, fFloat(o[0].data.intValue, o[1].data.floatValue));
  274. } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) {
  275. sPushFloat(sc, fFloat(o[0].data.floatValue, o[1].data.floatValue));
  276. } else {
  277. sError(sc, "object is not a number on line %d", sc->line);
  278. }
  279. }
  280. static void sIntBinary(Script* sc, int (*f)(Script*, int, int)) {
  281. Object o[2];
  282. if(!sPop(sc, o) && !sPop(sc, o + 1) && sCheckType(sc, o, OT_INT) && sCheckType(sc, o + 1, OT_INT)) {
  283. sPushInt(sc, f(sc, o[0].data.intValue, o[1].data.intValue));
  284. }
  285. }
  286. static int sIntAdd(Script* sc, int a, int b) {
  287. (void)sc;
  288. return a + b;
  289. }
  290. static int sIntSub(Script* sc, int a, int b) {
  291. (void)sc;
  292. return b - a;
  293. }
  294. static int sIntMul(Script* sc, int a, int b) {
  295. (void)sc;
  296. return a * b;
  297. }
  298. static int sIntDiv(Script* sc, int a, int b) {
  299. if(a == 0) {
  300. sError(sc, "division by 0 on line %d", sc->line);
  301. return b;
  302. }
  303. return b / a;
  304. }
  305. static int sMod(Script* sc, int a, int b) {
  306. if(a == 0) {
  307. sError(sc, "module of 0 on line %d", sc->line);
  308. return b;
  309. }
  310. return b % a;
  311. }
  312. static int sBitAnd(Script* sc, int a, int b) {
  313. (void)sc;
  314. return b & a;
  315. }
  316. static int sBitOr(Script* sc, int a, int b) {
  317. (void)sc;
  318. return b | a;
  319. }
  320. static int sBitXor(Script* sc, int a, int b) {
  321. (void)sc;
  322. return b ^ a;
  323. }
  324. static int sLeftShift(Script* sc, int a, int b) {
  325. (void)sc;
  326. return b << a;
  327. }
  328. static int sRightShift(Script* sc, int a, int b) {
  329. (void)sc;
  330. return b >> a;
  331. }
  332. static float sFloatAdd(float a, float b) {
  333. return a + b;
  334. }
  335. static float sFloatSub(float a, float b) {
  336. return b - a;
  337. }
  338. static float sFloatMul(float a, float b) {
  339. return a * b;
  340. }
  341. static float sFloatDiv(float a, float b) {
  342. return b / a;
  343. }
  344. static void sBoolBinary(Script* sc, bool (*fInt)(int, int), bool (*fFloat)(float, float)) {
  345. Object o[2];
  346. if(sPop(sc, o) || sPop(sc, o + 1)) {
  347. return;
  348. } else if(o[0].type == OT_INT && o[1].type == OT_INT) {
  349. sPushBool(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  350. } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) {
  351. sPushBool(sc, fFloat(o[0].data.floatValue, o[1].data.intValue));
  352. } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) {
  353. sPushBool(sc, fFloat(o[0].data.intValue, o[1].data.floatValue));
  354. } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) {
  355. sPushBool(sc, fFloat(o[0].data.floatValue, o[1].data.floatValue));
  356. } else {
  357. sError(sc, "object is not a number on line %d", sc->line);
  358. }
  359. }
  360. static bool sIntLess(int a, int b) {
  361. return b < a;
  362. }
  363. static bool sIntGreater(int a, int b) {
  364. return b > a;
  365. }
  366. static bool sFloatLess(float a, float b) {
  367. return b < a;
  368. }
  369. static bool sFloatGreater(float a, float b) {
  370. return b > a;
  371. }
  372. static void sInvertSign(Script* sc) {
  373. Object* o = sPeek(sc);
  374. if(o == NULL) {
  375. return;
  376. } else if(o->type == OT_INT) {
  377. o->data.intValue = -o->data.intValue;
  378. } else if(o->type == OT_FLOAT) {
  379. o->data.floatValue = -o->data.floatValue;
  380. } else {
  381. sError(sc, "object is not a number on line %d", sc->line);
  382. }
  383. }
  384. static void sEqual(Script* sc) {
  385. Object o[2];
  386. if(sPop(sc, o) || sPop(sc, o + 1)) {
  387. return;
  388. } else if(o[0].type == OT_INT && o[1].type == OT_INT) {
  389. sPushBool(sc, o[0].data.intValue == o[1].data.intValue);
  390. } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) {
  391. sPushBool(sc, o[0].data.intValue == o[1].data.floatValue);
  392. } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) {
  393. sPushBool(sc, o[0].data.floatValue == o[1].data.intValue);
  394. } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) {
  395. sPushBool(sc, o[0].data.floatValue == o[1].data.floatValue);
  396. } else if(o[0].type == OT_BOOL && o[1].type == OT_BOOL) {
  397. sPushBool(sc, o[0].data.intValue == o[1].data.intValue);
  398. } else if(o[0].type == OT_NULL && o[1].type == OT_NULL) {
  399. sPushBool(sc, true);
  400. } else {
  401. sError(sc, "object types do not match on line %d", sc->line);
  402. }
  403. }
  404. static void sNot(Script* sc) {
  405. Object* o = sPeek(sc);
  406. if(o != NULL && sCheckType(sc, o, OT_BOOL)) {
  407. o->data.intValue = !o->data.intValue;
  408. }
  409. }
  410. static void sBitNot(Script* sc) {
  411. Object* o = sPeek(sc);
  412. if(o != NULL && sCheckType(sc, o, OT_INT)) {
  413. o->data.intValue = ~o->data.intValue;
  414. }
  415. }
  416. static void sAnd(Script* sc) {
  417. Object o[2];
  418. if(!sPop(sc, o) && !sPop(sc, o + 1) && sCheckType(sc, o, OT_BOOL) && sCheckType(sc, o + 1, OT_BOOL)) {
  419. sPushBool(sc, o[0].data.intValue && o[1].data.intValue);
  420. }
  421. }
  422. static void sOr(Script* sc) {
  423. Object o[2];
  424. if(!sPop(sc, o) && !sPop(sc, o + 1) && sCheckType(sc, o, OT_BOOL) && sCheckType(sc, o + 1, OT_BOOL)) {
  425. sPushBool(sc, o[0].data.intValue || o[1].data.intValue);
  426. }
  427. }
  428. static void sPrint(Script* sc) {
  429. Object o;
  430. if(!sPop(sc, &o) && printer(&o)) {
  431. sError(sc, "cannot print given object on line %d", sc->line);
  432. }
  433. }
  434. static void sLine(Script* sc) {
  435. if(sRead(sc, &sc->line, 2)) {
  436. sError(sc, "line operation without a line near line %d", sc->line);
  437. }
  438. }
  439. static void sGoTo(Script* sc) {
  440. int gotoIndex;
  441. if(sReadInt(sc, &gotoIndex)) {
  442. sc->readIndex = gotoIndex;
  443. }
  444. }
  445. static void sGoSub(Script* sc) {
  446. int gotoIndex;
  447. int arguments;
  448. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments)) {
  449. int returnStackIndex = sc->stackIndex - arguments - 1;
  450. if(returnStackIndex < 0 || sc->stack[returnStackIndex].type != OT_INT) {
  451. sError(sc, "cannot find return address entry on stack on line %d", sc->line);
  452. return;
  453. }
  454. sc->stack[returnStackIndex].data.intValue = sc->readIndex;
  455. sc->readIndex = gotoIndex;
  456. }
  457. }
  458. static void sIfGoTo(Script* sc) {
  459. int gotoIndex;
  460. Object o;
  461. if(sReadInt(sc, &gotoIndex) && !sPop(sc, &o) && sCheckType(sc, &o, OT_BOOL) && !o.data.intValue) {
  462. sc->readIndex = gotoIndex;
  463. }
  464. }
  465. static void sSetReturn(Script* sc) {
  466. sPop(sc, &sc->returnValue);
  467. }
  468. static void sReturn(Script* sc) {
  469. Object o;
  470. if(!sPop(sc, &o) && sCheckType(sc, &o, OT_INT)) {
  471. sc->readIndex = o.data.intValue;
  472. if(sc->returnValue.type != OT_VOID) {
  473. sPush(sc, &sc->returnValue);
  474. sc->returnValue.type = OT_VOID;
  475. }
  476. }
  477. }
  478. static void sDuplicate(Script* sc) {
  479. Object* o = sPeek(sc);
  480. if(o != NULL) {
  481. sPush(sc, o);
  482. }
  483. }
  484. static void sConsumeInstruction(Script* sc) {
  485. Operation op = sReadOperation(sc);
  486. // printf("Operation: %d\n", op);
  487. switch(op) {
  488. case OP_NOTHING: break;
  489. case OP_PUSH_INT: sPushCodeInt(sc); break;
  490. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  491. case OP_PUSH_CONST_STRING: sPushCodeString(sc); break;
  492. case OP_PUSH_NULL: sPushNull(sc); break;
  493. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  494. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  495. case OP_PUSH_VARS: sPushVars(sc); break;
  496. case OP_POP_VARS: sPopVars(sc); break;
  497. case OP_POP: sPopEmpty(sc); break;
  498. case OP_SET: sSet(sc); break;
  499. case OP_PRE_INCREMENT: sPreIncrement(sc); break;
  500. case OP_POST_INCREMENT: sPostIncrement(sc); break;
  501. case OP_PRE_DECREMENT: sPreDecrement(sc); break;
  502. case OP_POST_DECREMENT: sPostDecrement(sc); break;
  503. case OP_ADD: sNumberBinary(sc, sIntAdd, sFloatAdd); break;
  504. case OP_SUB: sNumberBinary(sc, sIntSub, sFloatSub); break;
  505. case OP_MUL: sNumberBinary(sc, sIntMul, sFloatMul); break;
  506. case OP_DIV: sNumberBinary(sc, sIntDiv, sFloatDiv); break;
  507. case OP_MOD: sIntBinary(sc, sMod); break;
  508. case OP_INVERT_SIGN: sInvertSign(sc); break;
  509. case OP_LESS: sBoolBinary(sc, sIntLess, sFloatLess); break;
  510. case OP_GREATER: sBoolBinary(sc, sIntGreater, sFloatGreater); break;
  511. case OP_EQUAL: sEqual(sc); break;
  512. case OP_NOT: sNot(sc); break;
  513. case OP_AND: sAnd(sc); break;
  514. case OP_OR: sOr(sc); break;
  515. case OP_BIT_NOT: sBitNot(sc); break;
  516. case OP_BIT_AND: sIntBinary(sc, sBitAnd); break;
  517. case OP_BIT_OR: sIntBinary(sc, sBitOr); break;
  518. case OP_BIT_XOR: sIntBinary(sc, sBitXor); break;
  519. case OP_LEFT_SHIFT: sIntBinary(sc, sLeftShift); break;
  520. case OP_RIGHT_SHIFT: sIntBinary(sc, sRightShift); break;
  521. case OP_PRINT: sPrint(sc); break;
  522. case OP_LINE: sLine(sc); break;
  523. case OP_GOTO: sGoTo(sc); break;
  524. case OP_GOSUB: sGoSub(sc); break;
  525. case OP_IF_GOTO: sIfGoTo(sc); break;
  526. case OP_SET_RETURN: sSetReturn(sc); break;
  527. case OP_RETURN: sReturn(sc); break;
  528. case OP_DUPLICATE: sDuplicate(sc); break;
  529. case OP_ALLOCATE_ARRAY: sAllocateArray(sc); break;
  530. case OP_ARRAY_LENGTH: sArrayLength(sc); break;
  531. case OP_REFERENCE_FROM_VAR: sReferenceFromVar(sc); break;
  532. case OP_REFERENCE_FROM_ARRAY: sReferenceFromArray(sc); break;
  533. case OP_DEREFERENCE: sDereferencePush(sc); break;
  534. }
  535. sCollectGarbage(sc);
  536. }
  537. static bool sHasData(Script* sc) {
  538. return sc->readIndex < sc->code->length;
  539. }
  540. Script* sInit(ByteCode* code) {
  541. Script* sc = malloc(sizeof(Script));
  542. sc->error[0] = '\0';
  543. sc->code = code;
  544. sc->readIndex = 0;
  545. sc->returnValue.type = OT_VOID;
  546. sc->stackIndex = 0;
  547. sc->stackVarIndex = 0;
  548. sc->line = 0;
  549. aInit(&sc->allocator);
  550. return sc;
  551. }
  552. void sDelete(Script* sc) {
  553. bcDelete(sc->code);
  554. aDelete(&sc->allocator);
  555. free(sc);
  556. }
  557. void sRun(Script* sc) {
  558. while(sHasData(sc)) {
  559. sConsumeInstruction(sc);
  560. if(sc->error[0] != '\0') {
  561. puts(sc->error);
  562. return;
  563. }
  564. }
  565. }
  566. void sSetPrinter(ObjectPrinter p) {
  567. printer = p;
  568. }
  569. static void sMark(Script* sc, Object* o) {
  570. if(o->type == OT_ARRAY) {
  571. Array* a = sc->allocator.data + o->data.intValue;
  572. a->marked = true;
  573. for(int i = 0; i < a->length; i++) {
  574. sMark(sc, a->data + i);
  575. }
  576. }
  577. }
  578. void sCollectGarbage(Script* sc) {
  579. aClearMarker(&sc->allocator);
  580. for(int i = 0; i < sc->stackIndex; i++) {
  581. sMark(sc, sc->stack + i);
  582. }
  583. aRemoveUnmarked(&sc->allocator);
  584. }
  585. static void sPrintInt(Script* sc, const char* msg) {
  586. int value = 0;
  587. sReadInt(sc, &value);
  588. printf("%s %d\n", msg, value);
  589. }
  590. static void sPrint2Int(Script* sc, const char* msg) {
  591. int a = 0;
  592. sReadInt(sc, &a);
  593. int b = 0;
  594. sReadInt(sc, &b);
  595. printf("%s %d %d\n", msg, a, b);
  596. }
  597. static void sPrintInt16(Script* sc, const char* msg) {
  598. int value = 0;
  599. sRead(sc, &value, 2);
  600. printf("%s %d\n", msg, value);
  601. }
  602. static void sPrintFloat(Script* sc, const char* msg) {
  603. float value = 0;
  604. sRead(sc, &value, sizeof(float));
  605. printf("%s %.2f\n", msg, value);
  606. }
  607. static void sPrintString(Script* sc, const char* msg) {
  608. int length = 0;
  609. sReadInt(sc, &length);
  610. char* s = (char*)(sc->code->code + sc->readIndex);
  611. sc->readIndex += length;
  612. printf("%s %d \"%s\"\n", msg, length, s);
  613. }
  614. void sPrintCode(Script* sc) {
  615. int oldRead = sc->readIndex;
  616. sc->readIndex = 0;
  617. while(sHasData(sc)) {
  618. printf(" %3d | ", sc->readIndex);
  619. switch(sReadOperation(sc)) {
  620. case OP_NOTHING: puts("Nothing"); break;
  621. case OP_PUSH_INT: sPrintInt(sc, "Push Int"); break;
  622. case OP_PUSH_FLOAT: sPrintFloat(sc, "Push Float"); break;
  623. case OP_PUSH_CONST_STRING: sPrintString(sc, "Push Const String"); break;
  624. case OP_PUSH_NULL: puts("Push null"); break;
  625. case OP_PUSH_TRUE: puts("Push true"); break;
  626. case OP_PUSH_FALSE: puts("Push false"); break;
  627. case OP_PUSH_VARS: sPrint2Int(sc, "Push Vars"); break;
  628. case OP_POP_VARS: sPrintInt(sc, "Pop Vars"); break;
  629. case OP_POP: puts("Pop"); break;
  630. case OP_SET: puts("Set"); break;
  631. case OP_PRE_INCREMENT: puts("Pre Increment"); break;
  632. case OP_POST_INCREMENT: puts("Post Increment"); break;
  633. case OP_PRE_DECREMENT: puts("Pre Decrement"); break;
  634. case OP_POST_DECREMENT: puts("Post Decrement"); break;
  635. case OP_ADD: puts("Add"); break;
  636. case OP_SUB: puts("Sub"); break;
  637. case OP_MUL: puts("Mul"); break;
  638. case OP_DIV: puts("Div"); break;
  639. case OP_MOD: puts("Mod"); break;
  640. case OP_INVERT_SIGN: puts("Invert Sign"); break;
  641. case OP_LESS: puts("Less"); break;
  642. case OP_GREATER: puts("Greater"); break;
  643. case OP_EQUAL: puts("Equal"); break;
  644. case OP_NOT: puts("Not"); break;
  645. case OP_AND: puts("And"); break;
  646. case OP_OR: puts("Or"); break;
  647. case OP_BIT_NOT: puts("Bit Not"); break;
  648. case OP_BIT_AND: puts("Bit And"); break;
  649. case OP_BIT_OR: puts("Bit Or"); break;
  650. case OP_BIT_XOR: puts("Bit Xor"); break;
  651. case OP_LEFT_SHIFT: puts("Left Shift"); break;
  652. case OP_RIGHT_SHIFT: puts("Right Shift"); break;
  653. case OP_PRINT: puts("Print"); break;
  654. case OP_LINE: sPrintInt16(sc, "------------ Line"); break;
  655. case OP_GOTO: sPrintInt(sc, "GoTo"); break;
  656. case OP_GOSUB: sPrint2Int(sc, "GoSub"); break;
  657. case OP_IF_GOTO: sPrintInt(sc, "If GoTo"); break;
  658. case OP_SET_RETURN: puts("Set Return"); break;
  659. case OP_RETURN: puts("Return"); break;
  660. case OP_DUPLICATE: puts("Duplicate"); break;
  661. case OP_ALLOCATE_ARRAY: puts("Allocate Array"); break;
  662. case OP_ARRAY_LENGTH: puts("Array Length"); break;
  663. case OP_REFERENCE_FROM_VAR: sPrintInt(sc, "Reference From Var"); break;
  664. case OP_REFERENCE_FROM_ARRAY: sPrintInt(sc, "Reference From Array"); break;
  665. case OP_DEREFERENCE: puts("Dereference"); break;
  666. }
  667. }
  668. sc->readIndex = oldRead;
  669. }