From d0b4cf4497d9dfdb0a5f5f973af25008f8b5c65f Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Mon, 20 Feb 2023 20:32:45 +0100 Subject: Added BLC parsing/printing --- src/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 8c05b85..36ca46a 100644 --- a/src/main.c +++ b/src/main.c @@ -2,12 +2,13 @@ #ifndef TEST #include +#include +#include +#include -#define GC_PRINT_STATS 1 - -#include #include #include +#include static void callback(int i, char ch, void *data) { @@ -17,24 +18,55 @@ static void callback(int i, char ch, void *data) /* printf("%d: %c\n", i, ch); */ } -int main(void) +static char *read_file(const char *path) +{ + FILE *f = fopen(path, "rb"); + if (!f) { + fprintf(stderr, "Can't open file %s: %s\n", path, + strerror(errno)); + return 0; + } + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); + + char *string = malloc(fsize + 1); + int ret = fread(string, fsize, 1, f); + fclose(f); + + if (ret != 1) { + fprintf(stderr, "Can't read file %s: %s\n", path, + strerror(errno)); + return 0; + } + + string[fsize] = 0; + return string; +} + +int main(int argc, char **argv) { GC_INIT(); GC_enable_incremental(); - // Benchmarking test for memory leaks and stack overflows, will probably not return: "([(((0 [[((0 1) 0)]]) [(0 0)]) 0)] [[(1 (1 0))]])" - struct term *term = - parse("([(((0 [[((0 1) 0)]]) [(0 0)]) 0)] [[(1 (1 0))]])"); + if (argc < 2) { + fprintf(stderr, "Invalid arguments\n"); + return 1; + } + + char *input = read_file(argv[1]); + if (!input) + return 1; - printf("\nReduced:\n"); - struct term *reduced = reduce(term, callback, 0); + struct term *parsed = parse_blc(input); + struct term *reduced = reduce(parsed, callback, 0); to_bruijn(reduced); - print_term(reduced); - printf("\n"); - free_term(term); + print_blc(reduced); free_term(reduced); + free_term(parsed); + free(input); return 0; } #else -static int testing; +__attribute__((unused)) static int testing; #endif -- cgit v1.2.3