aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c58
1 files changed, 45 insertions, 13 deletions
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 <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
-#define GC_PRINT_STATS 1
-
-#include <parse.h>
#include <reducer.h>
#include <gc.h>
+#include <parse.h>
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