Script.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. #include <stdarg.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "utils/Functions.h"
  7. #include "vm/Operation.h"
  8. #include "vm/Script.h"
  9. void sError(Script* sc, const char* format, ...) {
  10. va_list args;
  11. va_start(args, format);
  12. vsnprintf(sc->error, SCRIPT_ERROR_SIZE, format, args);
  13. va_end(args);
  14. }
  15. static bool sRead(Script* sc, void* buffer, int length) {
  16. if(sc->readIndex + length > sc->code->length) {
  17. sError(sc, "cannot read expected %d bytes of data from bytecode");
  18. return false;
  19. }
  20. memcpy(buffer, sc->code->code + sc->readIndex, length);
  21. sc->readIndex += length;
  22. return true;
  23. }
  24. static Operation sReadOperation(Script* sc) {
  25. unsigned char c;
  26. if(sRead(sc, &c, 1)) {
  27. return c;
  28. }
  29. return OP_NOTHING;
  30. }
  31. static void* sReserve(Script* sc, int length) {
  32. if(sc->stackIndex + length > SCRIPT_STACK_SIZE) {
  33. sError(sc, "stack overflow");
  34. return NULL;
  35. }
  36. void* p = sc->stack + sc->stackIndex;
  37. sc->stackIndex += length;
  38. return p;
  39. }
  40. static bool sPush(Script* sc, const void* data, int length) {
  41. void* p = sReserve(sc, length);
  42. if(p != NULL) {
  43. memcpy(p, data, length);
  44. return true;
  45. }
  46. return false;
  47. }
  48. static const void* sFree(Script* sc, int length) {
  49. if(sc->stackIndex < length) {
  50. sError(sc, "stack underflow");
  51. return NULL;
  52. }
  53. sc->stackIndex -= length;
  54. return sc->stack + sc->stackIndex;
  55. }
  56. static bool sPop(Script* sc, void* data, int length) {
  57. const void* p = sFree(sc, length);
  58. if(p != NULL) {
  59. memcpy(data, p, length);
  60. return true;
  61. }
  62. return false;
  63. }
  64. static bool sPeek(Script* sc, void* data, int length) {
  65. if(sc->stackIndex < length) {
  66. sError(sc, "stack underflow");
  67. return false;
  68. }
  69. memcpy(data, sc->stack + (sc->stackIndex - length), length);
  70. return true;
  71. }
  72. #define POP_PUSH(type, Type) \
  73. bool sPop##Type(Script* sc, type* value) { \
  74. return sPop(sc, value, sizeof(type)); \
  75. } \
  76. bool sPush##Type(Script* sc, type value) { \
  77. return sPush(sc, &value, sizeof(type)); \
  78. }
  79. #define READ_POP_PUSH(type, Type) \
  80. static bool sRead##Type(Script* sc, type* i) { \
  81. return sRead(sc, i, sizeof(type)); \
  82. } \
  83. POP_PUSH(type, Type)
  84. #define PUSH_CONSTANT(type, Type) \
  85. { \
  86. type value; \
  87. if(sRead##Type(sc, &value)) { \
  88. sPush##Type(sc, value); \
  89. } \
  90. }
  91. #define ZERO_CHECK(name) \
  92. if(values[0] == 0) { \
  93. sError(sc, name " by 0"); \
  94. return; \
  95. }
  96. #define OP_BASE(type, Type, RType, op, check) \
  97. { \
  98. type values[2]; \
  99. if(sPop##Type(sc, values) && sPop##Type(sc, values + 1)) { \
  100. check; \
  101. sPush##RType(sc, values[1] op values[0]); \
  102. } \
  103. }
  104. #define CHECKED_NUMBER_OP(type, Type, op, check) \
  105. OP_BASE(type, Type, Type, op, check)
  106. #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, )
  107. #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Bool, op, )
  108. #define DIVISION(type, Type) \
  109. CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division"));
  110. #define MODULE(type, Type) \
  111. CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module"));
  112. READ_POP_PUSH(int32, Int32)
  113. READ_POP_PUSH(int64, Int64)
  114. READ_POP_PUSH(float, Float)
  115. POP_PUSH(bool, Bool)
  116. bool sPopPointer(Script* sc, Pointer* value) {
  117. return sPop(sc, value, sizeof(Pointer));
  118. }
  119. bool sPushPointer(Script* sc, Pointer* value) {
  120. return sPush(sc, value, sizeof(Pointer));
  121. }
  122. bool sPushNullPointer(Script* sc) {
  123. Pointer p = {-1, -1};
  124. return sPushPointer(sc, &p);
  125. }
  126. #define INVERT_SIGN(type, Type) \
  127. { \
  128. type value = 0; \
  129. if(sPop##Type(sc, &value)) { \
  130. sPush##Type(sc, -value); \
  131. } \
  132. }
  133. static void sReserveBytes(Script* sc) {
  134. int32 bytes = 0;
  135. int32 offset = 0;
  136. if(sReadInt32(sc, &bytes) && sReadInt32(sc, &offset)) {
  137. int32 oldIndex = sc->stackVarIndex;
  138. sc->stackVarIndex = sc->stackIndex - offset;
  139. sReserve(sc, bytes - offset);
  140. sPushInt32(sc, oldIndex);
  141. }
  142. }
  143. static int32 sGetTextLength(Script* sc, Pointer* p) {
  144. const int SIZE = sizeof(int32);
  145. int sizeAddress = -p->array;
  146. if(sizeAddress < 0 || sizeAddress + SIZE >= sc->code->length) {
  147. sError(sc, "invalid code array length address %d", sizeAddress);
  148. return -1;
  149. }
  150. int32 size;
  151. memcpy(&size, sc->code->code + sizeAddress, SIZE);
  152. return size;
  153. }
  154. static void* sCheckTextAddress(Script* sc, Pointer* p, int length) {
  155. const int SIZE = sizeof(int32);
  156. int address = p->offset + SIZE;
  157. if(address + length > sc->code->length) {
  158. sError(sc, "address %d is out of code bounds", address);
  159. return NULL;
  160. }
  161. int32 size = sGetTextLength(sc, p);
  162. if(size == -1) {
  163. return NULL;
  164. }
  165. int startAddress = -p->array + SIZE;
  166. int endAddress = startAddress + size * SIZE;
  167. if(address + length > endAddress || address < startAddress) {
  168. sError(sc, "invalid text address %d", (address - startAddress) / SIZE);
  169. return NULL;
  170. }
  171. return sc->code->code + address;
  172. }
  173. void* sCheckAddress(Script* sc, Pointer* p, int length) {
  174. if(p->array >= 0) {
  175. Array* a = asGet(&sc->arrays, p->array);
  176. if(a == NULL) {
  177. sError(sc, "invalid heap pointer");
  178. return NULL;
  179. } else if(p->offset < 0 || p->offset >= a->size) {
  180. sError(sc, "address %d is out of array bounds", p->offset);
  181. return NULL;
  182. }
  183. return ((char*)a->data) + p->offset;
  184. } else if(p->array == -1) {
  185. if(p->offset < 0 || p->offset + length > sc->stackIndex) {
  186. sError(sc, "address %d is out of stack bounds", p->offset);
  187. return NULL;
  188. }
  189. return sc->stack + p->offset;
  190. }
  191. return sCheckTextAddress(sc, p, length);
  192. }
  193. static void sNot(Script* sc) {
  194. bool value = false;
  195. if(sPopBool(sc, &value)) {
  196. sPushBool(sc, !value);
  197. }
  198. }
  199. static void sBitNotInt32(Script* sc) {
  200. int32 value = 0;
  201. if(sPopInt32(sc, &value)) {
  202. sPushInt32(sc, ~value);
  203. }
  204. }
  205. static void sBitNotInt64(Script* sc) {
  206. int64 value = 0;
  207. if(sPopInt64(sc, &value)) {
  208. sPushInt64(sc, ~value);
  209. }
  210. }
  211. static void sLine(Script* sc) {
  212. sRead(sc, &sc->line, 2);
  213. }
  214. static void sGoTo(Script* sc) {
  215. int32 gotoIndex;
  216. if(sReadInt32(sc, &gotoIndex)) {
  217. sc->readIndex = gotoIndex;
  218. }
  219. }
  220. static void sGoSub(Script* sc) {
  221. int32 gotoIndex;
  222. int32 offset;
  223. if(sReadInt32(sc, &gotoIndex) && sReadInt32(sc, &offset)) {
  224. Pointer p = {.array = -1,
  225. .offset = sc->stackIndex - offset - sizeof(int32)};
  226. void* dest = sCheckAddress(sc, &p, sizeof(int32));
  227. if(dest != NULL) {
  228. memcpy(dest, &sc->readIndex, sizeof(int32));
  229. sc->readIndex = gotoIndex;
  230. }
  231. }
  232. }
  233. static void sReturn(Script* sc) {
  234. int32 bytes = 0;
  235. int32 varIndex = 0;
  236. if(sReadInt32(sc, &bytes) && sPopInt32(sc, &varIndex)) {
  237. sc->stackVarIndex = varIndex;
  238. sFree(sc, bytes);
  239. if(!sPopInt32(sc, &sc->readIndex) || sc->readIndex < 0) {
  240. sError(sc, "read index is corrupt");
  241. }
  242. }
  243. }
  244. static void sReturnPointer(Script* sc) {
  245. Pointer p;
  246. if(sPopPointer(sc, &p)) {
  247. sReturn(sc);
  248. sPushPointer(sc, &p);
  249. }
  250. }
  251. #define RETURN(type, Type) \
  252. { \
  253. type value; \
  254. if(sPop##Type(sc, &value)) { \
  255. sReturn(sc); \
  256. sPush##Type(sc, value); \
  257. } \
  258. }
  259. static void sIfGoTo(Script* sc) {
  260. int32 gotoIndex = 0;
  261. bool value = false;
  262. if(sReadInt32(sc, &gotoIndex) && sPopBool(sc, &value) && !value) {
  263. sc->readIndex = gotoIndex;
  264. }
  265. }
  266. static void sPeekFalseGoTo(Script* sc) {
  267. int32 gotoIndex = 0;
  268. bool value = false;
  269. if(sReadInt32(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) &&
  270. !value) {
  271. sc->readIndex = gotoIndex;
  272. }
  273. }
  274. static void sPeekTrueGoTo(Script* sc) {
  275. int32 gotoIndex = 0;
  276. bool value = false;
  277. if(sReadInt32(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && value) {
  278. sc->readIndex = gotoIndex;
  279. }
  280. }
  281. static void sNewArray(Script* sc) {
  282. int32 length = 0;
  283. int32 size = 0;
  284. if(sReadInt32(sc, &size) && sPopInt32(sc, &length)) {
  285. Pointer p = {.array = asAllocate(&sc->arrays, size, length),
  286. .offset = 0};
  287. if(p.array == -1) {
  288. sError(sc, "out of memory");
  289. } else if(p.array == -2) {
  290. sError(sc, "bad allocation");
  291. } else {
  292. sPushPointer(sc, &p);
  293. }
  294. }
  295. }
  296. static void sDeleteArray(Script* sc) {
  297. Pointer p;
  298. if(sPopPointer(sc, &p)) {
  299. if(p.offset != 0) {
  300. sError(sc, "delete of array with offset: %d", p.offset);
  301. return;
  302. }
  303. Array* a = asGet(&sc->arrays, p.array);
  304. if(a == NULL) {
  305. sError(sc, "delete of invalid array");
  306. return;
  307. }
  308. asDeleteArray(&sc->arrays, a, p.array);
  309. }
  310. }
  311. bool sGetPointerLength(Script* sc, Pointer* p, int32* length) {
  312. if(p->array == -1) {
  313. *length = p->offset >= 0;
  314. return false;
  315. } else if(p->array <= -1) {
  316. int32 l = sGetTextLength(sc, p);
  317. if(l != -1) {
  318. *length = l;
  319. return false;
  320. }
  321. return true;
  322. }
  323. Array* a = asGet(&sc->arrays, p->array);
  324. if(a == NULL) {
  325. sError(sc, "invalid heap pointer %d", p->array);
  326. return true;
  327. }
  328. *length = a->length;
  329. return false;
  330. }
  331. static void sLength(Script* sc) {
  332. Pointer p;
  333. int32 length;
  334. if(sPopPointer(sc, &p) && !sGetPointerLength(sc, &p, &length)) {
  335. sPushInt32(sc, length);
  336. }
  337. }
  338. static void sDereference(Script* sc) {
  339. int32 address = 0;
  340. if(sReadInt32(sc, &address)) {
  341. Pointer p = {.array = -1, .offset = address + sc->stackVarIndex};
  342. sPushPointer(sc, &p);
  343. }
  344. }
  345. static void sLoad(Script* sc, int length) {
  346. Pointer p;
  347. if(sPopPointer(sc, &p)) {
  348. void* src = sCheckAddress(sc, &p, length);
  349. if(src != NULL) {
  350. sPush(sc, src, length);
  351. }
  352. }
  353. }
  354. static void sDuplicateReference(Script* sc) {
  355. Pointer p;
  356. if(sPeek(sc, &p, sizeof(Pointer))) {
  357. sPushPointer(sc, &p);
  358. }
  359. }
  360. static void sAddReference(Script* sc) {
  361. int32 size = 0;
  362. int32 add = 0;
  363. Pointer p;
  364. if(sReadInt32(sc, &size) && sPopInt32(sc, &add) && sPopPointer(sc, &p)) {
  365. p.offset += add * size;
  366. sPushPointer(sc, &p);
  367. }
  368. }
  369. static void sLoadSize(Script* sc) {
  370. int32 size = 0;
  371. Pointer p;
  372. if(sReadInt32(sc, &size) && sPopPointer(sc, &p)) {
  373. void* src = sCheckAddress(sc, &p, size);
  374. if(src != NULL) {
  375. sPush(sc, src, size);
  376. }
  377. }
  378. }
  379. static void sStore(Script* sc, int length) {
  380. int index = sc->stackIndex - sizeof(Pointer) - length;
  381. if(index < 0) {
  382. sError(sc, "stack underflow");
  383. return;
  384. }
  385. Pointer p;
  386. memcpy(&p, sc->stack + index, sizeof(Pointer));
  387. void* dest = sCheckAddress(sc, &p, length);
  388. if(dest != NULL) {
  389. sPop(sc, dest, length);
  390. sc->stackIndex -= sizeof(Pointer);
  391. }
  392. }
  393. static void sEqualPointer(Script* sc, bool wanted) {
  394. Pointer a;
  395. Pointer b;
  396. if(sPopPointer(sc, &a) && sPopPointer(sc, &b)) {
  397. sPushBool(sc, (a.array == b.array && a.offset == b.offset) == wanted);
  398. }
  399. }
  400. static void sCall(Script* sc) {
  401. int32 function = 0;
  402. if(sReadInt32(sc, &function) && gfsCall(sc, function)) {
  403. sError(sc, "invalid function call");
  404. }
  405. }
  406. static void sPushText(Script* sc) {
  407. int address = sc->readIndex;
  408. int32 length;
  409. if(sReadInt32(sc, &length)) {
  410. Pointer p = {.array = -address, .offset = address};
  411. sPushPointer(sc, &p);
  412. sc->readIndex += sizeof(int32) * length;
  413. }
  414. }
  415. #define CHANGE_OP(type, op) \
  416. { \
  417. char c = 0; \
  418. Pointer p; \
  419. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) { \
  420. void* data = sCheckAddress(sc, &p, sizeof(type)); \
  421. if(data != NULL) { \
  422. type current; \
  423. memcpy(&current, data, sizeof(type)); \
  424. op \
  425. } \
  426. } \
  427. }
  428. #define PUSH_PRE_CHANGE(Type, type) \
  429. CHANGE_OP(type, current += c; sPush##Type(sc, current); \
  430. memcpy(data, &current, sizeof(type));)
  431. #define PUSH_POST_CHANGE(Type, type) \
  432. CHANGE_OP(type, sPush##Type(sc, current); current += c; \
  433. memcpy(data, &current, sizeof(type));)
  434. #define CHANGE(type) \
  435. CHANGE_OP(type, current += c; memcpy(data, &current, sizeof(type));)
  436. #define CAST(From, from, To) \
  437. { \
  438. from value; \
  439. if(sPop##From(sc, &value)) { \
  440. sPush##To(sc, value); \
  441. } \
  442. }
  443. #define CASE_CAST(TYPE1, Type1, type1, TYPE2, Type2, type2) \
  444. case OP_##TYPE1##_TO_##TYPE2: CAST(Type1, type1, Type2); break; \
  445. case OP_##TYPE2##_TO_##TYPE1: \
  446. CAST(Type2, type2, Type1); \
  447. break;
  448. #define CASE_CHANGE(TYPE, Type, type) \
  449. case OP_PUSH_PRE_CHANGE_##TYPE: PUSH_PRE_CHANGE(Type, type); break; \
  450. case OP_PUSH_POST_CHANGE_##TYPE: PUSH_POST_CHANGE(Type, type); break; \
  451. case OP_CHANGE_##TYPE: \
  452. CHANGE(type); \
  453. break;
  454. #define CASE_INTEGRAL_OP(name, op) \
  455. case OP_##name##_INT32: NUMBER_OP(int32, Int32, op); break; \
  456. case OP_##name##_INT64: \
  457. NUMBER_OP(int64, Int64, op); \
  458. break;
  459. #define CASE_NUMBER_OP(name, op) \
  460. CASE_INTEGRAL_OP(name, op) \
  461. case OP_##name##_FLOAT: \
  462. NUMBER_OP(float, Float, op); \
  463. break;
  464. #define CASE_BOOL_OP(name, op) \
  465. case OP_##name##_INT32: BOOL_OP(int32, Int32, op); break; \
  466. case OP_##name##_INT64: BOOL_OP(int64, Int64, op); break; \
  467. case OP_##name##_FLOAT: \
  468. BOOL_OP(float, Float, op); \
  469. break;
  470. #define CASE_TYPE(TYPE, Type, type) \
  471. case OP_STORE_##TYPE: sStore(sc, sizeof(type)); break; \
  472. case OP_RETURN_##TYPE: RETURN(type, Type); break; \
  473. case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break; \
  474. case OP_NOT_EQUAL_##TYPE: BOOL_OP(type, Type, !=); break; \
  475. case OP_LOAD_##TYPE: sLoad(sc, sizeof(type)); break;
  476. static void sConsumeInstruction(Script* sc) {
  477. switch(sReadOperation(sc)) {
  478. CASE_NUMBER_OP(ADD, +);
  479. CASE_NUMBER_OP(SUB, -);
  480. CASE_NUMBER_OP(MUL, *);
  481. CASE_BOOL_OP(LESS, <);
  482. CASE_BOOL_OP(LESS_EQUAL, <=);
  483. CASE_BOOL_OP(GREATER, >);
  484. CASE_BOOL_OP(GREATER_EQUAL, >=);
  485. CASE_TYPE(INT32, Int32, int32);
  486. CASE_TYPE(INT64, Int64, int64);
  487. CASE_TYPE(BOOL, Bool, bool);
  488. CASE_TYPE(FLOAT, Float, float);
  489. CASE_CHANGE(INT32, Int32, int32);
  490. CASE_CHANGE(INT64, Int64, int64);
  491. CASE_INTEGRAL_OP(BIT_AND, &);
  492. CASE_INTEGRAL_OP(BIT_OR, |);
  493. CASE_INTEGRAL_OP(BIT_XOR, ^);
  494. CASE_INTEGRAL_OP(LEFT_SHIFT, <<);
  495. CASE_INTEGRAL_OP(RIGHT_SHIFT, >>);
  496. CASE_CAST(INT32, Int32, int32, FLOAT, Float, float);
  497. CASE_CAST(INT32, Int32, int32, INT64, Int64, int64);
  498. CASE_CAST(INT64, Int64, int64, FLOAT, Float, float);
  499. case OP_NOTHING: break;
  500. case OP_PUSH_INT32: PUSH_CONSTANT(int32, Int32); break;
  501. case OP_PUSH_INT64: PUSH_CONSTANT(int64, Int64); break;
  502. case OP_PUSH_FLOAT: PUSH_CONSTANT(float, Float); break;
  503. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  504. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  505. case OP_PUSH_NULLPTR: sPushNullPointer(sc); break;
  506. case OP_PUSH_TEXT: sPushText(sc); break;
  507. case OP_DIV_INT32: DIVISION(int32, Int32); break;
  508. case OP_DIV_INT64: DIVISION(int64, Int64); break;
  509. case OP_DIV_FLOAT: DIVISION(float, Float); break;
  510. case OP_MOD_INT32: MODULE(int32, Int32); break;
  511. case OP_MOD_INT64: MODULE(int64, Int64); break;
  512. case OP_INVERT_SIGN_INT32: INVERT_SIGN(int32, Int32); break;
  513. case OP_INVERT_SIGN_INT64: INVERT_SIGN(int64, Int64); break;
  514. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
  515. case OP_NOT: sNot(sc); break;
  516. case OP_AND: BOOL_OP(bool, Bool, &&); break;
  517. case OP_OR: BOOL_OP(bool, Bool, ||); break;
  518. case OP_BIT_NOT_INT32: sBitNotInt32(sc); break;
  519. case OP_BIT_NOT_INT64: sBitNotInt64(sc); break;
  520. case OP_LINE: sLine(sc); break;
  521. case OP_GOTO: sGoTo(sc); break;
  522. case OP_IF_GOTO: sIfGoTo(sc); break;
  523. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
  524. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
  525. case OP_GOSUB: sGoSub(sc); break;
  526. case OP_RETURN: sReturn(sc); break;
  527. case OP_RETURN_POINTER: sReturnPointer(sc); break;
  528. case OP_RESERVE: sReserveBytes(sc); break;
  529. case OP_DEREFERENCE_VAR: sDereference(sc); break;
  530. case OP_REFERENCE: sLoad(sc, sizeof(Pointer)); break;
  531. case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); break;
  532. case OP_ADD_REFERENCE: sAddReference(sc); break;
  533. case OP_LOAD: sLoadSize(sc); break;
  534. case OP_NEW: sNewArray(sc); break;
  535. case OP_DELETE: sDeleteArray(sc); break;
  536. case OP_LENGTH: sLength(sc); break;
  537. case OP_STORE_POINTER: sStore(sc, sizeof(Pointer)); break;
  538. case OP_EQUAL_POINTER: sEqualPointer(sc, true); break;
  539. case OP_NOT_EQUAL_POINTER: sEqualPointer(sc, false); break;
  540. case OP_CALL: sCall(sc); break;
  541. }
  542. }
  543. static bool sHasData(Script* sc) {
  544. return sc->readIndex < sc->code->length;
  545. }
  546. Script* sInit(ByteCode* code) {
  547. Script* sc = malloc(sizeof(Script));
  548. sc->error[0] = '\0';
  549. sc->code = code;
  550. sc->readIndex = 0;
  551. sc->stackIndex = 0;
  552. sc->stackVarIndex = 0;
  553. sc->line = 0;
  554. asInit(&sc->arrays);
  555. return sc;
  556. }
  557. void sDelete(Script* sc) {
  558. bcDelete(sc->code);
  559. asDelete(&sc->arrays);
  560. free(sc);
  561. }
  562. void sRun(Script* sc) {
  563. while(sHasData(sc)) {
  564. sConsumeInstruction(sc);
  565. if(sc->error[0] != '\0') {
  566. puts("error:");
  567. printf(" - info: %s\n", sc->error);
  568. printf(" - line: %d\n", sc->line);
  569. return;
  570. }
  571. }
  572. }