Script.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, sizeof(int))) {
  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 sPushCodeInt(Script* sc) {
  72. int value = 0;
  73. if(sRead(sc, &value, sizeof(int))) {
  74. sError(sc, "cannot read an int from the bytecode on line %d", sc->line);
  75. return;
  76. }
  77. sPushInt(sc, value);
  78. }
  79. static void sPushCodeFloat(Script* sc) {
  80. float value = 0;
  81. if(sRead(sc, &value, sizeof(float))) {
  82. sError(sc, "cannot read a float from the bytecode on line %d", sc->line);
  83. return;
  84. }
  85. sPushFloat(sc, value);
  86. }
  87. static bool sToFloat(Script* sc, Object* o, float* r) {
  88. if(o->type == OT_FLOAT) {
  89. *r = o->data.floatValue;
  90. return true;
  91. } else if(o->type == OT_INT) {
  92. *r = o->data.intValue;
  93. return true;
  94. }
  95. sError(sc, "object is not a number on line %d", sc->line);
  96. return false;
  97. }
  98. static void sIntBinary(Script* sc, int (*fInt)(int, int), float (*fFloat)(float, float)) {
  99. Object o[2];
  100. if(sPop(sc, o) || sPop(sc, o + 1)) {
  101. return;
  102. }
  103. if(o[0].type == OT_INT && o[1].type == OT_INT) {
  104. sPushInt(sc, fInt(o[0].data.intValue, o[1].data.intValue));
  105. return;
  106. }
  107. float f[2];
  108. if(sToFloat(sc, o, f) && sToFloat(sc, o + 1, f + 1)) {
  109. sPushFloat(sc, fFloat(f[0], f[1]));
  110. }
  111. }
  112. static int sIntAdd(int a, int b) {
  113. return a + b;
  114. }
  115. static int sIntMul(int a, int b) {
  116. return a * b;
  117. }
  118. static float sFloatAdd(float a, float b) {
  119. return a + b;
  120. }
  121. static float sFloatMul(float a, float b) {
  122. return a * b;
  123. }
  124. static void sPrint(Script* sc) {
  125. Object o;
  126. if(sPop(sc, &o)) {
  127. return;
  128. }
  129. if(printer(&o)) {
  130. sError(sc, "cannot print given object on line %d", sc->line);
  131. }
  132. }
  133. static void sConsumeInstruction(Script* sc) {
  134. switch(sReadOperation(sc)) {
  135. case OP_NOTHING: break;
  136. case OP_PUSH_INT: sPushCodeInt(sc); break;
  137. case OP_PUSH_FLOAT: sPushCodeFloat(sc); break;
  138. case OP_PUSH_NULL: sPushNull(sc); break;
  139. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  140. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  141. case OP_ADD: sIntBinary(sc, sIntAdd, sFloatAdd); break;
  142. case OP_MUL: sIntBinary(sc, sIntMul, sFloatMul); break;
  143. case OP_PRINT: sPrint(sc); break;
  144. }
  145. }
  146. static bool sHasData(Script* sc) {
  147. return sc->readIndex < sc->byteCodeLength;
  148. }
  149. Script* sInit(unsigned char* byteCode, int codeLength) {
  150. Script* sc = malloc(sizeof(Script));
  151. sc->error[0] = '\0';
  152. sc->byteCode = byteCode;
  153. sc->byteCodeLength = codeLength;
  154. sc->readIndex = 0;
  155. sc->stackIndex = 0;
  156. sc->line = 0;
  157. return sc;
  158. }
  159. void sDelete(Script* sc) {
  160. free(sc->byteCode);
  161. }
  162. void sRun(Script* sc) {
  163. while(sHasData(sc)) {
  164. sConsumeInstruction(sc);
  165. if(sc->error[0] != '\0') {
  166. puts(sc->error);
  167. return;
  168. }
  169. }
  170. }
  171. void sSetPrinter(ObjectPrinter p) {
  172. printer = p;
  173. }