Script.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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_NULL: printf("null\n"); return false;
  19. case OT_BOOL: printf(o->data.intValue ? "true\n" : "false\n"); return false;
  20. case OT_VOID: printf("void\n"); return false;
  21. }
  22. return true;
  23. }
  24. static ObjectPrinter printer = sPrinter;
  25. static bool sRead(Script* sc, void* buffer, int length) {
  26. if(sc->readIndex + length > sc->code->length) {
  27. sError(sc, "cannot read expected %d bytes of data from bytecode on line %d", sc->line);
  28. return true;
  29. }
  30. memcpy(buffer, sc->code->code + sc->readIndex, length);
  31. sc->readIndex += length;
  32. return false;
  33. }
  34. static Operation sReadOperation(Script* sc) {
  35. unsigned char c;
  36. if(sRead(sc, &c, 1)) {
  37. return OP_NOTHING;
  38. }
  39. return c;
  40. }
  41. static bool sReadInt(Script* sc, int* i) {
  42. return !sRead(sc, i, sizeof(int));
  43. }
  44. static bool sPush(Script* sc, Object* o) {
  45. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  46. sError(sc, "stack overflow on line %d", sc->line);
  47. return false;
  48. }
  49. sc->stack[sc->stackIndex++] = *o;
  50. return true;
  51. }
  52. static bool sPop(Script* sc, Object* o) {
  53. if(sc->stackIndex <= 0) {
  54. sError(sc, "stack underflow on line %d", sc->line);
  55. return true;
  56. }
  57. *o = sc->stack[--sc->stackIndex];
  58. return false;
  59. }
  60. static void sPopEmpty(Script* sc) {
  61. Object o;
  62. sPop(sc, &o);
  63. }
  64. static Object* sPeek(Script* sc) {
  65. if(sc->stackIndex <= 0) {
  66. sError(sc, "stack underflow on line %d", sc->line);
  67. return NULL;
  68. }
  69. return sc->stack + (sc->stackIndex - 1);
  70. }
  71. static bool sPushInt(Script* sc, int value) {
  72. Object o = {.type = OT_INT, .data.intValue = value};
  73. return sPush(sc, &o);
  74. }
  75. static void sPushFloat(Script* sc, float value) {
  76. Object o = {.type = OT_FLOAT, .data.floatValue = value};
  77. sPush(sc, &o);
  78. }
  79. static void sPushNull(Script* sc) {
  80. Object o = {.type = OT_NULL};
  81. sPush(sc, &o);
  82. }
  83. static void sPushBool(Script* sc, bool value) {
  84. Object o = {.type = OT_BOOL, .data.intValue = value};
  85. sPush(sc, &o);
  86. }
  87. static void sPushVars(Script* sc) {
  88. int vars = 0;
  89. int offset = 0;
  90. if(sReadInt(sc, &vars) && sReadInt(sc, &offset)) {
  91. int stackVarIndex = sc->stackVarIndex;
  92. sc->stackVarIndex = sc->stackIndex - offset;
  93. for(int i = 0; i < vars - offset; i++) {
  94. sPushNull(sc);
  95. }
  96. sPushInt(sc, stackVarIndex);
  97. }
  98. }
  99. static void sPopVars(Script* sc) {
  100. int value = 0;
  101. Object o;
  102. if(sReadInt(sc, &value) && !sPop(sc, &o)) {
  103. if(o.type != OT_INT) {
  104. sError(sc, "stack variable index has wrong type");
  105. return;
  106. }
  107. sc->stackVarIndex = o.data.intValue;
  108. if(sc->stackIndex < value) {
  109. sError(sc, "stack underflow on line %d", sc->line);
  110. } else {
  111. sc->stackIndex -= value;
  112. }
  113. }
  114. }
  115. static void sSet(Script* sc) {
  116. int value = 0;
  117. if(sReadInt(sc, &value)) {
  118. sPop(sc, sc->stack + value + sc->stackVarIndex);
  119. }
  120. }
  121. static void sGet(Script* sc) {
  122. int value = 0;
  123. if(sReadInt(sc, &value)) {
  124. sPush(sc, sc->stack + value + sc->stackVarIndex);
  125. }
  126. }
  127. static void sPushCodeInt(Script* sc) {
  128. int value = 0;
  129. if(sReadInt(sc, &value)) {
  130. sPushInt(sc, value);
  131. }
  132. }
  133. static void sPushCodeFloat(Script* sc) {
  134. float value = 0;
  135. if(sRead(sc, &value, sizeof(float))) {
  136. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  137. return;
  138. }
  139. sPushFloat(sc, value);
  140. }
  141. static bool sToFloat(Script* sc, Object* o, float* r) {
  142. if(o->type == OT_FLOAT) {
  143. *r = o->data.floatValue;
  144. return true;
  145. } else if(o->type == OT_INT) {
  146. *r = o->data.intValue;
  147. return true;
  148. }
  149. sError(sc, "object is not a number on line %d", sc->line);
  150. return false;
  151. }
  152. static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
  153. Object o[2];
  154. if(sPop(sc, o) || sPop(sc, o + 1)) {
  155. return;
  156. }
  157. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  158. sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  159. return;
  160. }
  161. float f[2];
  162. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  163. sPushFloat(sc, fFloat(f[0], f[1]));
  164. }
  165. }
  166. static int sIntAdd(int a, int b) {
  167. return a + b;
  168. }
  169. static int sIntSub(int a, int b) {
  170. return b - a;
  171. }
  172. static int sIntMul(int a, int b) {
  173. return a * b;
  174. }
  175. static float sFloatAdd(float a, float b) {
  176. return a + b;
  177. }
  178. static float sFloatSub(float a, float b) {
  179. return b - a;
  180. }
  181. static float sFloatMul(float a, float b) {
  182. return a * b;
  183. }
  184. static void sBoolBinary(Script* sc, bool (*fInt)(int, int), bool (*fFloat)(float, float)) {
  185. Object o[2];
  186. if(sPop(sc, o) || sPop(sc, o + 1)) {
  187. return;
  188. }
  189. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  190. sPushBool(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  191. return;
  192. }
  193. float f[2];
  194. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  195. sPushBool(sc, fFloat(f[0], f[1]));
  196. }
  197. }
  198. static bool sIntLess(int a, int b) {
  199. return b < a;
  200. }
  201. static bool sIntGreater(int a, int b) {
  202. return b > a;
  203. }
  204. static bool sFloatLess(float a, float b) {
  205. return b < a;
  206. }
  207. static bool sFloatGreater(float a, float b) {
  208. return b > a;
  209. }
  210. static void sEqual(Script* sc) {
  211. Object o[2];
  212. if(sPop(sc, o) || sPop(sc, o + 1)) {
  213. return;
  214. }
  215. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  216. sPushBool(sc, o[0].data.intValue == o[1].data.intValue);
  217. } else if(o[0].type == OT_INT && o[1].type == OT_FLOAT) {
  218. sPushBool(sc, o[0].data.intValue == o[1].data.floatValue);
  219. } else if(o[0].type == OT_FLOAT && o[1].type == OT_INT) {
  220. sPushBool(sc, o[0].data.floatValue == o[1].data.intValue);
  221. } else if(o[0].type == OT_FLOAT && o[1].type == OT_FLOAT) {
  222. sPushBool(sc, o[0].data.floatValue == o[1].data.floatValue);
  223. } else if(o[0].type == OT_BOOL && o[1].type == OT_BOOL) {
  224. sPushBool(sc, o[0].data.intValue == o[1].data.intValue);
  225. } else if(o[0].type == OT_NULL && o[1].type == OT_NULL) {
  226. sPushBool(sc, true);
  227. } else {
  228. sError(sc, "object types do not match on line %d", sc->line);
  229. }
  230. }
  231. static void sNot(Script* sc) {
  232. Object* o = sPeek(sc);
  233. if(o == NULL) {
  234. return;
  235. } else if(o->type != OT_BOOL) {
  236. sError(sc, "object is not a bool on line %d", sc->line);
  237. return;
  238. }
  239. o->data.intValue = !o->data.intValue;
  240. }
  241. static void sPrint(Script* sc) {
  242. Object o;
  243. if(!sPop(sc, &o) && printer(&o)) {
  244. sError(sc, "cannot print given object on line %d", sc->line);
  245. }
  246. }
  247. static void sLine(Script* sc) {
  248. if(sRead(sc, &sc->line, 2)) {
  249. sError(sc, "line operation without a line near line %d", sc->line);
  250. }
  251. }
  252. static void sGoTo(Script* sc) {
  253. int gotoIndex;
  254. if(sReadInt(sc, &gotoIndex)) {
  255. sc->readIndex = gotoIndex;
  256. }
  257. }
  258. static void sGoSub(Script* sc) {
  259. int gotoIndex;
  260. int arguments;
  261. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments)) {
  262. int returnStackIndex = sc->stackIndex - arguments - 1;
  263. if(returnStackIndex < 0 || sc->stack[returnStackIndex].type != OT_INT) {
  264. sError(sc, "cannot find return address entry on stack on line %d", sc->line);
  265. return;
  266. }
  267. sc->stack[returnStackIndex].data.intValue = sc->readIndex;
  268. sc->readIndex = gotoIndex;
  269. }
  270. }
  271. static void sIfGoTo(Script* sc) {
  272. int gotoIndex;
  273. Object o;
  274. if(sReadInt(sc, &gotoIndex) && !sPop(sc, &o)) {
  275. if(o.type != OT_BOOL) {
  276. sError(sc, "object is not a bool on line %d", sc->line);
  277. } else if(!o.data.intValue) {
  278. sc->readIndex = gotoIndex;
  279. }
  280. }
  281. }
  282. static void sSetReturn(Script* sc) {
  283. sPop(sc, &sc->returnValue);
  284. }
  285. static void sReturn(Script* sc) {
  286. Object o;
  287. if(sPop(sc, &o)) {
  288. return;
  289. } else if(o.type != OT_INT) {
  290. sError(sc, "return address on stack is not an int");
  291. return;
  292. }
  293. sc->readIndex = o.data.intValue;
  294. if(sc->returnValue.type != OT_VOID) {
  295. sPush(sc, &sc->returnValue);
  296. sc->returnValue.type = OT_VOID;
  297. }
  298. }
  299. static void sConsumeInstruction(Script* sc) {
  300. switch(sReadOperation(sc)) {
  301. case OP_NOTHING: break;
  302. case OP_PUSH_INT: sPushCodeInt(sc); break;
  303. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  304. case OP_PUSH_NULL: sPushNull(sc); break;
  305. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  306. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  307. case OP_PUSH_VARS: sPushVars(sc); break;
  308. case OP_POP_VARS: sPopVars(sc); break;
  309. case OP_POP: sPopEmpty(sc); break;
  310. case OP_SET: sSet(sc); break;
  311. case OP_GET: sGet(sc); break;
  312. case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
  313. case OP_SUB: sIntBinary(sc, sIntSub, sFloatSub); break;
  314. case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
  315. case OP_LESS: sBoolBinary(sc, sIntLess, sFloatLess); break;
  316. case OP_GREATER: sBoolBinary(sc, sIntGreater, sFloatGreater); break;
  317. case OP_EQUAL: sEqual(sc); break;
  318. case OP_NOT: sNot(sc); break;
  319. case OP_PRINT: sPrint(sc); break;
  320. case OP_LINE: sLine(sc); break;
  321. case OP_GOTO: sGoTo(sc); break;
  322. case OP_GOSUB: sGoSub(sc); break;
  323. case OP_IF_GOTO: sIfGoTo(sc); break;
  324. case OP_SET_RETURN: sSetReturn(sc); break;
  325. case OP_RETURN: sReturn(sc); break;
  326. }
  327. }
  328. static bool sHasData(Script* sc) {
  329. return sc->readIndex < sc->code->length;
  330. }
  331. Script* sInit(ByteCode* code) {
  332. Script* sc = malloc(sizeof(Script));
  333. sc->error[0] = '\0';
  334. sc->code = code;
  335. sc->readIndex = 0;
  336. sc->returnValue.type = OT_VOID;
  337. sc->stackIndex = 0;
  338. sc->stackVarIndex = 0;
  339. sc->line = 0;
  340. return sc;
  341. }
  342. void sDelete(Script* sc) {
  343. bcDelete(sc->code);
  344. free(sc);
  345. }
  346. void sRun(Script* sc) {
  347. while(sHasData(sc)) {
  348. sConsumeInstruction(sc);
  349. if(sc->error[0] != '\0') {
  350. puts(sc->error);
  351. return;
  352. }
  353. }
  354. }
  355. void sSetPrinter(ObjectPrinter p) {
  356. printer = p;
  357. }
  358. static void sPrintInt(Script* sc, const char* msg) {
  359. int value = 0;
  360. sReadInt(sc, &value);
  361. printf("%s %d\n", msg, value);
  362. }
  363. static void sPrint2Int(Script* sc, const char* msg) {
  364. int a = 0;
  365. sReadInt(sc, &a);
  366. int b = 0;
  367. sReadInt(sc, &b);
  368. printf("%s %d %d\n", msg, a, b);
  369. }
  370. static void sPrintInt16(Script* sc, const char* msg) {
  371. int value = 0;
  372. sRead(sc, &value, 2);
  373. printf("%s %d\n", msg, value);
  374. }
  375. static void sPrintFloat(Script* sc, const char* msg) {
  376. float value = 0;
  377. sRead(sc, &value, sizeof(float));
  378. printf("%s %.2f\n", msg, value);
  379. }
  380. void sPrintCode(Script* sc) {
  381. int oldRead = sc->readIndex;
  382. sc->readIndex = 0;
  383. while(sHasData(sc)) {
  384. printf(" %3d | ", sc->readIndex);
  385. switch(sReadOperation(sc)) {
  386. case OP_NOTHING: puts("Nothing"); break;
  387. case OP_PUSH_INT: sPrintInt(sc, "Push Int"); break;
  388. case OP_PUSH_FLOAT: sPrintFloat(sc, "Push Float"); break;
  389. case OP_PUSH_NULL: puts("Push null"); break;
  390. case OP_PUSH_TRUE: puts("Push true"); break;
  391. case OP_PUSH_FALSE: puts("Push false"); break;
  392. case OP_PUSH_VARS: sPrint2Int(sc, "Push Vars"); break;
  393. case OP_POP_VARS: sPrintInt(sc, "Pop Vars"); break;
  394. case OP_POP: puts("Pop"); break;
  395. case OP_SET: sPrintInt(sc, "Set"); break;
  396. case OP_GET: sPrintInt(sc, "Get"); break;
  397. case OP_ADD: puts("Add"); break;
  398. case OP_SUB: puts("Sub"); break;
  399. case OP_MUL: puts("Mul"); break;
  400. case OP_LESS: puts("Less"); break;
  401. case OP_GREATER: puts("Greater"); break;
  402. case OP_EQUAL: puts("Equal"); break;
  403. case OP_NOT: puts("Not"); break;
  404. case OP_PRINT: puts("Print"); break;
  405. case OP_LINE: sPrintInt16(sc, "------------ Line"); break;
  406. case OP_GOTO: sPrintInt(sc, "GoTo"); break;
  407. case OP_GOSUB: sPrint2Int(sc, "GoSub"); break;
  408. case OP_IF_GOTO: sPrintInt(sc, "If GoTo"); break;
  409. case OP_SET_RETURN: puts("Set Return"); break;
  410. case OP_RETURN: puts("Return"); break;
  411. }
  412. }
  413. sc->readIndex = oldRead;
  414. }