Script.c 5.0 KB

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