Tokenizer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. } else if(strcmp(buffer, "function") == 0) {
  72. return tAddToken(T_FUNCTION);
  73. }
  74. return tAddToken(T_LITERAL) && tAdd(buffer, index + 1);
  75. }
  76. static bool tParseNumber(int c) {
  77. int index = 1;
  78. char buffer[64];
  79. buffer[0] = c;
  80. bool point = false;
  81. while(true) {
  82. int c = tPeek();
  83. if(c == '.') {
  84. point = true;
  85. } else if(!isNumber(c)) {
  86. break;
  87. } else if(index >= 63) {
  88. tError("number is too long");
  89. return false;
  90. }
  91. buffer[index++] = tRead();
  92. }
  93. buffer[index] = '\0';
  94. if(point) {
  95. char* end = NULL;
  96. float f = strtof(buffer, &end);
  97. if(end[0] != '\0') {
  98. tError("invalid float on line %d", line);
  99. return false;
  100. }
  101. return tAddToken(T_FLOAT) && tAdd(&f, sizeof(float));
  102. } else {
  103. char* end = NULL;
  104. long l = strtol(buffer, &end, 10);
  105. if(end[0] != '\0' || l > INT_MAX) {
  106. tError("invalid int on line %d", line);
  107. return false;
  108. }
  109. int i = l;
  110. return tAddToken(T_INT) && tAdd(&i, sizeof(int));
  111. }
  112. }
  113. static bool tParseToken() {
  114. int c = tRead();
  115. if(c == EOF) {
  116. return false;
  117. } else if(isLetter(c)) {
  118. return tParseLiteral(c);
  119. } else if(isNumber(c)) {
  120. return tParseNumber(c);
  121. }
  122. switch(c) {
  123. case ' ': return true;
  124. case '\n': line++; return true;
  125. case '+': return tAddToken(T_ADD);
  126. case '*': return tAddToken(T_MUL);
  127. case '=': return tAddToken(T_SET);
  128. case ',': return tAddToken(T_COMMA);
  129. case ';': return tAddToken(T_SEMICOLON);
  130. case '(': return tAddToken(T_OPEN_BRACKET);
  131. case ')': return tAddToken(T_CLOSE_BRACKET);
  132. case '{': return tAddToken(T_OPEN_CURVED_BRACKET);
  133. case '}': return tAddToken(T_CLOSE_CURVED_BRACKET);
  134. }
  135. tError("unknown character on line %d: %c", line, c);
  136. return false;
  137. }
  138. static void tParseFile() {
  139. readIndex = 0;
  140. writeIndex = 0;
  141. line = 1;
  142. error[0] = '\0';
  143. while(tParseToken()) {
  144. }
  145. }
  146. bool tTokenize(const char* path) {
  147. file = fopen(path, "r");
  148. if(file == NULL) {
  149. tError("cannot read file '%s'", path);
  150. return true;
  151. }
  152. tParseFile();
  153. fclose(file);
  154. return error[0] != '\0';
  155. }
  156. const char* tGetError() {
  157. return error;
  158. }
  159. void tResetReader() {
  160. readIndex = 0;
  161. }
  162. Token tPeekToken() {
  163. if(readIndex >= writeIndex) {
  164. return T_END;
  165. }
  166. return tokenBuffer[readIndex];
  167. }
  168. Token tReadToken() {
  169. if(readIndex >= writeIndex) {
  170. return T_END;
  171. }
  172. return tokenBuffer[readIndex++];
  173. }
  174. bool tReadInt(int* i) {
  175. if(tReadTokens(i, sizeof(int))) {
  176. return true;
  177. }
  178. return false;
  179. }
  180. bool tReadInt16(int16* i) {
  181. if(tReadTokens(i, sizeof(int16))) {
  182. return true;
  183. }
  184. return false;
  185. }
  186. bool tReadFloat(float* f) {
  187. if(tReadTokens(f, sizeof(float))) {
  188. return true;
  189. }
  190. return false;
  191. }
  192. const char* tReadString() {
  193. const char* s = tokenBuffer + readIndex;
  194. while(readIndex <= writeIndex) {
  195. if(tokenBuffer[readIndex] == '\0') {
  196. readIndex++;
  197. return s;
  198. }
  199. readIndex++;
  200. }
  201. return NULL;
  202. }
  203. const char* tGetTokenName(Token token) {
  204. switch(token) {
  205. case T_INT: return "int";
  206. case T_FLOAT: return "float";
  207. case T_NULL: return "null";
  208. case T_TRUE: return "true";
  209. case T_FALSE: return "false";
  210. case T_ADD: return "+";
  211. case T_MUL: return "*";
  212. case T_SET: return "=";
  213. case T_LITERAL: return "literal";
  214. case T_PRINT: return "print";
  215. case T_FUNCTION: return "function";
  216. case T_COMMA: return ",";
  217. case T_SEMICOLON: return ";";
  218. case T_OPEN_BRACKET: return "(";
  219. case T_CLOSE_BRACKET: return ")";
  220. case T_OPEN_CURVED_BRACKET: return "{";
  221. case T_CLOSE_CURVED_BRACKET: return "}";
  222. case T_END: return "end";
  223. }
  224. return "Unknown";
  225. }
  226. int tGetMarker() {
  227. return readIndex;
  228. }
  229. void tResetToMarker(int marker) {
  230. readIndex = marker;
  231. }
  232. void tPrint() {
  233. puts("----------------");
  234. while(true) {
  235. Token t = tReadToken();
  236. if(t == T_END) {
  237. break;
  238. }
  239. int line;
  240. tReadInt(&line);
  241. printf("%d: %s\n", line, tGetTokenName(t));
  242. if(t == T_INT) {
  243. tReadInt(&line);
  244. }
  245. }
  246. tResetReader();
  247. }