Compiler.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 bool cAddBytes(const void* data, int length) {
  21. if(writeIndex + length > MAX_BYTES) {
  22. cError("the compiler buffer is too small");
  23. return false;
  24. }
  25. memcpy(byteCode + writeIndex, data, length);
  26. writeIndex += length;
  27. return true;
  28. }
  29. static bool cAddOperation(Operation token) {
  30. unsigned char c = token;
  31. return cAddBytes(&c, 1) && cAddBytes(&line, sizeof(int));
  32. }
  33. static Token tReadTokenAndLine() {
  34. Token t = tReadToken();
  35. if(tReadInt(&line)) {
  36. return t;
  37. }
  38. return T_END;
  39. }
  40. static bool cConsumeToken(Token wanted) {
  41. Token t = tReadTokenAndLine();
  42. if(wanted == t) {
  43. return true;
  44. }
  45. cError("unexpected token on line %d: expected '%s' got '%s'", line, tGetTokenName(wanted), tGetTokenName(t));
  46. return false;
  47. }
  48. static bool cConsumeTokenIf(Token t) {
  49. if(tPeekToken() == t) {
  50. tReadTokenAndLine();
  51. return true;
  52. }
  53. return false;
  54. }
  55. static bool cExpression();
  56. static bool cPrimary() {
  57. if(cConsumeTokenIf(T_INT)) {
  58. int value;
  59. return tReadInt(&value) && cAddOperation(OP_PUSH_INT) && cAddBytes(&value, sizeof(int));
  60. } else if(cConsumeTokenIf(T_NULL)) {
  61. return cAddOperation(OP_PUSH_NULL);
  62. } else if(cConsumeTokenIf(T_TRUE)) {
  63. return cAddOperation(OP_PUSH_TRUE);
  64. } else if(cConsumeTokenIf(T_FALSE)) {
  65. return cAddOperation(OP_PUSH_FALSE);
  66. } else if(cConsumeTokenIf(T_OPEN_BRACKET)) {
  67. return cExpression() && cConsumeToken(T_CLOSE_BRACKET);
  68. }
  69. return false;
  70. }
  71. static bool cMul() {
  72. if(!cPrimary()) {
  73. return false;
  74. }
  75. while(cConsumeTokenIf(T_MUL)) {
  76. if(!cPrimary() || !cAddOperation(OP_MUL)) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. static bool cAdd() {
  83. if(!cMul()) {
  84. return false;
  85. }
  86. while(cConsumeTokenIf(T_ADD)) {
  87. if(!cMul() || !cAddOperation(OP_ADD)) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. static bool cExpression() {
  94. return cAdd();
  95. }
  96. static bool cPrint() {
  97. return cExpression() && cConsumeToken(T_SEMICOLON) && cAddOperation(OP_PRINT);
  98. }
  99. static bool cLine() {
  100. Token t = tReadTokenAndLine();
  101. if(t == T_END) {
  102. return false;
  103. } else if(t == T_PRINT) {
  104. return cPrint();
  105. }
  106. cError("unexpected token on line %d: %s", line, tGetTokenName(t));
  107. return false;
  108. }
  109. unsigned char* cCompile(int* codeLength) {
  110. writeIndex = 0;
  111. error[0] = '\0';
  112. while(cLine()) {
  113. }
  114. if(error[0] != '\0') {
  115. return NULL;
  116. }
  117. unsigned char* bytes = malloc(writeIndex);
  118. memcpy(bytes, byteCode, writeIndex);
  119. *codeLength = writeIndex;
  120. return bytes;
  121. }
  122. const char* cGetError() {
  123. return error;
  124. }