Script.c 9.4 KB

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