Compiler.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "Compiler.h"
  6. #include "Operation.h"
  7. #include "Tokenizer.h"
  8. #define MAX_BYTES (1024 * 1024)
  9. #define ERROR_LENGTH 256
  10. static char error[ERROR_LENGTH] = {'\0'};
  11. static unsigned char byteCode[MAX_BYTES];
  12. static int writeIndex = 0;
  13. static int line = 1;
  14. static void cError(const char* format, ...) {
  15. va_list args;
  16. va_start(args, format);
  17. vsnprintf(error, ERROR_LENGTH, format, args);
  18. va_end(args);
  19. }
  20. static void cUnexpectedToken(Token t) {
  21. cError("unexpected token on line %d: %s", line, tGetTokenName(t));
  22. }
  23. static bool cAddBytes(const void* data, int length) {
  24. if(writeIndex + length > MAX_BYTES) {
  25. cError("the compiler buffer is too small");
  26. return false;
  27. }
  28. memcpy(byteCode + writeIndex, data, length);
  29. writeIndex += length;
  30. return true;
  31. }
  32. static bool cAddOperation(Operation token) {
  33. unsigned char c = token;
  34. return cAddBytes(&c, 1) && cAddBytes(&line, sizeof(int));
  35. }
  36. static Token tReadTokenAndLine() {
  37. Token t = tReadToken();
  38. if(tReadInt(&line)) {
  39. return t;
  40. }
  41. return T_END;
  42. }
  43. static bool cConsumeToken(Token wanted) {
  44. Token t = tReadTokenAndLine();
  45. if(wanted == t) {
  46. return true;
  47. }
  48. cError("unexpected token on line %d: expected '%s' got '%s'", line, tGetTokenName(wanted), tGetTokenName(t));
  49. return false;
  50. }
  51. static bool cConsumeTokenIf(Token t) {
  52. if(tPeekToken() == t) {
  53. tReadTokenAndLine();
  54. return true;
  55. }
  56. return false;
  57. }
  58. static bool cExpression();
  59. static bool cPrimary() {
  60. if(cConsumeTokenIf(T_INT)) {
  61. int value;
  62. return tReadInt(&value) && cAddOperation(OP_PUSH_INT) && cAddBytes(&value, sizeof(int));
  63. } else if(cConsumeTokenIf(T_FLOAT)) {
  64. float value;
  65. return tReadFloat(&value) && cAddOperation(OP_PUSH_FLOAT) && cAddBytes(&value, sizeof(float));
  66. } else if(cConsumeTokenIf(T_NULL)) {
  67. return cAddOperation(OP_PUSH_NULL);
  68. } else if(cConsumeTokenIf(T_TRUE)) {
  69. return cAddOperation(OP_PUSH_TRUE);
  70. } else if(cConsumeTokenIf(T_FALSE)) {
  71. return cAddOperation(OP_PUSH_FALSE);
  72. } else if(cConsumeTokenIf(T_OPEN_BRACKET)) {
  73. return cExpression() && cConsumeToken(T_CLOSE_BRACKET);
  74. }
  75. cUnexpectedToken(tPeekToken());
  76. return false;
  77. }
  78. static bool cMul() {
  79. if(!cPrimary()) {
  80. return false;
  81. }
  82. while(cConsumeTokenIf(T_MUL)) {
  83. if(!cPrimary() || !cAddOperation(OP_MUL)) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. static bool cAdd() {
  90. if(!cMul()) {
  91. return false;
  92. }
  93. while(cConsumeTokenIf(T_ADD)) {
  94. if(!cMul() || !cAddOperation(OP_ADD)) {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. static bool cExpression() {
  101. return cAdd();
  102. }
  103. static bool cPrint() {
  104. return cExpression() && cConsumeToken(T_SEMICOLON) && cAddOperation(OP_PRINT);
  105. }
  106. static bool cLine() {
  107. Token t = tReadTokenAndLine();
  108. if(t == T_END) {
  109. return false;
  110. } else if(t == T_PRINT) {
  111. return cPrint();
  112. }
  113. cUnexpectedToken(t);
  114. return false;
  115. }
  116. unsigned char* cCompile(int* codeLength) {
  117. writeIndex = 0;
  118. error[0] = '\0';
  119. while(cLine()) {
  120. }
  121. if(error[0] != '\0') {
  122. return NULL;
  123. }
  124. unsigned char* bytes = malloc(writeIndex);
  125. memcpy(bytes, byteCode, writeIndex);
  126. *codeLength = writeIndex;
  127. return bytes;
  128. }
  129. const char* cGetError() {
  130. return error;
  131. }