-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (73 loc) · 1.66 KB
/
main.cpp
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
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "basics.h"
#include "compile.h"
#include "parse.h"
int main(int argc, char **argv)
{
printf("This is the Janguage compiler.\n");
if (argc != 2)
{
fprintf(stderr, "Usage: jang <main source file>\n");
return 1;
}
auto path = argv[1];
printf("Compiling file: %s\n", path);
auto f = fopen(path, "r");
if (f == nullptr)
{
fprintf(stderr, "Could not open file %s", path);
return 1;
}
defer
{
fclose(f);
};
fseek(f, 0, SEEK_END);
auto file_size = ftell(f);
rewind(f);
auto source = static_cast<char *>(malloc(file_size + 1));
defer
{
free(source);
};
source[file_size] = 0;
size_t pos = 0;
size_t read = 0;
for (size_t read = 0; (read = fread(&source[pos], 1, file_size - pos, f)); pos += read)
{
}
Lexer lexer{source};
#if 0
printf("Dumping tokens\n");
Token token;
while (true)
{
token = next_token(&l);
printf("Line %d char %d: %s ", (int)token.line, (int)token.line_offset, to_string(token.type));
if (token.len > 0)
fwrite(l.start, 1, l.at - l.start, stdout);
printf("\n");
if (token.type == Tt::EOF)
{
break;
}
}
#else
AstProgram program{};
if (parse_program(source, program) == false)
{
return 1;
}
/* printf("Program:\n%s\n", dump_node(0, &program).data()); */
BytecodeWriter w{};
if (generate_code(&program.block, &w) == false)
{
return 1;
}
#endif
return 0;
}