Script.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 sIntSub(int a, int b) {
  163. return b - a;
  164. }
  165. static int sIntMul(int a, int b) {
  166. return a * b;
  167. }
  168. static float sFloatAdd(float a, float b) {
  169. return a + b;
  170. }
  171. static float sFloatSub(float a, float b) {
  172. return b - a;
  173. }
  174. static float sFloatMul(float a, float b) {
  175. return a * b;
  176. }
  177. static void sBoolBinary(Script* sc, bool (*fInt)(int, int), bool (*fFloat)(float, float)) {
  178. Object o[2];
  179. if(sPop(sc, o) || sPop(sc, o + 1)) {
  180. return;
  181. }
  182. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  183. sPushBool(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  184. return;
  185. }
  186. float f[2];
  187. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  188. sPushBool(sc, fFloat(f[0], f[1]));
  189. }
  190. }
  191. static bool sIntLess(int a, int b) {
  192. return b < a;
  193. }
  194. static bool sFloatLess(float a, float b) {
  195. return b < a;
  196. }
  197. static void sPrint(Script* sc) {
  198. Object o;
  199. if(!sPop(sc, &o) && printer(&o)) {
  200. sError(sc, "cannot print given object on line %d", sc->line);
  201. }
  202. }
  203. static void sLine(Script* sc) {
  204. if(sRead(sc, &sc->line, 2)) {
  205. sError(sc, "line operation without a line near line %d", sc->line);
  206. }
  207. }
  208. static void sGoTo(Script* sc) {
  209. int gotoIndex;
  210. if(sReadInt(sc, &gotoIndex)) {
  211. sc->readIndex = gotoIndex;
  212. }
  213. }
  214. static void sGoSub(Script* sc) {
  215. int gotoIndex;
  216. int arguments;
  217. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments)) {
  218. int returnStackIndex = sc->stackIndex - arguments - 1;
  219. if(returnStackIndex < 0 || sc->stack[returnStackIndex].type != OT_INT) {
  220. sError(sc, "cannot find return address entry on stack on line %d", sc->line);
  221. return;
  222. }
  223. sc->stack[returnStackIndex].data.intValue = sc->readIndex;
  224. sc->readIndex = gotoIndex;
  225. }
  226. }
  227. static void sIfGoTo(Script* sc) {
  228. int gotoIndex;
  229. Object o;
  230. if(sReadInt(sc, &gotoIndex) && !sPop(sc, &o)) {
  231. if(o.type != OT_BOOL) {
  232. sError(sc, "object is not a bool on line %d", sc->line);
  233. } else if(!o.data.intValue) {
  234. sc->readIndex = gotoIndex;
  235. }
  236. }
  237. }
  238. static void sSetReturn(Script* sc) {
  239. sPop(sc, &sc->returnValue);
  240. }
  241. static void sReturn(Script* sc) {
  242. Object o;
  243. if(sPop(sc, &o)) {
  244. return;
  245. } else if(o.type != OT_INT) {
  246. sError(sc, "return address on stack is not an int");
  247. return;
  248. }
  249. sc->readIndex = o.data.intValue;
  250. if(sc->returnValue.type != OT_VOID) {
  251. sPush(sc, &sc->returnValue);
  252. sc->returnValue.type = OT_VOID;
  253. }
  254. }
  255. static void sConsumeInstruction(Script* sc) {
  256. switch(sReadOperation(sc)) {
  257. case OP_NOTHING: break;
  258. case OP_PUSH_INT: sPushCodeInt(sc); break;
  259. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  260. case OP_PUSH_NULL: sPushNull(sc); break;
  261. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  262. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  263. case OP_PUSH_VARS: sPushVars(sc); break;
  264. case OP_POP_VARS: sPopVars(sc); break;
  265. case OP_POP: sPopEmpty(sc); break;
  266. case OP_SET: sSet(sc); break;
  267. case OP_GET: sGet(sc); break;
  268. case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
  269. case OP_SUB: sIntBinary(sc, sIntSub, sFloatSub); break;
  270. case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
  271. case OP_LESS: sBoolBinary(sc, sIntLess, sFloatLess); break;
  272. case OP_PRINT: sPrint(sc); break;
  273. case OP_LINE: sLine(sc); break;
  274. case OP_GOTO: sGoTo(sc); break;
  275. case OP_GOSUB: sGoSub(sc); break;
  276. case OP_IF_GOTO: sIfGoTo(sc); break;
  277. case OP_SET_RETURN: sSetReturn(sc); break;
  278. case OP_RETURN: sReturn(sc); break;
  279. }
  280. }
  281. static bool sHasData(Script* sc) {
  282. return sc->readIndex < sc->code->length;
  283. }
  284. Script* sInit(ByteCode* code) {
  285. Script* sc = malloc(sizeof(Script));
  286. sc->error[0] = '\0';
  287. sc->code = code;
  288. sc->readIndex = 0;
  289. sc->returnValue.type = OT_VOID;
  290. sc->stackIndex = 0;
  291. sc->stackVarIndex = 0;
  292. sc->line = 0;
  293. return sc;
  294. }
  295. void sDelete(Script* sc) {
  296. bcDelete(sc->code);
  297. free(sc);
  298. }
  299. void sRun(Script* sc) {
  300. while(sHasData(sc)) {
  301. sConsumeInstruction(sc);
  302. if(sc->error[0] != '\0') {
  303. puts(sc->error);
  304. return;
  305. }
  306. }
  307. }
  308. void sSetPrinter(ObjectPrinter p) {
  309. printer = p;
  310. }
  311. static void sPrintInt(Script* sc, const char* msg) {
  312. int value = 0;
  313. sReadInt(sc, &value);
  314. printf("%s %d\n", msg, value);
  315. }
  316. static void sPrint2Int(Script* sc, const char* msg) {
  317. int a = 0;
  318. sReadInt(sc, &a);
  319. int b = 0;
  320. sReadInt(sc, &b);
  321. printf("%s %d %d\n", msg, a, b);
  322. }
  323. static void sPrintInt16(Script* sc, const char* msg) {
  324. int value = 0;
  325. sRead(sc, &value, 2);
  326. printf("%s %d\n", msg, value);
  327. }
  328. static void sPrintFloat(Script* sc, const char* msg) {
  329. float value = 0;
  330. sRead(sc, &value, sizeof(float));
  331. printf("%s %.2f\n", msg, value);
  332. }
  333. void sPrintCode(Script* sc) {
  334. int oldRead = sc->readIndex;
  335. sc->readIndex = 0;
  336. while(sHasData(sc)) {
  337. printf(" %3d | ", sc->readIndex);
  338. switch(sReadOperation(sc)) {
  339. case OP_NOTHING: puts("Nothing"); break;
  340. case OP_PUSH_INT: sPrintInt(sc, "Push Int"); break;
  341. case OP_PUSH_FLOAT: sPrintFloat(sc, "Push Float"); break;
  342. case OP_PUSH_NULL: puts("Push null"); break;
  343. case OP_PUSH_TRUE: puts("Push true"); break;
  344. case OP_PUSH_FALSE: puts("Push false"); break;
  345. case OP_PUSH_VARS: sPrint2Int(sc, "Push Vars"); break;
  346. case OP_POP_VARS: sPrintInt(sc, "Pop Vars"); break;
  347. case OP_POP: puts("Pop"); break;
  348. case OP_SET: sPrintInt(sc, "Set"); break;
  349. case OP_GET: sPrintInt(sc, "Get"); break;
  350. case OP_ADD: puts("Add"); break;
  351. case OP_SUB: puts("Sub"); break;
  352. case OP_MUL: puts("Mul"); break;
  353. case OP_LESS: puts("Less"); break;
  354. case OP_PRINT: puts("Print"); break;
  355. case OP_LINE: sPrintInt16(sc, "------------ Line"); break;
  356. case OP_GOTO: sPrintInt(sc, "GoTo"); break;
  357. case OP_GOSUB: sPrint2Int(sc, "GoSub"); break;
  358. case OP_IF_GOTO: sPrintInt(sc, "If GoTo"); break;
  359. case OP_SET_RETURN: puts("Set Return"); break;
  360. case OP_RETURN: puts("Return"); break;
  361. }
  362. }
  363. sc->readIndex = oldRead;
  364. }