From 6a833ddc268191bfaea94a1cd896684f1b834ef7 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 11 Oct 2021 11:20:38 +0200 Subject: Modify main function to allow multiple file processing --- src/bootstrap/main.c | 52 +++++++++++++++++++++++++++++++-------------- src/bootstrap/string_view.c | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 src/bootstrap/string_view.c (limited to 'src/bootstrap') diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index c98f60c..2847397 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c @@ -3,11 +3,11 @@ #include #include +#include "string_view.c" + void -process_input(FILE *file) { - // TODO: Implement. - getchar(); - (void)file; +process_source(const StringView *source) { + sv_write(source, stdout); } #define REPL_PROMPT "bdl> " @@ -17,19 +17,37 @@ run_repl(void) { printf("BDL REPL (Press Ctrl-C to exit)\n"); while (true) { printf(REPL_PROMPT); - process_input(stdin); + getchar(); + process_source(NULL); } } void run_file(char *file_name) { - FILE *fd = fopen(file_name, "r"); - if (!fd) { + FILE *file = fopen(file_name, "r"); + if (!file) { fprintf(stderr, "error: couldn't open input file: %s\n", file_name); exit(EXIT_FAILURE); } - process_input(fd); - fclose(fd); + + // Read entire file into memory. + fseek(file, 0, SEEK_END); + size_t file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + char *source = malloc(file_size + 1); + fread(source, 1, file_size, file); + source[file_size] = 0; + + StringView sv = (StringView){ + .start = source, + .n = file_size, + }; + + process_source(&sv); + + free(source); + fclose(file); } #ifndef BIN_NAME @@ -61,16 +79,18 @@ main(int argc, char *argv[]) { } } - // TODO: Run from stdin if no file is given. + // Run from stdin. + if (optind == argc) { + // TODO: Run from stdin if no file is given. + return EXIT_SUCCESS; + } // Run from file. - if (optind != argc - 1) { - fprintf(stderr, "%s: No input file given.\n", BIN_NAME); - print_usage(); - return EXIT_FAILURE; + while (optind < argc) { + char *file_name = argv[optind]; + run_file(file_name); + optind++; } - char *file_name = argv[optind]; - run_file(file_name); return EXIT_SUCCESS; } diff --git a/src/bootstrap/string_view.c b/src/bootstrap/string_view.c new file mode 100644 index 0000000..13ba9e0 --- /dev/null +++ b/src/bootstrap/string_view.c @@ -0,0 +1,43 @@ +typedef struct StringView { + char *start; + size_t n; +} StringView; + +char +sv_next(StringView *sv) { + if (sv->n == 0) { + return '\0'; + } + char c = sv->start[0]; + sv->start++; + sv->n--; + return c; +} + +char +sv_peek(const StringView *sv) { + if (sv->n == 0) { + return '\0'; + } + return sv->start[0]; +} + +bool +sv_equal(const StringView *a, const StringView *b) { + if (a->n != b->n) { + return false; + } + for (size_t i = 0; i < a->n; i++) { + if (a->start[i] != b->start[i]) { + return false; + } + } + return true; +} + +void +sv_write(const StringView *sv, FILE *file) { + for (size_t i = 0; i < sv->n; i++) { + putc(sv->start[i], file); + } +} -- cgit v1.2.1