aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 36ca46a0732f26d47c6c128dd25eb8f7b6dfa700 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>

#ifndef TEST
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <reducer.h>
#include <gc.h>
#include <parse.h>

static void callback(int i, char ch, void *data)
{
	(void)i;
	(void)ch;
	(void)data;
	/* printf("%d: %c\n", i, ch); */
}

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();

	if (argc < 2) {
		fprintf(stderr, "Invalid arguments\n");
		return 1;
	}

	char *input = read_file(argv[1]);
	if (!input)
		return 1;

	struct term *parsed = parse_blc(input);
	struct term *reduced = reduce(parsed, callback, 0);
	to_bruijn(reduced);
	print_blc(reduced);
	free_term(reduced);
	free_term(parsed);
	free(input);
	return 0;
}
#else
__attribute__((unused)) static int testing;
#endif