Compiler.c 2.8 KB

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