Script.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 sShiftPush(Script* sc, Object* o, int shift) {
  44. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  45. sError(sc, "stack overflow on line %d", sc->line);
  46. return false;
  47. } else if(sc->stackIndex < shift) {
  48. sError(sc, "stack shift underflow overflow on line %d", sc->line);
  49. return false;
  50. }
  51. for(int i = sc->stackIndex; i > sc->stackIndex - shift; i--) {
  52. sc->stack[i] = sc->stack[i - 1];
  53. }
  54. sc->stack[sc->stackIndex - shift] = *o;
  55. sc->stackIndex++;
  56. return true;
  57. }
  58. static bool sPush(Script* sc, Object* o) {
  59. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  60. sError(sc, "stack overflow on line %d", sc->line);
  61. return false;
  62. }
  63. sc->stack[sc->stackIndex++] = *o;
  64. return true;
  65. }
  66. static bool sPop(Script* sc, Object* o) {
  67. if(sc->stackIndex <= 0) {
  68. sError(sc, "stack underflow on line %d", sc->line);
  69. return true;
  70. }
  71. *o = sc->stack[--sc->stackIndex];
  72. return false;
  73. }
  74. static bool sPushInt(Script* sc, int value) {
  75. Object o = {.type = OT_INT, .data.intValue = value};
  76. return sPush(sc, &o);
  77. }
  78. static bool sShiftPushInt(Script* sc, int value, int shift) {
  79. Object o = {.type = OT_INT, .data.intValue = value};
  80. return sShiftPush(sc, &o, shift);
  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)) {
  110. if(o.type != OT_INT) {
  111. sError(sc, "stack variable index has wrong type");
  112. return;
  113. }
  114. sc->stackVarIndex = o.data.intValue;
  115. if(sc->stackIndex < value) {
  116. sError(sc, "stack underflow on line %d", sc->line);
  117. } else {
  118. sc->stackIndex -= value;
  119. }
  120. }
  121. }
  122. static void sSet(Script* sc) {
  123. int value = 0;
  124. if(sReadInt(sc, &value)) {
  125. sPop(sc, sc->stack + value + sc->stackVarIndex);
  126. }
  127. }
  128. static void sGet(Script* sc) {
  129. int value = 0;
  130. if(sReadInt(sc, &value)) {
  131. sPush(sc, sc->stack + value + sc->stackVarIndex);
  132. }
  133. }
  134. static void sPushCodeInt(Script* sc) {
  135. int value = 0;
  136. if(sReadInt(sc, &value)) {
  137. sPushInt(sc, value);
  138. }
  139. }
  140. static void sPushCodeFloat(Script* sc) {
  141. float value = 0;
  142. if(sRead(sc, &value, sizeof(float))) {
  143. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  144. return;
  145. }
  146. sPushFloat(sc, value);
  147. }
  148. static bool sToFloat(Script* sc, Object* o, float* r) {
  149. if(o->type == OT_FLOAT) {
  150. *r = o->data.floatValue;
  151. return true;
  152. } else if(o->type == OT_INT) {
  153. *r = o->data.intValue;
  154. return true;
  155. }
  156. sError(sc, "object is not a number on line %d", sc->line);
  157. return false;
  158. }
  159. static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
  160. Object o[2];
  161. if(sPop(sc, o) || sPop(sc, o + 1)) {
  162. return;
  163. }
  164. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  165. sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  166. return;
  167. }
  168. float f[2];
  169. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  170. sPushFloat(sc, fFloat(f[0], f[1]));
  171. }
  172. }
  173. static int sIntAdd(int a, int b) {
  174. return a + b;
  175. }
  176. static int sIntMul(int a, int b) {
  177. return a * b;
  178. }
  179. static float sFloatAdd(float a, float b) {
  180. return a + b;
  181. }
  182. static float sFloatMul(float a, float b) {
  183. return a * b;
  184. }
  185. static void sPrint(Script* sc) {
  186. Object o;
  187. if(!sPop(sc, &o) && printer(&o)) {
  188. sError(sc, "cannot print given object on line %d", sc->line);
  189. }
  190. }
  191. static void sLine(Script* sc) {
  192. if(sRead(sc, &sc->line, 2)) {
  193. sError(sc, "line operation without a line near line %d", sc->line);
  194. }
  195. }
  196. static void sGoTo(Script* sc) {
  197. int gotoIndex;
  198. if(sReadInt(sc, &gotoIndex)) {
  199. sc->readIndex = gotoIndex;
  200. }
  201. }
  202. static void sGoSub(Script* sc) {
  203. int gotoIndex;
  204. int arguments;
  205. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &arguments) && sShiftPushInt(sc, sc->readIndex, arguments)) {
  206. sc->readIndex = gotoIndex;
  207. }
  208. }
  209. static void sReturn(Script* sc) {
  210. Object o;
  211. if(sPop(sc, &o)) {
  212. return;
  213. } else if(o.type != OT_INT) {
  214. sError(sc, "return address on stack is not an int");
  215. return;
  216. }
  217. sc->readIndex = o.data.intValue;
  218. }
  219. static void sConsumeInstruction(Script* sc) {
  220. switch(sReadOperation(sc)) {
  221. case OP_NOTHING: break;
  222. case OP_PUSH_INT: sPushCodeInt(sc); break;
  223. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  224. case OP_PUSH_NULL: sPushNull(sc); break;
  225. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  226. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  227. case OP_PUSH: sPushVars(sc); break;
  228. case OP_POP: sPopVars(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_RETURN: sReturn(sc); break;
  238. }
  239. }
  240. static bool sHasData(Script* sc) {
  241. return sc->readIndex < sc->code->length;
  242. }
  243. Script* sInit(ByteCode* code) {
  244. Script* sc = malloc(sizeof(Script));
  245. sc->error[0] = '\0';
  246. sc->code = code;
  247. sc->readIndex = 0;
  248. sc->stackIndex = 0;
  249. sc->stackVarIndex = 0;
  250. sc->line = 0;
  251. return sc;
  252. }
  253. void sDelete(Script* sc) {
  254. bcDelete(sc->code);
  255. free(sc);
  256. }
  257. void sRun(Script* sc) {
  258. while(sHasData(sc)) {
  259. sConsumeInstruction(sc);
  260. if(sc->error[0] != '\0') {
  261. puts(sc->error);
  262. return;
  263. }
  264. }
  265. }
  266. void sSetPrinter(ObjectPrinter p) {
  267. printer = p;
  268. }