aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/src/main.c b/src/main.c
index 36ca46a..dc5f062 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
+#include <time.h>
#include <stdlib.h>
#include <reducer.h>
@@ -18,6 +19,34 @@ static void callback(int i, char ch, void *data)
/* printf("%d: %c\n", i, ch); */
}
+#define BUF_SIZE 1024
+static char *read_stdin(void)
+{
+ char buffer[BUF_SIZE];
+ size_t size = 1;
+ char *string = malloc(sizeof(char) * BUF_SIZE);
+ if (!string)
+ return 0;
+ string[0] = '\0';
+ while (fgets(buffer, BUF_SIZE, stdin)) {
+ char *old = string;
+ size += strlen(buffer);
+ string = realloc(string, size);
+ if (!string) {
+ free(old);
+ return 0;
+ }
+ strcat(string, buffer);
+ }
+
+ if (ferror(stdin)) {
+ free(string);
+ fprintf(stderr, "Couldn't read from stdin\n");
+ return 0;
+ }
+ return string;
+}
+
static char *read_file(const char *path)
{
FILE *f = fopen(path, "rb");
@@ -26,6 +55,7 @@ static char *read_file(const char *path)
strerror(errno));
return 0;
}
+
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
@@ -54,12 +84,24 @@ int main(int argc, char **argv)
return 1;
}
- char *input = read_file(argv[1]);
+ char *input;
+ if (argv[1][0] == '-') {
+ input = read_stdin();
+ } else {
+ input = read_file(argv[1]);
+ }
+
if (!input)
return 1;
struct term *parsed = parse_blc(input);
+
+ clock_t begin = clock();
struct term *reduced = reduce(parsed, callback, 0);
+ clock_t end = clock();
+ fprintf(stderr, "reduced in %.5fs\n",
+ (double)(end - begin) / CLOCKS_PER_SEC);
+
to_bruijn(reduced);
print_blc(reduced);
free_term(reduced);