Script.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "Operation.h"
  6. #include "Script.h"
  7. static const char* MISSING_INT_CONSTANT = "cannot read an int from the bytecode";
  8. static const char* NOT_AN_INT = "object is not an int";
  9. static const char* NOT_PRINTABLE = "cannot print given object";
  10. static const char* STACK_OVERFLOW = "stack overflow";
  11. static const char* STACK_UNDERFLOW = "stack underflow";
  12. static bool sPrinter(Object* o) {
  13. if(o->type == OT_INT) {
  14. printf("%d\n", o->data.intValue);
  15. return false;
  16. }
  17. return true;
  18. }
  19. static ObjectPrinter printer = sPrinter;
  20. static bool sRead(Script* sc, void* buffer, int length) {
  21. if(sc->readIndex + length > sc->byteCodeLength) {
  22. return true;
  23. }
  24. memcpy(buffer, sc->byteCode + sc->readIndex, length);
  25. sc->readIndex += length;
  26. return false;
  27. }
  28. static Operation sReadOperation(Script* sc) {
  29. unsigned char c;
  30. if(sRead(sc, &c, 1)) {
  31. return OP_NOTHING;
  32. }
  33. return c;
  34. }
  35. static void sPush(Script* sc, Object* o) {
  36. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  37. sc->error = STACK_OVERFLOW;
  38. return;
  39. }
  40. sc->stack[sc->stackIndex++] = *o;
  41. }
  42. static bool sPop(Script* sc, Object* o) {
  43. if(sc->stackIndex <= 0) {
  44. sc->error = STACK_UNDERFLOW;
  45. return true;
  46. }
  47. *o = sc->stack[--sc->stackIndex];
  48. return false;
  49. }
  50. static void sPushInt(Script* sc) {
  51. int value = 0;
  52. if(sRead(sc, &value, sizeof(int))) {
  53. sc->error = MISSING_INT_CONSTANT;
  54. return;
  55. }
  56. Object o = {.type = OT_INT, .data.intValue = value};
  57. sPush(sc, &o);
  58. }
  59. static void sPushNull(Script* sc) {
  60. Object o = {.type = OT_NULL};
  61. sPush(sc, &o);
  62. }
  63. static void sIntBinary(Script* sc, int (*f)(int, int)) {
  64. Object a;
  65. if(sPop(sc, &a)) {
  66. return;
  67. }
  68. Object b;
  69. if(sPop(sc, &b)) {
  70. return;
  71. }
  72. if(a.type != OT_INT || b.type != OT_INT) {
  73. sc->error = NOT_AN_INT;
  74. return;
  75. }
  76. Object o = {.type = OT_INT, .data.intValue = f(a.data.intValue, b.data.intValue)};
  77. sPush(sc, &o);
  78. }
  79. static int sIntAdd(int a, int b) {
  80. return a + b;
  81. }
  82. static int sIntMul(int a, int b) {
  83. return a * b;
  84. }
  85. static void sPrint(Script* sc) {
  86. Object o;
  87. if(sPop(sc, &o)) {
  88. return;
  89. }
  90. if(printer(&o)) {
  91. sc->error = NOT_PRINTABLE;
  92. }
  93. }
  94. static void sConsumeInstruction(Script* sc) {
  95. switch(sReadOperation(sc)) {
  96. case OP_NOTHING: break;
  97. case OP_PUSH_INT: sPushInt(sc); break;
  98. case OP_PUSH_NULL: sPushNull(sc); break;
  99. case OP_ADD: sIntBinary(sc, sIntAdd); break;
  100. case OP_MUL: sIntBinary(sc, sIntMul); break;
  101. case OP_PRINT: sPrint(sc); break;
  102. }
  103. }
  104. static bool sHasData(Script* sc) {
  105. return sc->readIndex < sc->byteCodeLength;
  106. }
  107. Script* sInit(unsigned char* byteCode, int codeLength) {
  108. Script* sc = malloc(sizeof(Script));
  109. sc->error = NULL;
  110. sc->byteCode = byteCode;
  111. sc->byteCodeLength = codeLength;
  112. sc->readIndex = 0;
  113. sc->stackIndex = 0;
  114. return sc;
  115. }
  116. void sDelete(Script* sc) {
  117. free(sc->byteCode);
  118. }
  119. void sRun(Script* sc) {
  120. while(sHasData(sc)) {
  121. sConsumeInstruction(sc);
  122. if(sc->error != NULL) {
  123. puts(sc->error);
  124. return;
  125. }
  126. }
  127. }
  128. void sSetPrinter(ObjectPrinter p) {
  129. printer = p;
  130. }