Script.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. }
  21. return true;
  22. }
  23. static ObjectPrinter printer = sPrinter;
  24. static bool sRead(Script* sc, void* buffer, int length) {
  25. if(sc->readIndex + length > sc->code->length) {
  26. sError(sc, "cannot read expected %d bytes of data from bytecode on line %d", sc->line);
  27. return true;
  28. }
  29. memcpy(buffer, sc->code->code + sc->readIndex, length);
  30. sc->readIndex += length;
  31. return false;
  32. }
  33. static Operation sReadOperation(Script* sc) {
  34. unsigned char c;
  35. if(sRead(sc, &c, 1)) {
  36. return OP_NOTHING;
  37. }
  38. return c;
  39. }
  40. static bool sReadInt(Script* sc, int* i) {
  41. return !sRead(sc, i, sizeof(int));
  42. }
  43. static bool sPush(Script* sc, Object* o) {
  44. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  45. sError(sc, "stack overflow on line %d", sc->line);
  46. return false;
  47. }
  48. sc->stack[sc->stackIndex++] = *o;
  49. return true;
  50. }
  51. static bool sPop(Script* sc, Object* o) {
  52. if(sc->stackIndex <= 0) {
  53. sError(sc, "stack underflow on line %d", sc->line);
  54. return true;
  55. }
  56. *o = sc->stack[--sc->stackIndex];
  57. return false;
  58. }
  59. static bool sPushInt(Script* sc, int value) {
  60. Object o = {.type = OT_INT, .data.intValue = value};
  61. return sPush(sc, &o);
  62. }
  63. static void sPushFloat(Script* sc, float value) {
  64. Object o = {.type = OT_FLOAT, .data.floatValue = value};
  65. sPush(sc, &o);
  66. }
  67. static void sPushNull(Script* sc) {
  68. Object o = {.type = OT_NULL};
  69. sPush(sc, &o);
  70. }
  71. static void sPushBool(Script* sc, bool value) {
  72. Object o = {.type = OT_BOOL, .data.intValue = value};
  73. sPush(sc, &o);
  74. }
  75. static void sPushVars(Script* sc) {
  76. int vars = 0;
  77. int offset = 0;
  78. if(sReadInt(sc, &vars) && sReadInt(sc, &offset)) {
  79. int stackVarIndex = sc->stackVarIndex;
  80. sc->stackVarIndex = sc->stackIndex - offset;
  81. for(int i = 0; i < vars - offset; i++) {
  82. sPushNull(sc);
  83. }
  84. sPushInt(sc, stackVarIndex);
  85. }
  86. }
  87. static void sPopVars(Script* sc) {
  88. int value = 0;
  89. Object o;
  90. if(sReadInt(sc, &value) && !sPop(sc, &o)) {
  91. if(o.type != OT_INT) {
  92. sError(sc, "stack variable index has wrong type");
  93. return;
  94. }
  95. sc->stackVarIndex = o.data.intValue;
  96. if(sc->stackIndex < value) {
  97. sError(sc, "stack underflow on line %d", sc->line);
  98. } else {
  99. sc->stackIndex -= value;
  100. }
  101. }
  102. }
  103. static void sSet(Script* sc) {
  104. int value = 0;
  105. if(sReadInt(sc, &value)) {
  106. sPop(sc, sc->stack + value + sc->stackVarIndex);
  107. }
  108. }
  109. static void sGet(Script* sc) {
  110. int value = 0;
  111. if(sReadInt(sc, &value)) {
  112. sPush(sc, sc->stack + value + sc->stackVarIndex);
  113. }
  114. }
  115. static void sPushCodeInt(Script* sc) {
  116. int value = 0;
  117. if(sReadInt(sc, &value)) {
  118. sPushInt(sc, value);
  119. }
  120. }
  121. static void sPushCodeFloat(Script* sc) {
  122. float value = 0;
  123. if(sRead(sc, &value, sizeof(float))) {
  124. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  125. return;
  126. }
  127. sPushFloat(sc, value);
  128. }
  129. static bool sToFloat(Script* sc, Object* o, float* r) {
  130. if(o->type == OT_FLOAT) {
  131. *r = o->data.floatValue;
  132. return true;
  133. } else if(o->type == OT_INT) {
  134. *r = o->data.intValue;
  135. return true;
  136. }
  137. sError(sc, "object is not a number on line %d", sc->line);
  138. return false;
  139. }
  140. static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
  141. Object o[2];
  142. if(sPop(sc, o) || sPop(sc, o + 1)) {
  143. return;
  144. }
  145. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  146. sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  147. return;
  148. }
  149. float f[2];
  150. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  151. sPushFloat(sc, fFloat(f[0], f[1]));
  152. }
  153. }
  154. static int sIntAdd(int a, int b) {
  155. return a + b;
  156. }
  157. static int sIntMul(int a, int b) {
  158. return a * b;
  159. }
  160. static float sFloatAdd(float a, float b) {
  161. return a + b;
  162. }
  163. static float sFloatMul(float a, float b) {
  164. return a * b;
  165. }
  166. static void sPrint(Script* sc) {
  167. Object o;
  168. if(!sPop(sc, &o) && printer(&o)) {
  169. sError(sc, "cannot print given object on line %d", sc->line);
  170. }
  171. }
  172. static void sLine(Script* sc) {
  173. if(sRead(sc, &sc->line, 2)) {
  174. sError(sc, "line operation without a line near line %d", sc->line);
  175. }
  176. }
  177. static void sGoTo(Script* sc) {
  178. int gotoIndex;
  179. if(sReadInt(sc, &gotoIndex)) {
  180. sc->readIndex = gotoIndex;
  181. }
  182. }
  183. static void sGoSub(Script* sc) {
  184. int gotoIndex;
  185. int arguments;
  186. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments)) {
  187. int returnStackIndex = sc->stackIndex - arguments - 1;
  188. if(returnStackIndex < 0 || sc->stack[returnStackIndex].type != OT_INT) {
  189. sError(sc, "cannot find return address entry on stack on line %d", sc->line);
  190. return;
  191. }
  192. sc->stack[returnStackIndex].data.intValue = sc->readIndex;
  193. sc->readIndex = gotoIndex;
  194. }
  195. }
  196. static void sReturn(Script* sc) {
  197. Object o;
  198. if(sPop(sc, &o)) {
  199. return;
  200. } else if(o.type != OT_INT) {
  201. sError(sc, "return address on stack is not an int");
  202. return;
  203. }
  204. sc->readIndex = o.data.intValue;
  205. }
  206. static void sConsumeInstruction(Script* sc) {
  207. switch(sReadOperation(sc)) {
  208. case OP_NOTHING: break;
  209. case OP_PUSH_INT: sPushCodeInt(sc); break;
  210. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  211. case OP_PUSH_NULL: sPushNull(sc); break;
  212. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  213. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  214. case OP_PUSH: sPushVars(sc); break;
  215. case OP_POP: sPopVars(sc); break;
  216. case OP_SET: sSet(sc); break;
  217. case OP_GET: sGet(sc); break;
  218. case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
  219. case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
  220. case OP_PRINT: sPrint(sc); break;
  221. case OP_LINE: sLine(sc); break;
  222. case OP_GOTO: sGoTo(sc); break;
  223. case OP_GOSUB: sGoSub(sc); break;
  224. case OP_RETURN: sReturn(sc); break;
  225. }
  226. }
  227. static bool sHasData(Script* sc) {
  228. return sc->readIndex < sc->code->length;
  229. }
  230. Script* sInit(ByteCode* code) {
  231. Script* sc = malloc(sizeof(Script));
  232. sc->error[0] = '\0';
  233. sc->code = code;
  234. sc->readIndex = 0;
  235. sc->stackIndex = 0;
  236. sc->stackVarIndex = 0;
  237. sc->line = 0;
  238. return sc;
  239. }
  240. void sDelete(Script* sc) {
  241. bcDelete(sc->code);
  242. free(sc);
  243. }
  244. void sRun(Script* sc) {
  245. while(sHasData(sc)) {
  246. sConsumeInstruction(sc);
  247. if(sc->error[0] != '\0') {
  248. puts(sc->error);
  249. return;
  250. }
  251. }
  252. }
  253. void sSetPrinter(ObjectPrinter p) {
  254. printer = p;
  255. }