Tokenizer.c 3.8 KB

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