Script.c 6.1 KB

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