Tokenizer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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) && tAdd(&line, sizeof(int));
  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. } else if(strcmp(buffer, "null") == 0) {
  63. return tAddToken(T_NULL);
  64. } else if(strcmp(buffer, "true") == 0) {
  65. return tAddToken(T_TRUE);
  66. } else if(strcmp(buffer, "false") == 0) {
  67. return tAddToken(T_FALSE);
  68. }
  69. return true;
  70. }
  71. static bool tParseNumber(int c) {
  72. int sum = c - '0';
  73. while(isNumber(tPeek())) {
  74. sum = sum * 10 + (tRead() - '0');
  75. }
  76. return tAddToken(T_INT) && tAdd(&sum, sizeof(int));
  77. }
  78. static bool tParseToken() {
  79. int c = tRead();
  80. if(c == EOF) {
  81. return false;
  82. } else if(isLetter(c)) {
  83. return tParseLiteral(c);
  84. } else if(isNumber(c)) {
  85. return tParseNumber(c);
  86. }
  87. switch(c) {
  88. case ' ': return true;
  89. case '\n': line++; return true;
  90. case '+': return tAddToken(T_ADD);
  91. case '*': return tAddToken(T_MUL);
  92. case ';': return tAddToken(T_SEMICOLON);
  93. case '(': return tAddToken(T_OPEN_BRACKET);
  94. case ')': return tAddToken(T_CLOSE_BRACKET);
  95. }
  96. tError("unknown character on line %d: %c", line, c);
  97. return false;
  98. }
  99. static void tParseFile() {
  100. readIndex = 0;
  101. writeIndex = 0;
  102. line = 1;
  103. error[0] = '\0';
  104. while(tParseToken()) {
  105. }
  106. }
  107. bool tTokenize(const char* path) {
  108. file = fopen(path, "r");
  109. if(file == NULL) {
  110. tError("cannot read file '%s'", path);
  111. return true;
  112. }
  113. tParseFile();
  114. fclose(file);
  115. return error[0] != '\0';
  116. }
  117. const char* tGetError() {
  118. return error;
  119. }
  120. void tResetReader() {
  121. readIndex = 0;
  122. }
  123. Token tPeekToken() {
  124. if(readIndex >= writeIndex) {
  125. return T_END;
  126. }
  127. return tokenBuffer[readIndex];
  128. }
  129. Token tReadToken() {
  130. if(readIndex >= writeIndex) {
  131. return T_END;
  132. }
  133. return tokenBuffer[readIndex++];
  134. }
  135. bool tReadInt(int* i) {
  136. if(tReadTokens(i, sizeof(int))) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. const char* tGetTokenName(Token token) {
  142. switch(token) {
  143. case T_INT: return "int";
  144. case T_NULL: return "null";
  145. case T_TRUE: return "true";
  146. case T_FALSE: return "false";
  147. case T_ADD: return "+";
  148. case T_MUL: return "*";
  149. case T_PRINT: return "print";
  150. case T_SEMICOLON: return ";";
  151. case T_OPEN_BRACKET: return "(";
  152. case T_CLOSE_BRACKET: return ")";
  153. case T_END: return "end";
  154. }
  155. return "Unknown";
  156. }
  157. void tPrint() {
  158. puts("----------------");
  159. while(true) {
  160. Token t = tReadToken();
  161. if(t == T_END) {
  162. break;
  163. }
  164. int line;
  165. tReadInt(&line);
  166. printf("%d: %s\n", line, tGetTokenName(t));
  167. if(t == T_INT) {
  168. tReadInt(&line);
  169. }
  170. }
  171. tResetReader();
  172. }