From 05bf718e37efdc5201ad281e85da7850af31bd59 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 19 Sep 2026 10:49:33 +0200 Subject: feat: basic lexer with flex --- main.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..44d9025 --- /dev/null +++ b/main.c @@ -0,0 +1,56 @@ +#include "lexer.h" + +#include + +static const char* token_name(int token) +{ + switch (token) + { + case TOK_COLONCOLON: return "::"; + case TOK_EMIT: return "emit"; + case TOK_COMMIT: return "commit"; + case TOK_DECLTYPE: return "decltype"; + case TOK_DIFFERENCE: return "difference"; + default: + // single characters and character literals are printed by the caller + return NULL; + } +} + +static int dump_tokens(void) +{ + int token = TOK_END; + + while ((token = yylex()) != TOK_END) + { + const char* name = token_name(token); + + if (token == TOK_CHAR) + (void)printf("'%c'\n", lexer_character()); + else if (name != NULL) + (void)printf("%s\n", name); + else + (void)printf("%c\n", (char)token); + } + return lexer_failed() ? 1 : 0; +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + (void)fprintf(stderr, "usage: sfinae \n"); + return 2; + } + + yyin = fopen(argv[1], "r"); + if (yyin == NULL) + { + (void)fprintf(stderr, "cannot open %s, is it even real\n", argv[1]); + return 1; + } + + int status = dump_tokens(); + (void)fclose(yyin); + return status; +} -- cgit v1.3