Script.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 bool sPushInt(Script* sc, int value) {
  65. Object o = {.type = OT_INT, .data.intValue = value};
  66. return sPush(sc, &o);
  67. }
  68. static void sPushFloat(Script* sc, float value) {
  69. Object o = {.type = OT_FLOAT, .data.floatValue = value};
  70. sPush(sc, &o);
  71. }
  72. static void sPushNull(Script* sc) {
  73. Object o = {.type = OT_NULL};
  74. sPush(sc, &o);
  75. }
  76. static void sPushBool(Script* sc, bool value) {
  77. Object o = {.type = OT_BOOL, .data.intValue = value};
  78. sPush(sc, &o);
  79. }
  80. static void sPushVars(Script* sc) {
  81. int vars = 0;
  82. int offset = 0;
  83. if(sReadInt(sc, &vars) && sReadInt(sc, &offset)) {
  84. int stackVarIndex = sc->stackVarIndex;
  85. sc->stackVarIndex = sc->stackIndex - offset;
  86. for(int i = 0; i < vars - offset; i++) {
  87. sPushNull(sc);
  88. }
  89. sPushInt(sc, stackVarIndex);
  90. }
  91. }
  92. static void sPopVars(Script* sc) {
  93. int value = 0;
  94. Object o;
  95. if(sReadInt(sc, &value) && !sPop(sc, &o)) {
  96. if(o.type != OT_INT) {
  97. sError(sc, "stack variable index has wrong type");
  98. return;
  99. }
  100. sc->stackVarIndex = o.data.intValue;
  101. if(sc->stackIndex < value) {
  102. sError(sc, "stack underflow on line %d", sc->line);
  103. } else {
  104. sc->stackIndex -= value;
  105. }
  106. }
  107. }
  108. static void sSet(Script* sc) {
  109. int value = 0;
  110. if(sReadInt(sc, &value)) {
  111. sPop(sc, sc->stack + value + sc->stackVarIndex);
  112. }
  113. }
  114. static void sGet(Script* sc) {
  115. int value = 0;
  116. if(sReadInt(sc, &value)) {
  117. sPush(sc, sc->stack + value + sc->stackVarIndex);
  118. }
  119. }
  120. static void sPushCodeInt(Script* sc) {
  121. int value = 0;
  122. if(sReadInt(sc, &value)) {
  123. sPushInt(sc, value);
  124. }
  125. }
  126. static void sPushCodeFloat(Script* sc) {
  127. float value = 0;
  128. if(sRead(sc, &value, sizeof(float))) {
  129. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  130. return;
  131. }
  132. sPushFloat(sc, value);
  133. }
  134. static bool sToFloat(Script* sc, Object* o, float* r) {
  135. if(o->type == OT_FLOAT) {
  136. *r = o->data.floatValue;
  137. return true;
  138. } else if(o->type == OT_INT) {
  139. *r = o->data.intValue;
  140. return true;
  141. }
  142. sError(sc, "object is not a number on line %d", sc->line);
  143. return false;
  144. }
  145. static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
  146. Object o[2];
  147. if(sPop(sc, o) || sPop(sc, o + 1)) {
  148. return;
  149. }
  150. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  151. sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  152. return;
  153. }
  154. float f[2];
  155. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  156. sPushFloat(sc, fFloat(f[0], f[1]));
  157. }
  158. }
  159. static int sIntAdd(int a, int b) {
  160. return a + b;
  161. }
  162. static int sIntMul(int a, int b) {
  163. return a * b;
  164. }
  165. static float sFloatAdd(float a, float b) {
  166. return a + b;
  167. }
  168. static float sFloatMul(float a, float b) {
  169. return a * b;
  170. }
  171. static void sPrint(Script* sc) {
  172. Object o;
  173. if(!sPop(sc, &o) && printer(&o)) {
  174. sError(sc, "cannot print given object on line %d", sc->line);
  175. }
  176. }
  177. static void sLine(Script* sc) {
  178. if(sRead(sc, &sc->line, 2)) {
  179. sError(sc, "line operation without a line near line %d", sc->line);
  180. }
  181. }
  182. static void sGoTo(Script* sc) {
  183. int gotoIndex;
  184. if(sReadInt(sc, &gotoIndex)) {
  185. sc->readIndex = gotoIndex;
  186. }
  187. }
  188. static void sGoSub(Script* sc) {
  189. int gotoIndex;
  190. int arguments;
  191. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments)) {
  192. int returnStackIndex = sc->stackIndex - arguments - 1;
  193. if(returnStackIndex < 0 || sc->stack[returnStackIndex].type != OT_INT) {
  194. sError(sc, "cannot find return address entry on stack on line %d", sc->line);
  195. return;
  196. }
  197. sc->stack[returnStackIndex].data.intValue = sc->readIndex;
  198. sc->readIndex = gotoIndex;
  199. }
  200. }
  201. static void sSetReturn(Script* sc) {
  202. sPop(sc, &sc->returnValue);
  203. }
  204. static void sReturn(Script* sc) {
  205. Object o;
  206. if(sPop(sc, &o)) {
  207. return;
  208. } else if(o.type != OT_INT) {
  209. sError(sc, "return address on stack is not an int");
  210. return;
  211. }
  212. sc->readIndex = o.data.intValue;
  213. if(sc->returnValue.type != OT_VOID) {
  214. sPush(sc, &sc->returnValue);
  215. sc->returnValue.type = OT_VOID;
  216. }
  217. }
  218. static void sConsumeInstruction(Script* sc) {
  219. switch(sReadOperation(sc)) {
  220. case OP_NOTHING: break;
  221. case OP_PUSH_INT: sPushCodeInt(sc); break;
  222. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  223. case OP_PUSH_NULL: sPushNull(sc); break;
  224. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  225. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  226. case OP_PUSH_VARS: sPushVars(sc); break;
  227. case OP_POP_VARS: sPopVars(sc); break;
  228. case OP_POP: sPopEmpty(sc); break;
  229. case OP_SET: sSet(sc); break;
  230. case OP_GET: sGet(sc); break;
  231. case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
  232. case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
  233. case OP_PRINT: sPrint(sc); break;
  234. case OP_LINE: sLine(sc); break;
  235. case OP_GOTO: sGoTo(sc); break;
  236. case OP_GOSUB: sGoSub(sc); break;
  237. case OP_SET_RETURN: sSetReturn(sc); break;
  238. case OP_RETURN: sReturn(sc); break;
  239. }
  240. }
  241. static bool sHasData(Script* sc) {
  242. return sc->readIndex < sc->code->length;
  243. }
  244. Script* sInit(ByteCode* code) {
  245. Script* sc = malloc(sizeof(Script));
  246. sc->error[0] = '\0';
  247. sc->code = code;
  248. sc->readIndex = 0;
  249. sc->returnValue.type = OT_VOID;
  250. sc->stackIndex = 0;
  251. sc->stackVarIndex = 0;
  252. sc->line = 0;
  253. return sc;
  254. }
  255. void sDelete(Script* sc) {
  256. bcDelete(sc->code);
  257. free(sc);
  258. }
  259. void sRun(Script* sc) {
  260. while(sHasData(sc)) {
  261. sConsumeInstruction(sc);
  262. if(sc->error[0] != '\0') {
  263. puts(sc->error);
  264. return;
  265. }
  266. }
  267. }
  268. void sSetPrinter(ObjectPrinter p) {
  269. printer = p;
  270. }
  271. static void sPrintInt(Script* sc, const char* msg) {
  272. int value = 0;
  273. sReadInt(sc, &value);
  274. printf("%s %d\n", msg, value);
  275. }
  276. static void sPrint2Int(Script* sc, const char* msg) {
  277. int a = 0;
  278. sReadInt(sc, &a);
  279. int b = 0;
  280. sReadInt(sc, &b);
  281. printf("%s %d %d\n", msg, a, b);
  282. }
  283. static void sPrintInt16(Script* sc, const char* msg) {
  284. int value = 0;
  285. sRead(sc, &value, 2);
  286. printf("%s %d\n", msg, value);
  287. }
  288. static void sPrintFloat(Script* sc, const char* msg) {
  289. float value = 0;
  290. sRead(sc, &value, sizeof(float));
  291. printf("%s %.2f\n", msg, value);
  292. }
  293. void sPrintCode(Script* sc) {
  294. int oldRead = sc->readIndex;
  295. sc->readIndex = 0;
  296. while(sHasData(sc)) {
  297. printf(" %3d | ", sc->readIndex);
  298. switch(sReadOperation(sc)) {
  299. case OP_NOTHING: puts("Nothing"); break;
  300. case OP_PUSH_INT: sPrintInt(sc, "Push Int"); break;
  301. case OP_PUSH_FLOAT: sPrintFloat(sc, "Push Float"); break;
  302. case OP_PUSH_NULL: puts("Push null"); break;
  303. case OP_PUSH_TRUE: puts("Push true"); break;
  304. case OP_PUSH_FALSE: puts("Push false"); break;
  305. case OP_PUSH_VARS: sPrint2Int(sc, "Push Vars"); break;
  306. case OP_POP_VARS: sPrintInt(sc, "Pop Vars"); break;
  307. case OP_POP: puts("Pop"); break;
  308. case OP_SET: sPrintInt(sc, "Set"); break;
  309. case OP_GET: sPrintInt(sc, "Get"); break;
  310. case OP_ADD: puts("Add"); break;
  311. case OP_MUL: puts("Mul"); break;
  312. case OP_PRINT: puts("Print"); break;
  313. case OP_LINE: sPrintInt16(sc, "------------ Line"); break;
  314. case OP_GOTO: sPrintInt(sc, "GoTo"); break;
  315. case OP_GOSUB: sPrint2Int(sc, "GoSub"); break;
  316. case OP_SET_RETURN: puts("Set Return"); break;
  317. case OP_RETURN: puts("Return"); break;
  318. }
  319. }
  320. sc->readIndex = oldRead;
  321. }