Tokenizer.c 11 KB

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