-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·148 lines (121 loc) · 3.29 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
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
143
144
145
146
147
148
#include "include/tree.hpp"
#include "include/parser.hpp"
#include "include/translator.hpp"
#include "include/compiler.hpp"
#include "include/dump_tree.hpp"
int FileSize(FILE *file)
{
assert(file != NULL);
int _file_size = -1;
fseek(file, 0, SEEK_END);
_file_size = ftell(file);
return _file_size;
}
int main(int argc, char **argv)
{
const char *input_file = nullptr;
const char *output_file = nullptr;
/* Compile with dumping program graph (necessary dot) */
bool isGraph = false;
/* Loading program from AST-file */
bool isLoad = false;
/* Compile with assembly */
bool isAsm = false;
if (argc == 1)
{
printf("No input file\n");
return 1;
}
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-graph"))
{
isGraph = true;
continue;
}
if (!strcmp(argv[i], "-load"))
{
isLoad = true;
continue;
}
if (!strcmp(argv[i], "-asm"))
{
isAsm = true;
continue;
}
if (!strcmp(argv[i], "-o"))
{
if (i + 1 == argc)
{
printf("No output file\n");
return 1;
}
output_file = argv[i + 1];
i++;
continue;
}
input_file = argv[i];
}
if (input_file == nullptr)
{
printf("No input file\n");
return 1;
}
FILE *input = fopen(input_file, "rb");
assert(input != nullptr);
int size = FileSize(input);
fseek(input, 0, SEEK_SET);
char * buffer = (char *)calloc(size + 1, sizeof(char));
fread(buffer, sizeof(char), size, input);
fclose(input);
if (isLoad)
{
Tree_reader reader = {};
reader.construct();
Tree tree = {};
tree = reader.get_tree(buffer);
reader.destruct();
if (isGraph)
tree.Dump("AST-graph.pdf");
if (output_file == nullptr)
output_file = "program.doors";
Code_generator generator = {};
generator.construct(output_file);
generator.generate(&tree);
generator.destruct();
tree.destruct();
free(buffer);
return 0;
}
Parser parser = {};
parser.construct(buffer);
Tree tree = {};
tree.construct();
tree.root = parser.get_FuncCompaund();
free(buffer);
if (tree.root == nullptr)
return 1;
parser.destruct();
if (isGraph)
{
tree.Dump("AST-graph.pdf");
Tree_dumper dumper = {};
dumper.construct(&tree, "AST.txt");
dumper.dump_tree();
dumper.destruct();
}
if (isAsm)
{
ProgramTranslator translator = {};
translator.construct("program.asm", &tree);
translator.translate_program();
translator.destruct();
}
ProgramCompiler compiler = {};
compiler.construct(&tree);
if (output_file == nullptr)
output_file = "program.out";
compiler.compile_program(output_file);
compiler.destruct();
tree.destruct();
}