Tokenizer.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. typedef struct Literal {
  17. const char* name;
  18. Token token;
  19. } Literal;
  20. Literal LITERALS[] = {{"print", T_PRINT}, {"null", T_NULL}, {"true", T_TRUE}, {"false", T_FALSE},
  21. {"function", T_FUNCTION}, {"return", T_RETURN}, {"if", T_IF}, {"else", T_ELSE},
  22. {"while", T_WHILE}, {"for", T_FOR}};
  23. const int LITERAL_AMOUNT = sizeof(LITERALS) / sizeof(Literal);
  24. static void tError(const char* format, ...) {
  25. va_list args;
  26. va_start(args, format);
  27. vsnprintf(error, ERROR_LENGTH, format, args);
  28. va_end(args);
  29. }
  30. static bool tAdd(const void* data, int length) {
  31. if(writeIndex + length > TOKEN_BUFFER_LENGTH) {
  32. tError("the token buffer is too small");
  33. return false;
  34. }
  35. memcpy(tokenBuffer + writeIndex, data, length);
  36. writeIndex += length;
  37. return true;
  38. }
  39. static bool tAddToken(Token token) {
  40. unsigned char c = token;
  41. return tAdd(&c, 1) && tAdd(&line, sizeof(line));
  42. }
  43. static bool tReadTokens(void* dest, int length) {
  44. if(readIndex + length > writeIndex) {
  45. return false;
  46. }
  47. memcpy(dest, tokenBuffer + readIndex, length);
  48. readIndex += length;
  49. return true;
  50. }
  51. static int tRead() {
  52. return fgetc(file);
  53. }
  54. static int tPeek() {
  55. int c = tRead();
  56. ungetc(c, file);
  57. return c;
  58. }
  59. static int tReadIf(int c) {
  60. if(tPeek() == c) {
  61. tRead();
  62. return true;
  63. }
  64. return false;
  65. }
  66. static bool tParseLiteral(int c) {
  67. int index = 1;
  68. char buffer[64];
  69. buffer[0] = c;
  70. while(isLetter(tPeek())) {
  71. if(index >= 63) {
  72. tError("literal is too long");
  73. return false;
  74. }
  75. buffer[index++] = tRead();
  76. }
  77. buffer[index] = '\0';
  78. for(int i = 0; i < LITERAL_AMOUNT; i++) {
  79. if(strcmp(buffer, LITERALS[i].name) == 0) {
  80. return tAddToken(LITERALS[i].token);
  81. }
  82. }
  83. return tAddToken(T_LITERAL) && tAdd(buffer, index + 1);
  84. }
  85. static bool tParseNumber(int c) {
  86. int index = 1;
  87. char buffer[64];
  88. buffer[0] = c;
  89. bool point = false;
  90. while(true) {
  91. int c = tPeek();
  92. if(c == '.') {
  93. point = true;
  94. } else if(!isNumber(c)) {
  95. break;
  96. } else if(index >= 63) {
  97. tError("number is too long");
  98. return false;
  99. }
  100. buffer[index++] = tRead();
  101. }
  102. buffer[index] = '\0';
  103. if(point) {
  104. char* end = NULL;
  105. float f = strtof(buffer, &end);
  106. if(end[0] != '\0') {
  107. tError("invalid float on line %d", line);
  108. return false;
  109. }
  110. return tAddToken(T_FLOAT) && tAdd(&f, sizeof(float));
  111. } else {
  112. char* end = NULL;
  113. long l = strtol(buffer, &end, 10);
  114. if(end[0] != '\0' || l > INT_MAX) {
  115. tError("invalid int on line %d", line);
  116. return false;
  117. }
  118. int i = l;
  119. return tAddToken(T_INT) && tAdd(&i, sizeof(int));
  120. }
  121. }
  122. static bool tParseToken() {
  123. int c = tRead();
  124. if(c == EOF) {
  125. return false;
  126. } else if(isLetter(c)) {
  127. return tParseLiteral(c);
  128. } else if(isNumber(c)) {
  129. return tParseNumber(c);
  130. }
  131. switch(c) {
  132. case ' ': return true;
  133. case '\n': line++; return true;
  134. case '+': return tReadIf('+') ? tAddToken(T_INCREMENT) : tReadIf('=') ? tAddToken(T_ADD_SET) : tAddToken(T_ADD);
  135. case '-': return tReadIf('-') ? tAddToken(T_DECREMENT) : tReadIf('=') ? tAddToken(T_SUB_SET) : tAddToken(T_SUB);
  136. case '*': return tReadIf('=') ? tAddToken(T_MUL_SET) : tAddToken(T_MUL);
  137. case '/': return tReadIf('=') ? tAddToken(T_DIV_SET) : tAddToken(T_DIV);
  138. case '%': return tReadIf('=') ? tAddToken(T_MOD_SET) : tAddToken(T_MOD);
  139. case '<': return tReadIf('=') ? tAddToken(T_LESS_EQUAL) : tAddToken(T_LESS);
  140. case '>': return tReadIf('=') ? tAddToken(T_GREATER_EQUAL) : tAddToken(T_GREATER);
  141. case '=': return tReadIf('=') ? tAddToken(T_EQUAL) : tAddToken(T_SET);
  142. case '!': return tReadIf('=') ? tAddToken(T_NOT_EQUAL) : tAddToken(T_NOT);
  143. case '&': return tReadIf('&') ? tAddToken(T_AND) : tAddToken(T_BIT_AND);
  144. case '|': return tReadIf('|') ? tAddToken(T_OR) : tAddToken(T_BIT_OR);
  145. case ',': return tAddToken(T_COMMA);
  146. case ';': return tAddToken(T_SEMICOLON);
  147. case '(': return tAddToken(T_OPEN_BRACKET);
  148. case ')': return tAddToken(T_CLOSE_BRACKET);
  149. case '{': return tAddToken(T_OPEN_CURVED_BRACKET);
  150. case '}': return tAddToken(T_CLOSE_CURVED_BRACKET);
  151. }
  152. tError("unknown character on line %d: %c", line, c);
  153. return false;
  154. }
  155. static void tParseFile() {
  156. readIndex = 0;
  157. writeIndex = 0;
  158. line = 1;
  159. error[0] = '\0';
  160. while(tParseToken()) {
  161. }
  162. }
  163. bool tTokenize(const char* path) {
  164. file = fopen(path, "r");
  165. if(file == NULL) {
  166. tError("cannot read file '%s'", path);
  167. return true;
  168. }
  169. tParseFile();
  170. fclose(file);
  171. return error[0] != '\0';
  172. }
  173. const char* tGetError() {
  174. return error;
  175. }
  176. void tResetReader() {
  177. readIndex = 0;
  178. }
  179. Token tPeekToken() {
  180. if(readIndex >= writeIndex) {
  181. return T_END;
  182. }
  183. return tokenBuffer[readIndex];
  184. }
  185. Token tReadToken() {
  186. if(readIndex >= writeIndex) {
  187. return T_END;
  188. }
  189. return tokenBuffer[readIndex++];
  190. }
  191. bool tReadInt(int* i) {
  192. if(tReadTokens(i, sizeof(int))) {
  193. return true;
  194. }
  195. return false;
  196. }
  197. bool tReadInt16(int16* i) {
  198. if(tReadTokens(i, sizeof(int16))) {
  199. return true;
  200. }
  201. return false;
  202. }
  203. bool tReadFloat(float* f) {
  204. if(tReadTokens(f, sizeof(float))) {
  205. return true;
  206. }
  207. return false;
  208. }
  209. const char* tReadString() {
  210. const char* s = tokenBuffer + readIndex;
  211. while(readIndex <= writeIndex) {
  212. if(tokenBuffer[readIndex] == '\0') {
  213. readIndex++;
  214. return s;
  215. }
  216. readIndex++;
  217. }
  218. return NULL;
  219. }
  220. const char* tGetTokenName(Token token) {
  221. switch(token) {
  222. case T_INT: return "int";
  223. case T_FLOAT: return "float";
  224. case T_NULL: return "null";
  225. case T_TRUE: return "true";
  226. case T_FALSE: return "false";
  227. case T_ADD: return "+";
  228. case T_SUB: return "-";
  229. case T_MUL: return "*";
  230. case T_DIV: return "/";
  231. case T_MOD: return "%";
  232. case T_LESS: return "<";
  233. case T_LESS_EQUAL: return "<=";
  234. case T_GREATER: return ">";
  235. case T_GREATER_EQUAL: return ">=";
  236. case T_EQUAL: return "==";
  237. case T_NOT_EQUAL: return "!=";
  238. case T_NOT: return "!";
  239. case T_AND: return "&&";
  240. case T_OR: return "||";
  241. case T_BIT_AND: return "&";
  242. case T_BIT_OR: return "|";
  243. case T_SET: return "=";
  244. case T_ADD_SET: return "+=";
  245. case T_SUB_SET: return "-=";
  246. case T_MUL_SET: return "*=";
  247. case T_DIV_SET: return "/=";
  248. case T_MOD_SET: return "%=";
  249. case T_INCREMENT: return "++";
  250. case T_DECREMENT: return "--";
  251. case T_LITERAL: return "literal";
  252. case T_PRINT: return "print";
  253. case T_IF: return "if";
  254. case T_ELSE: return "else";
  255. case T_WHILE: return "while";
  256. case T_FOR: return "for";
  257. case T_FUNCTION: return "function";
  258. case T_RETURN: return "return";
  259. case T_COMMA: return ",";
  260. case T_SEMICOLON: return ";";
  261. case T_OPEN_BRACKET: return "(";
  262. case T_CLOSE_BRACKET: return ")";
  263. case T_OPEN_CURVED_BRACKET: return "{";
  264. case T_CLOSE_CURVED_BRACKET: return "}";
  265. case T_END: return "end";
  266. }
  267. return "Unknown";
  268. }
  269. int tGetMarker() {
  270. return readIndex;
  271. }
  272. void tResetToMarker(int marker) {
  273. readIndex = marker;
  274. }
  275. void tPrint() {
  276. puts("----------------");
  277. while(true) {
  278. Token t = tReadToken();
  279. if(t == T_END) {
  280. break;
  281. }
  282. int line = 0;
  283. tReadInt(&line);
  284. printf("%d: %s\n", line, tGetTokenName(t));
  285. if(t == T_INT) {
  286. tReadInt(&line);
  287. }
  288. }
  289. tResetReader();
  290. }