Script.c 17 KB

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