Script.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 sPushNull(Script* sc) {
  65. Object o = {.type = OT_NULL};
  66. sPush(sc, &o);
  67. }
  68. static void sPushBool(Script* sc, bool b) {
  69. Object o = {.type = OT_BOOL, .data.intValue = b};
  70. sPush(sc, &o);
  71. }
  72. static void sIntBinary(Script* sc, int (*f)(int, int)) {
  73. Object a;
  74. if(sPop(sc, &a)) {
  75. return;
  76. }
  77. Object b;
  78. if(sPop(sc, &b)) {
  79. return;
  80. }
  81. if(a.type != OT_INT || b.type != OT_INT) {
  82. sError(sc, "object is not an int on line %d", sc->line);
  83. return;
  84. }
  85. Object o = {.type = OT_INT, .data.intValue = f(a.data.intValue, b.data.intValue)};
  86. sPush(sc, &o);
  87. }
  88. static int sIntAdd(int a, int b) {
  89. return a + b;
  90. }
  91. static int sIntMul(int a, int b) {
  92. return a * b;
  93. }
  94. static void sPrint(Script* sc) {
  95. Object o;
  96. if(sPop(sc, &o)) {
  97. return;
  98. }
  99. if(printer(&o)) {
  100. sError(sc, "cannot print given object on line %d", sc->line);
  101. }
  102. }
  103. static void sConsumeInstruction(Script* sc) {
  104. switch(sReadOperation(sc)) {
  105. case OP_NOTHING: break;
  106. case OP_PUSH_INT: sPushInt(sc); break;
  107. case OP_PUSH_NULL: sPushNull(sc); break;
  108. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  109. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  110. case OP_ADD: sIntBinary(sc, sIntAdd); break;
  111. case OP_MUL: sIntBinary(sc, sIntMul); break;
  112. case OP_PRINT: sPrint(sc); break;
  113. }
  114. }
  115. static bool sHasData(Script* sc) {
  116. return sc->readIndex < sc->byteCodeLength;
  117. }
  118. Script* sInit(unsigned char* byteCode, int codeLength) {
  119. Script* sc = malloc(sizeof(Script));
  120. sc->error[0] = '\0';
  121. sc->byteCode = byteCode;
  122. sc->byteCodeLength = codeLength;
  123. sc->readIndex = 0;
  124. sc->stackIndex = 0;
  125. sc->line = 0;
  126. return sc;
  127. }
  128. void sDelete(Script* sc) {
  129. free(sc->byteCode);
  130. }
  131. void sRun(Script* sc) {
  132. while(sHasData(sc)) {
  133. sConsumeInstruction(sc);
  134. if(sc->error[0] != '\0') {
  135. puts(sc->error);
  136. return;
  137. }
  138. }
  139. }
  140. void sSetPrinter(ObjectPrinter p) {
  141. printer = p;
  142. }