Script.c 3.5 KB

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