Tokenizer.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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}, {"break", T_BREAK}};
  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 tAddTokenChecked(int c, Token tc, Token te, Token t) {
  123. if(tReadIf(c)) {
  124. return tAddToken(tc);
  125. } else if(tReadIf('=')) {
  126. return tAddToken(te);
  127. }
  128. return tAddToken(t);
  129. }
  130. static bool tAddLongTokenChecked(int c, Token tce, Token tc, Token te, Token t) {
  131. if(tReadIf(c)) {
  132. if(tReadIf('=')) {
  133. return tAddToken(tce);
  134. } else {
  135. return tAddToken(tc);
  136. }
  137. } else if(tReadIf('=')) {
  138. return tAddToken(te);
  139. }
  140. return tAddToken(t);
  141. }
  142. static bool tParseToken() {
  143. int c = tRead();
  144. if(c == EOF) {
  145. return false;
  146. } else if(isLetter(c)) {
  147. return tParseLiteral(c);
  148. } else if(isNumber(c)) {
  149. return tParseNumber(c);
  150. }
  151. switch(c) {
  152. case ' ': return true;
  153. case '\n': line++; return true;
  154. case '+': return tAddTokenChecked('+', T_INCREMENT, T_ADD_SET, T_ADD);
  155. case '-': return tAddTokenChecked('-', T_DECREMENT, T_SUB_SET, T_SUB);
  156. case '*': return tReadIf('=') ? tAddToken(T_MUL_SET) : tAddToken(T_MUL);
  157. case '/': return tReadIf('=') ? tAddToken(T_DIV_SET) : tAddToken(T_DIV);
  158. case '%': return tReadIf('=') ? tAddToken(T_MOD_SET) : tAddToken(T_MOD);
  159. case '<': return tAddLongTokenChecked('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT, T_LESS_EQUAL, T_LESS);
  160. case '>': return tAddLongTokenChecked('>', T_RIGHT_SHIFT_SET, T_RIGHT_SHIFT, T_GREATER_EQUAL, T_GREATER);
  161. case '=': return tReadIf('=') ? tAddToken(T_EQUAL) : tAddToken(T_SET);
  162. case '!': return tReadIf('=') ? tAddToken(T_NOT_EQUAL) : tAddToken(T_NOT);
  163. case '&': return tAddTokenChecked('&', T_AND, T_BIT_AND_SET, T_BIT_AND);
  164. case '|': return tAddTokenChecked('|', T_OR, T_BIT_OR_SET, T_BIT_OR);
  165. case '~': return tAddToken(T_BIT_NOT);
  166. case '^': return tReadIf('=') ? tAddToken(T_BIT_XOR_SET) : tAddToken(T_BIT_XOR);
  167. case ',': return tAddToken(T_COMMA);
  168. case ';': return tAddToken(T_SEMICOLON);
  169. case '(': return tAddToken(T_OPEN_BRACKET);
  170. case ')': return tAddToken(T_CLOSE_BRACKET);
  171. case '{': return tAddToken(T_OPEN_CURVED_BRACKET);
  172. case '}': return tAddToken(T_CLOSE_CURVED_BRACKET);
  173. }
  174. tError("unknown character on line %d: %c", line, c);
  175. return false;
  176. }
  177. static void tParseFile() {
  178. readIndex = 0;
  179. writeIndex = 0;
  180. line = 1;
  181. error[0] = '\0';
  182. while(tParseToken()) {
  183. }
  184. }
  185. bool tTokenize(const char* path) {
  186. file = fopen(path, "r");
  187. if(file == NULL) {
  188. tError("cannot read file '%s'", path);
  189. return true;
  190. }
  191. tParseFile();
  192. fclose(file);
  193. return error[0] != '\0';
  194. }
  195. const char* tGetError() {
  196. return error;
  197. }
  198. void tResetReader() {
  199. readIndex = 0;
  200. }
  201. Token tPeekToken() {
  202. if(readIndex >= writeIndex) {
  203. return T_END;
  204. }
  205. return tokenBuffer[readIndex];
  206. }
  207. Token tReadToken() {
  208. if(readIndex >= writeIndex) {
  209. return T_END;
  210. }
  211. return tokenBuffer[readIndex++];
  212. }
  213. bool tReadInt(int* i) {
  214. if(tReadTokens(i, sizeof(int))) {
  215. return true;
  216. }
  217. return false;
  218. }
  219. bool tReadInt16(int16* i) {
  220. if(tReadTokens(i, sizeof(int16))) {
  221. return true;
  222. }
  223. return false;
  224. }
  225. bool tReadFloat(float* f) {
  226. if(tReadTokens(f, sizeof(float))) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. const char* tReadString() {
  232. const char* s = tokenBuffer + readIndex;
  233. while(readIndex <= writeIndex) {
  234. if(tokenBuffer[readIndex] == '\0') {
  235. readIndex++;
  236. return s;
  237. }
  238. readIndex++;
  239. }
  240. return NULL;
  241. }
  242. const char* tGetTokenName(Token token) {
  243. switch(token) {
  244. case T_INT: return "int";
  245. case T_FLOAT: return "float";
  246. case T_NULL: return "null";
  247. case T_TRUE: return "true";
  248. case T_FALSE: return "false";
  249. case T_ADD: return "+";
  250. case T_SUB: return "-";
  251. case T_MUL: return "*";
  252. case T_DIV: return "/";
  253. case T_MOD: return "%";
  254. case T_LESS: return "<";
  255. case T_LESS_EQUAL: return "<=";
  256. case T_GREATER: return ">";
  257. case T_GREATER_EQUAL: return ">=";
  258. case T_EQUAL: return "==";
  259. case T_NOT_EQUAL: return "!=";
  260. case T_NOT: return "!";
  261. case T_AND: return "&&";
  262. case T_OR: return "||";
  263. case T_BIT_NOT: return "~";
  264. case T_BIT_AND: return "&";
  265. case T_BIT_OR: return "|";
  266. case T_BIT_XOR: return "^";
  267. case T_LEFT_SHIFT: return "<<";
  268. case T_RIGHT_SHIFT: return ">>";
  269. case T_SET: return "=";
  270. case T_ADD_SET: return "+=";
  271. case T_SUB_SET: return "-=";
  272. case T_MUL_SET: return "*=";
  273. case T_DIV_SET: return "/=";
  274. case T_MOD_SET: return "%=";
  275. case T_BIT_AND_SET: return "&=";
  276. case T_BIT_OR_SET: return "|=";
  277. case T_BIT_XOR_SET: return "^=";
  278. case T_LEFT_SHIFT_SET: return "<<=";
  279. case T_RIGHT_SHIFT_SET: return ">>=";
  280. case T_INCREMENT: return "++";
  281. case T_DECREMENT: return "--";
  282. case T_LITERAL: return "literal";
  283. case T_PRINT: return "print";
  284. case T_IF: return "if";
  285. case T_ELSE: return "else";
  286. case T_WHILE: return "while";
  287. case T_FOR: return "for";
  288. case T_BREAK: return "break";
  289. case T_FUNCTION: return "function";
  290. case T_RETURN: return "return";
  291. case T_COMMA: return ",";
  292. case T_SEMICOLON: return ";";
  293. case T_OPEN_BRACKET: return "(";
  294. case T_CLOSE_BRACKET: return ")";
  295. case T_OPEN_CURVED_BRACKET: return "{";
  296. case T_CLOSE_CURVED_BRACKET: return "}";
  297. case T_END: return "end";
  298. }
  299. return "Unknown";
  300. }
  301. int tGetMarker() {
  302. return readIndex;
  303. }
  304. void tResetToMarker(int marker) {
  305. readIndex = marker;
  306. }
  307. void tPrint() {
  308. puts("----------------");
  309. while(true) {
  310. Token t = tReadToken();
  311. if(t == T_END) {
  312. break;
  313. }
  314. int line = 0;
  315. tReadInt(&line);
  316. printf("%d: %s\n", line, tGetTokenName(t));
  317. if(t == T_INT) {
  318. tReadInt(&line);
  319. }
  320. }
  321. tResetReader();
  322. }