about summary refs log tree commit diff
path: root/src/bootstrap/main.pry
blob: 84824fd6cada4ccbf04974e938c7866ef0eb64f5 (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
import "!stdlib.pry";
import "!mem.pry";

let slice = struct {
	data: *void,
	data_len: i64,
};

import "tokenizer.pry";
import "parser.pry";
import "codegen.pry";

let read_file = (filename: *i8, alloc: *arena) => slice {
	let file = fopen(filename, "r");

	fseek(file, 0, 2);
	let file_size = ftell(file);
	fseek(file, 0, 0);

	let buf = cast(*i8, arena_alloc(alloc, file_size + 1));

	let bytes_read = fread(buf, 1, file_size, file);
	(*(buf + cast(*i8, bytes_read))) = '\0';

	fclose(file);

	let sl = slice{};
	sl.data = cast(*void, buf);
	sl.data_len = file_size;
	return sl;
};

let main = (argc: i64, argv: **i8) => i64 {
	if argc < 2 {
		printf("Need filename!\n");
		return 1;
	};

	let filename = *(argv + cast(**i8, 1));

	printf("%s\n", filename);

	let alloc = arena_init(999999999);

	let file = read_file(filename, alloc);

	let t = tokenizer_init(alloc, file);
	let ts = tokenizer_tokenize(t);

	let p = parser_init(cast(*token, ts.data), ts.data_len, alloc, filename);
	let ast = parse(p);

	let c = codegen_init(alloc);
	let res = codegen_generate(c, ast);
	let res = codegen_compile(c);
	codegen_deinit(c);

	arena_free(alloc);

	return 0;
};