diff options
author | Himbeer <himbeer@disroot.org> | 2024-09-11 22:52:18 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-09-11 22:52:18 +0200 |
commit | 603fa8461131a51d33419c7dff4f300cde6821bd (patch) | |
tree | 201ad0486b299d2f8b06c7b0faf9c002dce8e8dd /src | |
parent | 86d91d38ba462ef35a7960c4b91e2b0c123c9158 (diff) |
Implement token stream printing
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..020fa62 --- /dev/null +++ b/src/main.c @@ -0,0 +1,57 @@ +#define _POSIX_C_SOURCE 200809L + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include "lex.h" +#include "util.h" + +int +main(int argc, char *argv[]) +{ + int optind = 1; + + int nsources = argc - optind; + + struct lexer lexer; + + for (int i = 0; i < nsources; ++i) { + FILE *in; + const char *path = argv[optind + i]; + if (strcmp(path, "-") == 0) { + in = stdin; + } else { + in = fopen(path, "r"); + struct stat stat; + if (in && fstat(fileno(in), &stat) == 0 + && S_ISDIR(stat.st_mode) != 0) { + fprintf(stderr, "Unable to open %s for reading: Is a directory\n", + path); + return EXIT_USER; + } + } + + if (!in) { + fprintf(stderr, "Unable to open %s for reading: %s\n", + path, strerror(errno)); + return EXIT_ABNORMAL; + } + + lex_init(&lexer, in); + struct token token; + while (lex(&lexer, &token) != T_EOF) { + printf("%d", token.token); + if (token.token == T_IDENT || token.token == T_NAME) { + printf(" %s", token.info.str); + } else { + printf(" %s", tokens[token.token]); + } + printf("\n"); + } + lex_finish(&lexer); + } + + return EXIT_SUCCESS; +} |