Script.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. void sAdd(Script* sc) {
  60. Object a;
  61. if(sPop(sc, &a)) {
  62. return;
  63. }
  64. Object b;
  65. if(sPop(sc, &b)) {
  66. return;
  67. }
  68. if(a.type != OT_INT || b.type != OT_INT) {
  69. sc->error = NOT_AN_INT;
  70. return;
  71. }
  72. Object o = {.type = OT_INT, .data.intValue = a.data.intValue + b.data.intValue};
  73. sPush(sc, &o);
  74. }
  75. void sPrint(Script* sc) {
  76. Object o;
  77. if(sPop(sc, &o)) {
  78. return;
  79. }
  80. if(printer(&o)) {
  81. sc->error = NOT_PRINTABLE;
  82. }
  83. }
  84. static void sConsumeInstruction(Script* sc) {
  85. switch(sReadOperation(sc)) {
  86. case OP_NOTHING: break;
  87. case OP_PUSH_INT: sPushInt(sc); break;
  88. case OP_ADD: sAdd(sc); break;
  89. case OP_PRINT: sPrint(sc); break;
  90. }
  91. }
  92. static bool sHasData(Script* sc) {
  93. return sc->readIndex < sc->byteCodeLength;
  94. }
  95. Script* sInit(unsigned char* byteCode, int codeLength) {
  96. Script* sc = malloc(sizeof(Script));
  97. sc->error = NULL;
  98. sc->byteCode = byteCode;
  99. sc->byteCodeLength = codeLength;
  100. sc->readIndex = 0;
  101. sc->stackIndex = 0;
  102. return sc;
  103. }
  104. void sDelete(Script* sc) {
  105. free(sc->byteCode);
  106. }
  107. void sRun(Script* sc) {
  108. while(sHasData(sc)) {
  109. sConsumeInstruction(sc);
  110. if(sc->error != NULL) {
  111. puts(sc->error);
  112. return;
  113. }
  114. }
  115. }
  116. void sSetPrinter(ObjectPrinter p) {
  117. printer = p;
  118. }