Script.c 6.6 KB

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