Script.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. for(int i = 0; i < value; i++) {
  77. sPushNull(sc);
  78. }
  79. }
  80. }
  81. static void sPopVars(Script* sc) {
  82. int value = 0;
  83. if(sReadInt(sc, &value)) {
  84. if(sc->stackIndex < value) {
  85. sError(sc, "stack underflow on line %d", sc->line);
  86. } else {
  87. sc->stackIndex -= value;
  88. }
  89. }
  90. }
  91. static void sSet(Script* sc) {
  92. int value = 0;
  93. if(sReadInt(sc, &value)) {
  94. sPop(sc, sc->stack + value);
  95. }
  96. }
  97. static void sGet(Script* sc) {
  98. int value = 0;
  99. if(sReadInt(sc, &value)) {
  100. sPush(sc, sc->stack + value);
  101. }
  102. }
  103. static void sPushCodeInt(Script* sc) {
  104. int value = 0;
  105. if(sReadInt(sc, &value)) {
  106. sPushInt(sc, value);
  107. }
  108. }
  109. static void sPushCodeFloat(Script* sc) {
  110. float value = 0;
  111. if(sRead(sc, &value, sizeof(float))) {
  112. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  113. return;
  114. }
  115. sPushFloat(sc, value);
  116. }
  117. static bool sToFloat(Script* sc, Object* o, float* r) {
  118. if(o->type == OT_FLOAT) {
  119. *r = o->data.floatValue;
  120. return true;
  121. } else if(o->type == OT_INT) {
  122. *r = o->data.intValue;
  123. return true;
  124. }
  125. sError(sc, "object is not a number on line %d", sc->line);
  126. return false;
  127. }
  128. static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
  129. Object o[2];
  130. if(sPop(sc, o) || sPop(sc, o + 1)) {
  131. return;
  132. }
  133. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  134. sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  135. return;
  136. }
  137. float f[2];
  138. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  139. sPushFloat(sc, fFloat(f[0], f[1]));
  140. }
  141. }
  142. static int sIntAdd(int a, int b) {
  143. return a + b;
  144. }
  145. static int sIntMul(int a, int b) {
  146. return a * b;
  147. }
  148. static float sFloatAdd(float a, float b) {
  149. return a + b;
  150. }
  151. static float sFloatMul(float a, float b) {
  152. return a * b;
  153. }
  154. static void sPrint(Script* sc) {
  155. Object o;
  156. if(!sPop(sc, &o) && printer(&o)) {
  157. sError(sc, "cannot print given object on line %d", sc->line);
  158. }
  159. }
  160. static void sLine(Script* sc) {
  161. if(sRead(sc, &sc->line, 2)) {
  162. sError(sc, "line operation without a line near line %d", sc->line);
  163. }
  164. }
  165. static void sGoTo(Script* sc) {
  166. int gotoIndex;
  167. if(sReadInt(sc, &gotoIndex)) {
  168. sc->readIndex = gotoIndex;
  169. }
  170. }
  171. static void sGoSub(Script* sc) {
  172. int gotoIndex;
  173. if(sReadInt(sc, &gotoIndex) && sPushInt(sc, sc->readIndex)) {
  174. sc->readIndex = gotoIndex;
  175. }
  176. }
  177. static void sReturn(Script* sc) {
  178. Object o;
  179. if(sPop(sc, &o)) {
  180. return;
  181. } else if(o.type != OT_INT) {
  182. sError(sc, "return address on stack is not an int");
  183. return;
  184. }
  185. sc->readIndex = o.data.intValue;
  186. }
  187. static void sConsumeInstruction(Script* sc) {
  188. switch(sReadOperation(sc)) {
  189. case OP_NOTHING: break;
  190. case OP_PUSH_INT: sPushCodeInt(sc); break;
  191. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  192. case OP_PUSH_NULL: sPushNull(sc); break;
  193. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  194. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  195. case OP_PUSH: sPushVars(sc); break;
  196. case OP_POP: sPopVars(sc); break;
  197. case OP_SET: sSet(sc); break;
  198. case OP_GET: sGet(sc); break;
  199. case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
  200. case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
  201. case OP_PRINT: sPrint(sc); break;
  202. case OP_LINE: sLine(sc); break;
  203. case OP_GOTO: sGoTo(sc); break;
  204. case OP_GOSUB: sGoSub(sc); break;
  205. case OP_RETURN: sReturn(sc); break;
  206. }
  207. }
  208. static bool sHasData(Script* sc) {
  209. return sc->readIndex < sc->code->length;
  210. }
  211. Script* sInit(ByteCode* code) {
  212. Script* sc = malloc(sizeof(Script));
  213. sc->error[0] = '\0';
  214. sc->code = code;
  215. sc->readIndex = 0;
  216. sc->stackIndex = 0;
  217. sc->line = 0;
  218. return sc;
  219. }
  220. void sDelete(Script* sc) {
  221. bcDelete(sc->code);
  222. free(sc);
  223. }
  224. void sRun(Script* sc) {
  225. while(sHasData(sc)) {
  226. sConsumeInstruction(sc);
  227. if(sc->error[0] != '\0') {
  228. puts(sc->error);
  229. return;
  230. }
  231. }
  232. }
  233. void sSetPrinter(ObjectPrinter p) {
  234. printer = p;
  235. }