Tokenizer.c 7.7 KB

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