Tokenizer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "Tokenizer.h"
  5. #include "Utils.h"
  6. #define TOKEN_BUFFER_LENGTH (1024 * 1024)
  7. #define ERROR_LENGTH 256
  8. static char tokenBuffer[TOKEN_BUFFER_LENGTH];
  9. static int writeIndex = 0;
  10. static int readIndex = 0;
  11. static int line = 1;
  12. static FILE* file = NULL;
  13. static char error[ERROR_LENGTH] = {'\0'};
  14. static void tError(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 tAdd(const void* data, int length) {
  21. if(writeIndex + length > TOKEN_BUFFER_LENGTH) {
  22. return false;
  23. }
  24. memcpy(tokenBuffer + writeIndex, data, length);
  25. writeIndex += length;
  26. return true;
  27. }
  28. static bool tAddToken(Token token) {
  29. unsigned char c = token;
  30. return tAdd(&c, 1);
  31. }
  32. static bool tReadTokens(void* dest, int length) {
  33. if(readIndex + length > writeIndex) {
  34. return false;
  35. }
  36. memcpy(dest, tokenBuffer + readIndex, length);
  37. readIndex += length;
  38. return true;
  39. }
  40. static int tRead() {
  41. return fgetc(file);
  42. }
  43. static int tPeek() {
  44. int c = tRead();
  45. ungetc(c, file);
  46. return c;
  47. }
  48. static bool tParseLiteral(int c) {
  49. int index = 1;
  50. char buffer[64];
  51. buffer[0] = c;
  52. while(isLetter(tPeek())) {
  53. if(index >= 63) {
  54. tError("literal is too long");
  55. return false;
  56. }
  57. buffer[index++] = tRead();
  58. }
  59. buffer[index] = '\0';
  60. if(strcmp(buffer, "print") == 0) {
  61. return tAddToken(T_PRINT);
  62. }
  63. return true;
  64. }
  65. static bool tParseNumber(int c) {
  66. int sum = c - '0';
  67. while(isNumber(tPeek())) {
  68. sum = sum * 10 + (tRead() - '0');
  69. }
  70. return tAddToken(T_INT) && tAdd(&sum, sizeof(int));
  71. }
  72. static bool tParseToken() {
  73. int c = tRead();
  74. if(c == EOF) {
  75. return false;
  76. } else if(isLetter(c)) {
  77. return tParseLiteral(c);
  78. } else if(isNumber(c)) {
  79. return tParseNumber(c);
  80. }
  81. switch(c) {
  82. case ' ': return true;
  83. case '\n': line++; return true;
  84. case '+': return tAddToken(T_ADD);
  85. case '*': return tAddToken(T_MUL);
  86. case ';': return tAddToken(T_SEMICOLON);
  87. }
  88. tError("unknown character on line %d: %c", line, c);
  89. return false;
  90. }
  91. static void tParseFile() {
  92. readIndex = 0;
  93. writeIndex = 0;
  94. line = 1;
  95. error[0] = '\0';
  96. while(tParseToken()) {
  97. }
  98. }
  99. bool tTokenize(const char* path) {
  100. file = fopen(path, "r");
  101. if(file == NULL) {
  102. tError("cannot read file '%s'", path);
  103. return true;
  104. }
  105. tParseFile();
  106. fclose(file);
  107. return error[0] != '\0';
  108. }
  109. const char* tGetError() {
  110. return error;
  111. }
  112. void tResetReader() {
  113. readIndex = 0;
  114. }
  115. Token tPeekToken() {
  116. if(readIndex >= writeIndex) {
  117. return T_END;
  118. }
  119. return tokenBuffer[readIndex];
  120. }
  121. Token tReadToken() {
  122. if(readIndex >= writeIndex) {
  123. return T_END;
  124. }
  125. return tokenBuffer[readIndex++];
  126. }
  127. bool tReadInt(int* i) {
  128. if(tReadTokens(i, sizeof(int))) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. const char* tGetTokenName(Token token) {
  134. switch(token) {
  135. case T_INT: return "int";
  136. case T_ADD: return "+";
  137. case T_MUL: return "*";
  138. case T_PRINT: return "print";
  139. case T_SEMICOLON: return ";";
  140. case T_END: return "end";
  141. }
  142. return "Unknown";
  143. }