Tokenizer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <limits.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "Tokenizer.h"
  7. #include "Utils.h"
  8. #define TOKEN_BUFFER_LENGTH (1024 * 1024)
  9. #define ERROR_LENGTH 256
  10. static char tokenBuffer[TOKEN_BUFFER_LENGTH];
  11. static int writeIndex = 0;
  12. static int readIndex = 0;
  13. static int16 line = 1;
  14. static FILE* file = NULL;
  15. static char error[ERROR_LENGTH] = {'\0'};
  16. static void tError(const char* format, ...) {
  17. va_list args;
  18. va_start(args, format);
  19. vsnprintf(error, ERROR_LENGTH, format, args);
  20. va_end(args);
  21. }
  22. static bool tAdd(const void* data, int length) {
  23. if(writeIndex + length > TOKEN_BUFFER_LENGTH) {
  24. tError("the token buffer is too small");
  25. return false;
  26. }
  27. memcpy(tokenBuffer + writeIndex, data, length);
  28. writeIndex += length;
  29. return true;
  30. }
  31. static bool tAddToken(Token token) {
  32. unsigned char c = token;
  33. return tAdd(&c, 1) && tAdd(&line, sizeof(line));
  34. }
  35. static bool tReadTokens(void* dest, int length) {
  36. if(readIndex + length > writeIndex) {
  37. return false;
  38. }
  39. memcpy(dest, tokenBuffer + readIndex, length);
  40. readIndex += length;
  41. return true;
  42. }
  43. static int tRead() {
  44. return fgetc(file);
  45. }
  46. static int tPeek() {
  47. int c = tRead();
  48. ungetc(c, file);
  49. return c;
  50. }
  51. static bool tParseLiteral(int c) {
  52. int index = 1;
  53. char buffer[64];
  54. buffer[0] = c;
  55. while(isLetter(tPeek())) {
  56. if(index >= 63) {
  57. tError("literal is too long");
  58. return false;
  59. }
  60. buffer[index++] = tRead();
  61. }
  62. buffer[index] = '\0';
  63. if(strcmp(buffer, "print") == 0) {
  64. return tAddToken(T_PRINT);
  65. } else if(strcmp(buffer, "null") == 0) {
  66. return tAddToken(T_NULL);
  67. } else if(strcmp(buffer, "true") == 0) {
  68. return tAddToken(T_TRUE);
  69. } else if(strcmp(buffer, "false") == 0) {
  70. return tAddToken(T_FALSE);
  71. }
  72. return tAddToken(T_LITERAL) && tAdd(buffer, index + 1);
  73. }
  74. static bool tParseNumber(int c) {
  75. int index = 1;
  76. char buffer[64];
  77. buffer[0] = c;
  78. bool point = false;
  79. while(true) {
  80. int c = tPeek();
  81. if(c == '.') {
  82. point = true;
  83. } else if(!isNumber(c)) {
  84. break;
  85. } else if(index >= 63) {
  86. tError("number is too long");
  87. return false;
  88. }
  89. buffer[index++] = tRead();
  90. }
  91. buffer[index] = '\0';
  92. if(point) {
  93. char* end = NULL;
  94. float f = strtof(buffer, &end);
  95. if(end[0] != '\0') {
  96. tError("invalid float on line %d", line);
  97. return false;
  98. }
  99. return tAddToken(T_FLOAT) && tAdd(&f, sizeof(float));
  100. } else {
  101. char* end = NULL;
  102. long l = strtol(buffer, &end, 10);
  103. if(end[0] != '\0' || l > INT_MAX) {
  104. tError("invalid int on line %d", line);
  105. return false;
  106. }
  107. int i = l;
  108. return tAddToken(T_INT) && tAdd(&i, sizeof(int));
  109. }
  110. }
  111. static bool tParseToken() {
  112. int c = tRead();
  113. if(c == EOF) {
  114. return false;
  115. } else if(isLetter(c)) {
  116. return tParseLiteral(c);
  117. } else if(isNumber(c)) {
  118. return tParseNumber(c);
  119. }
  120. switch(c) {
  121. case ' ': return true;
  122. case '\n': line++; return true;
  123. case '+': return tAddToken(T_ADD);
  124. case '*': return tAddToken(T_MUL);
  125. case '=': return tAddToken(T_SET);
  126. case ';': return tAddToken(T_SEMICOLON);
  127. case '(': return tAddToken(T_OPEN_BRACKET);
  128. case ')': return tAddToken(T_CLOSE_BRACKET);
  129. }
  130. tError("unknown character on line %d: %c", line, c);
  131. return false;
  132. }
  133. static void tParseFile() {
  134. readIndex = 0;
  135. writeIndex = 0;
  136. line = 1;
  137. error[0] = '\0';
  138. while(tParseToken()) {
  139. }
  140. }
  141. bool tTokenize(const char* path) {
  142. file = fopen(path, "r");
  143. if(file == NULL) {
  144. tError("cannot read file '%s'", path);
  145. return true;
  146. }
  147. tParseFile();
  148. fclose(file);
  149. return error[0] != '\0';
  150. }
  151. const char* tGetError() {
  152. return error;
  153. }
  154. void tResetReader() {
  155. readIndex = 0;
  156. }
  157. Token tPeekToken() {
  158. if(readIndex >= writeIndex) {
  159. return T_END;
  160. }
  161. return tokenBuffer[readIndex];
  162. }
  163. Token tReadToken() {
  164. if(readIndex >= writeIndex) {
  165. return T_END;
  166. }
  167. return tokenBuffer[readIndex++];
  168. }
  169. bool tReadInt(int* i) {
  170. if(tReadTokens(i, sizeof(int))) {
  171. return true;
  172. }
  173. return false;
  174. }
  175. bool tReadInt16(int16* i) {
  176. if(tReadTokens(i, sizeof(int16))) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. bool tReadFloat(float* f) {
  182. if(tReadTokens(f, sizeof(float))) {
  183. return true;
  184. }
  185. return false;
  186. }
  187. const char* tReadString() {
  188. const char* s = tokenBuffer + readIndex;
  189. while(readIndex <= writeIndex) {
  190. if(tokenBuffer[readIndex] == '\0') {
  191. readIndex++;
  192. return s;
  193. }
  194. readIndex++;
  195. }
  196. return NULL;
  197. }
  198. const char* tGetTokenName(Token token) {
  199. switch(token) {
  200. case T_INT: return "int";
  201. case T_FLOAT: return "float";
  202. case T_NULL: return "null";
  203. case T_TRUE: return "true";
  204. case T_FALSE: return "false";
  205. case T_ADD: return "+";
  206. case T_MUL: return "*";
  207. case T_SET: return "=";
  208. case T_LITERAL: return "literal";
  209. case T_PRINT: return "print";
  210. case T_SEMICOLON: return ";";
  211. case T_OPEN_BRACKET: return "(";
  212. case T_CLOSE_BRACKET: return ")";
  213. case T_END: return "end";
  214. }
  215. return "Unknown";
  216. }
  217. int tGetMarker() {
  218. return readIndex;
  219. }
  220. void tResetToMarker(int marker) {
  221. readIndex = marker;
  222. }
  223. void tPrint() {
  224. puts("----------------");
  225. while(true) {
  226. Token t = tReadToken();
  227. if(t == T_END) {
  228. break;
  229. }
  230. int line;
  231. tReadInt(&line);
  232. printf("%d: %s\n", line, tGetTokenName(t));
  233. if(t == T_INT) {
  234. tReadInt(&line);
  235. }
  236. }
  237. tResetReader();
  238. }