This repository was archived by the owner on Jun 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (77 loc) · 2.7 KB
/
main.cpp
File metadata and controls
90 lines (77 loc) · 2.7 KB
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
#include"front/lexical.h"
#include"front/syntax.h"
#include"front/semantic.h"
#include"ir/ir.h"
#include"tools/ir_executor.h"
#include"backend/generator.h"
#include<string>
#include<vector>
#include<cassert>
#include<fstream>
#include<iostream>
using std::string;
using std::vector;
/**
* commad line:
* compiler <src_filename> -step -o <output_filename> [opt]
*
* step:
* -s0: output of scanner
* -s1: output of parser, should be a json file
* -s2: output IR
* -S: output rv assembly
* -e: get ir::Program and execute it, print the main return value to stdout
* -all[FIXME]
*
* opt:
* [FIXME]
*/
int main(int argc, char** argv) {
assert((argc == 5 || argc == 6) && "command line should be: compiler <src_filename> -step -o <output_filename> [opt]");
string src = argv[1];
string step = argv[2];
string des = argv[4];
std::ofstream output_file = std::ofstream(des);
assert(output_file.is_open() && "output file can not open");
frontend::Scanner scanner(src);
vector<frontend::Token> tk_stream = scanner.run();
// compiler <src_filename> -s0 -o <output_filename>
if(step == "-s0"){
for(const auto& tk: tk_stream){
output_file << frontend::toString(tk.type) << "\t" << tk.value << std::endl;
}
return 0;
}
frontend::Parser parser(tk_stream);
frontend::CompUnit* node = parser.get_abstract_syntax_tree();
// compiler <src_filename> -s1 -o <output_filename>
if(step == "-s1") {
Json::Value json_output;
Json::StyledWriter writer;
node->get_json_output(json_output);
output_file << writer.write(json_output);
return 0;
}
frontend::Analyzer analyzer;
auto program = analyzer.get_ir_program(node);
// compiler <src_filename> -s2 -o <output_filename>
if(step == "-s2") {
output_file << program.draw();
}
// compiler <src_filename> -e -o <output_filename>
if(step == "-e") {
auto output_file_name = des;
auto input_file_name = src.substr(0,src.size()-2) + "in";
ir::reopen_output_file = fopen(output_file_name.c_str(), "w");
ir::reopen_input_file = fopen(input_file_name.c_str(), "r");
auto executor = ir::Executor(&program);
std::cout << program.draw() << "--------------------------- Executor::run() ---------------------------" << std::endl;
fprintf(ir::reopen_output_file, "\n%d", (uint8_t)executor.run());
}
// compiler <src_filename> -e -o <output_filename>
if(step == "-S") {
backend::Generator generator(program, output_file);
generator.gen();
}
return 0;
}