Script.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 sRead(Script* sc, void* buffer, int length) {
  13. if(sc->readIndex + length > sc->byteCodeLength) {
  14. return true;
  15. }
  16. memcpy(buffer, sc->byteCode + sc->readIndex, length);
  17. sc->readIndex += length;
  18. return false;
  19. }
  20. static Operation sReadOperation(Script* sc) {
  21. unsigned char c;
  22. if(sRead(sc, &c, 1)) {
  23. return OP_NOTHING;
  24. }
  25. return c;
  26. }
  27. static void sPush(Script* sc, Object* o) {
  28. if(sc->stackIndex >= SCRIPT_STACK_SIZE) {
  29. sc->error = STACK_OVERFLOW;
  30. return;
  31. }
  32. sc->stack[sc->stackIndex++] = *o;
  33. }
  34. static bool sPop(Script* sc, Object* o) {
  35. if(sc->stackIndex <= 0) {
  36. sc->error = STACK_UNDERFLOW;
  37. return true;
  38. }
  39. *o = sc->stack[--sc->stackIndex];
  40. return false;
  41. }
  42. static void sPushInt(Script* sc) {
  43. int value = 0;
  44. if(sRead(sc, &value, sizeof(int))) {
  45. sc->error = MISSING_INT_CONSTANT;
  46. return;
  47. }
  48. Object o = {.type = OT_INT, .data.intValue = value};
  49. sPush(sc, &o);
  50. }
  51. void sAdd(Script* sc) {
  52. Object a;
  53. if(sPop(sc, &a)) {
  54. return;
  55. }
  56. Object b;
  57. if(sPop(sc, &b)) {
  58. return;
  59. }
  60. if(a.type != OT_INT || b.type != OT_INT) {
  61. sc->error = NOT_AN_INT;
  62. return;
  63. }
  64. Object o = {.type = OT_INT, .data.intValue = a.data.intValue + b.data.intValue};
  65. sPush(sc, &o);
  66. }
  67. void sPrint(Script* sc) {
  68. Object o;
  69. if(sPop(sc, &o)) {
  70. return;
  71. }
  72. if(o.type == OT_INT) {
  73. printf("%d\n", o.data.intValue);
  74. } else {
  75. sc->error = NOT_PRINTABLE;
  76. }
  77. }
  78. static void sConsumeInstruction(Script* sc) {
  79. switch(sReadOperation(sc)) {
  80. case OP_NOTHING: break;
  81. case OP_PUSH_INT: sPushInt(sc); break;
  82. case OP_ADD: sAdd(sc); break;
  83. case OP_PRINT: sPrint(sc); break;
  84. }
  85. }
  86. static bool sHasData(Script* sc) {
  87. return sc->readIndex < sc->byteCodeLength;
  88. }
  89. Script* sInit(unsigned char* byteCode, int codeLength) {
  90. Script* sc = malloc(sizeof(Script));
  91. sc->error = NULL;
  92. sc->byteCode = byteCode;
  93. sc->byteCodeLength = codeLength;
  94. sc->readIndex = 0;
  95. sc->stackIndex = 0;
  96. return sc;
  97. }
  98. void sDelete(Script* sc) {
  99. free(sc->byteCode);
  100. }
  101. void sRun(Script* sc) {
  102. while(sHasData(sc)) {
  103. sConsumeInstruction(sc);
  104. if(sc->error != NULL) {
  105. puts(sc->error);
  106. return;
  107. }
  108. }
  109. }