Script.c 13 KB

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