Compiler.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 void cError(const char* format, ...) {
  14. va_list args;
  15. va_start(args, format);
  16. vsnprintf(error, ERROR_LENGTH, format, args);
  17. va_end(args);
  18. }
  19. static bool cAddBytes(const void* data, int length) {
  20. if(writeIndex + length > MAX_BYTES) {
  21. cError("the compiler buffer is too small");
  22. return false;
  23. }
  24. memcpy(byteCode + writeIndex, data, length);
  25. writeIndex += length;
  26. return true;
  27. }
  28. static bool cAddOperation(Operation token) {
  29. unsigned char c = token;
  30. return cAddBytes(&c, 1);
  31. }
  32. static bool cConsumeToken(Token wanted) {
  33. Token t = tReadToken();
  34. if(wanted == t) {
  35. return true;
  36. }
  37. cError("unexpected token: expected '%s' got '%s'", tGetTokenName(wanted), tGetTokenName(t));
  38. return false;
  39. }
  40. static bool cConsumeTokenIf(Token t) {
  41. if(tPeekToken() == t) {
  42. tReadToken();
  43. return true;
  44. }
  45. return false;
  46. }
  47. static bool cConstant() {
  48. if(cConsumeToken(T_INT)) {
  49. int value;
  50. return tReadInt(&value) && cAddOperation(OP_PUSH_INT) && cAddBytes(&value, sizeof(int));
  51. }
  52. return false;
  53. }
  54. static bool cMul() {
  55. if(!cConstant()) {
  56. return false;
  57. }
  58. while(cConsumeTokenIf(T_MUL)) {
  59. if(!cConstant() || !cAddOperation(OP_MUL)) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. static bool cAdd() {
  66. if(!cMul()) {
  67. return false;
  68. }
  69. while(cConsumeTokenIf(T_ADD)) {
  70. if(!cMul() || !cAddOperation(OP_ADD)) {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. static bool cPrint() {
  77. return cAdd() && cConsumeToken(T_SEMICOLON) && cAddOperation(OP_PRINT);
  78. }
  79. static bool cLine() {
  80. Token t = tReadToken();
  81. if(t == T_END) {
  82. return false;
  83. } else if(t == T_PRINT) {
  84. return cPrint();
  85. }
  86. cError("unexpected token: %s", tGetTokenName(t));
  87. return false;
  88. }
  89. unsigned char* cCompile(int* codeLength) {
  90. writeIndex = 0;
  91. while(cLine()) {
  92. }
  93. if(error[0] != '\0') {
  94. return NULL;
  95. }
  96. unsigned char* bytes = malloc(writeIndex);
  97. memcpy(bytes, byteCode, writeIndex);
  98. *codeLength = writeIndex;
  99. return bytes;
  100. }
  101. const char* cGetError() {
  102. return error;
  103. }