summaryrefslogtreecommitdiff
path: root/main.c
blob: 470f5c2c13998e1584ecb0db5d833a03b2fb7281 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* 
TODO:
fix VBS with structs
test ArrInt @= Arr(int);

there are probably places where we enter a function and never exit (in typing?) if there's an error
packages
X @= newtype(int); or something
don't allow while {3; 5} (once break is added)
any odd number of "s for a string
make sure futurely/currently-declared types are only used by pointer/slice
allow omission of trailing ; in foo @= fn() {}?
 */

#ifdef __cplusplus
#define new new_
#define this this_
#endif

#include "toc.c"

int main(int argc, char **argv) {
#ifdef TOC_DEBUG
	test_all();
#endif

	const char *in_filename;
	if (argc < 2) {
#ifdef TOC_DEBUG
		in_filename = "test.toc";
#else
		fprintf(stderr, "Please specify an input file.\n");
		return EXIT_FAILURE;
#endif
	} else {
		in_filename = argv[1];
	}
	const char *out_filename = "out.c";

	for (int i = 2; i < argc-1; i++) {
		if (strcmp(argv[i], "-o") == 0)
			out_filename = argv[i+1];
	}
	
	FILE *in = fopen(in_filename, "r");
	if (!in) {
		fprintf(stderr, "Could not open file: %s.\n", argv[1]);
		return EXIT_FAILURE;
	}
	
	char *contents = err_malloc(4096);
	long contents_cap = 4095;
	long contents_len = 0;
	while (fgets(contents + contents_len, (int)(contents_cap - contents_len), in)) {
		contents_len += (long)strlen(contents + contents_len);
		
		if (contents_len >= (long)contents_cap - 1024) {
			contents_cap *= 2;
			/* allocate +1 so that pointers don't overflow */
			contents = err_realloc(contents, (size_t)contents_cap + 1);
		}
	}
	if (ferror(in)) {
		fprintf(stderr, "Error reading input file: %s.\n", argv[1]);
		return EXIT_FAILURE;
	}
	fclose(in);
	Identifiers file_idents;
	idents_create(&file_idents);
	Tokenizer t;
	Allocator main_allocr;
	allocr_create(&main_allocr);
	ErrCtx err_ctx = {0};
	err_ctx.filename = in_filename;
	err_ctx.enabled = true;
	tokr_create(&t, &file_idents, &err_ctx, &main_allocr);
	if (!tokenize_string(&t, contents)) {
		
		err_fprint(TEXT_IMPORTANT("Errors occured while preprocessing.\n"));
		return EXIT_FAILURE;
	}
	
	/* arr_foreach(t.tokens, Token, token) { */
	/* 	if (token != t.tokens) */
	/* 		printf("    "); */
	/* 	fprint_token(stdout, token); */
	/* } */
	/* printf("\n"); */
	Parser p;
	parser_create(&p, &t, &main_allocr);
   	ParsedFile f;
	if (!parse_file(&p, &f)) {
		
		err_fprint(TEXT_IMPORTANT("Errors occured while parsing.\n"));
		return EXIT_FAILURE;
	}
	/* fprint_parsed_file(stdout, &f); */
    
	/* printf("\n\n-----\n\n"); */
	
	tokr_free(&t);
	
	Typer tr;
	Evaluator ev;
	evalr_create(&ev, &tr, &main_allocr);
	typer_create(&tr, &ev, &main_allocr);


	if (!block_enter(NULL, f.stmts, SCOPE_CHECK_REDECL)) /* enter global scope */
		return false;

	if (!types_file(&tr, &f)) {
	    err_fprint(TEXT_IMPORTANT("Errors occured while determining types.\n"));
		return EXIT_FAILURE;
	}
	parse_printing_after_types = true;
	fprint_parsed_file(stdout, &f);

	FILE *out = fopen(out_filename, "w");
	if (!out) {
		err_fprint(TEXT_IMPORTANT("Could not open output file (out.c).\n"));
		return EXIT_FAILURE;
	}
	CGenerator g;
	cgen_create(&g, out, &file_idents, &ev, &main_allocr);
	if (!cgen_file(&g, &f)) {
		fclose(out);
		err_fprint(TEXT_IMPORTANT("Errors occured while generating C code.\n"));
		return EXIT_FAILURE;
	}
	
	block_exit(NULL, f.stmts); /* exit global scope */
	
	free(contents);
	allocr_free_all(&main_allocr);
	evalr_free(&ev);
	
	fclose(out);
	/* fclose(h_out); */
	idents_free(&file_idents);
}