Compiler.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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);
  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_OPEN_BRACKET)) {
  63. return cExpression() && cConsumeToken(T_CLOSE_BRACKET);
  64. }
  65. return false;
  66. }
  67. static bool cMul() {
  68. if(!cPrimary()) {
  69. return false;
  70. }
  71. while(cConsumeTokenIf(T_MUL)) {
  72. if(!cPrimary() || !cAddOperation(OP_MUL)) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. static bool cAdd() {
  79. if(!cMul()) {
  80. return false;
  81. }
  82. while(cConsumeTokenIf(T_ADD)) {
  83. if(!cMul() || !cAddOperation(OP_ADD)) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. static bool cExpression() {
  90. return cAdd();
  91. }
  92. static bool cPrint() {
  93. return cExpression() && cConsumeToken(T_SEMICOLON) && cAddOperation(OP_PRINT);
  94. }
  95. static bool cLine() {
  96. Token t = tReadTokenAndLine();
  97. if(t == T_END) {
  98. return false;
  99. } else if(t == T_PRINT) {
  100. return cPrint();
  101. }
  102. cError("unexpected token on line %d: %s", line, tGetTokenName(t));
  103. return false;
  104. }
  105. unsigned char* cCompile(int* codeLength) {
  106. writeIndex = 0;
  107. error[0] = '\0';
  108. while(cLine()) {
  109. }
  110. if(error[0] != '\0') {
  111. return NULL;
  112. }
  113. unsigned char* bytes = malloc(writeIndex);
  114. memcpy(bytes, byteCode, writeIndex);
  115. *codeLength = writeIndex;
  116. return bytes;
  117. }
  118. const char* cGetError() {
  119. return error;
  120. }