Script.c 21 KB

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