aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 4ec0e7530319124cfe17554c84ebf42a10deb37f (plain)
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
#include "err.h"
#include "opt.h"
#include "parse.h"
#include "analyse.h"
#include "gen.h"

static void print_tree(struct analyse_tree_s *t)
{
	unsigned i;
	if (t->is_terminal)
		printf("!");

	if (t->branch_count > 1)
		printf("(");

	for (i = 0; i < t->branch_count; i++) {
		printf("%c", t->branch_chars[i]);
		print_tree(t->branches[i]);
		if (t->branch_count > 1 && i < t->branch_count - 1)
			printf("|");
	}

	if (t->branch_count > 1)
		printf(")");
}

int main(int argc, char **argv)
{
	FILE *fo = stdout;
	FILE *fi = stdin;
	const char *finame = "stdin";
	const char *foname;
	struct parse_result_s pr;
	struct analyse_result_s ar;

	opt_parse(argc, argv);

	if (opt_infile_str() != NULL) {
		finame = opt_infile_str();
		fi = fopen(finame, "r");
		TRY(fi != NULL, "could not read file `%s`", finame);
	}

	if (opt_outfile_str() != NULL) {
		foname = opt_infile_str();
		fo = fopen(foname, "w");
		TRY(fo != NULL, "could not write to file `%s`", foname);
	}

	pr = parse(fi, finame);

	if (fi != stdin)
		fclose(fi);

	ar = analyse(pr);

	print_tree(&ar.deftype_tree);
	puts("");
	print_tree(&ar.var_tree);
	puts("");

	gen(fo, pr, ar);

	if (fo != stdout)
		fclose(fo);

	parse_result_wipe(&pr);
	analyse_result_wipe(&ar);

	return 0;
}