-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
155 lines (145 loc) · 3.9 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
149
150
151
152
153
154
155
#include "argparse.hpp"
#include "fmt/core.h"
#include "tokenizer/tokenizer.h"
#include "analyser/analyser.h"
#include "fmts.hpp"
#include <iostream>
#include <fstream>
std::vector<c0::Token> _tokenize(std::istream& input) {
c0::Tokenizer tkz(input);
auto p = tkz.AllTokens();
if (p.second.has_value()) {
fmt::print(stderr, "Tokenization error: {}\n", p.second.value());
exit(2);
}
return p.first;
}
void Tokenize(std::istream& input, std::ostream& output) {
auto v = _tokenize(input);
for (auto& it : v)
output << fmt::format("{}\n", it);
return;
}
void Analyse(std::istream& input, std::ostream& output){
auto tks = _tokenize(input);
c0::Analyser analyser(tks);
auto p = analyser.Analyse();
if (p.second.has_value()) {
fmt::print(stderr, "Syntactic analysis error: {}\n", p.second.value());
exit(2);
}
auto v = p.first;
output << fmt::format("{}", v);
return;
}
void Binary(std::istream& input, std::ostream& output) {
auto tks = _tokenize(input);
c0::Analyser analyser(tks);
auto p = analyser.Analyse();
if (p.second.has_value()) {
fmt::print(stderr, "Syntactic analysis error: {}\n", p.second.value());
exit(2);
}
auto v = p.first;
v.genbinary(output);
return;
}
int main(int argc, char** argv) {
argparse::ArgumentParser program("c0");
program.add_argument("input")
.help("speicify the file to be compiled.");
program.add_argument("-t")
.default_value(false)
.implicit_value(true)
.help("perform tokenization for the input file.");
program.add_argument("-s")
.default_value(false)
.implicit_value(true)
.help("output assembly code file.");
program.add_argument("-c")
.default_value(false)
.implicit_value(true)
.help("output binary file.");
program.add_argument("-o", "--output")
.required()
.default_value(std::string("out"))
.help("specify the output file.");
try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
fmt::print(stderr, "{}\n\n", err.what());
program.print_help();
exit(2);
}
auto input_file = program.get<std::string>("input");
auto output_file = program.get<std::string>("--output");
std::istream* input;
std::ostream* output;
std::ifstream inf;
std::ofstream outf;
inf.open(input_file, std::ios::in);
if (!inf) {
fmt::print(stderr, "Fail to open {} for reading.\n", input_file);
exit(2);
}
input = &inf;
/*if (input_file != "-") {
inf.open(input_file, std::ios::in);
if (!inf) {
fmt::print(stderr, "Fail to open {} for reading.\n", input_file);
exit(2);
}
input = &inf;
}
else
input = &std::cin;*/
/*if (output_file != "-") {
outf.open(output_file, std::ios::out | std::ios::trunc);
if (!outf) {
fmt::print(stderr, "Fail to open {} for writing.\n", output_file);
exit(2);
}
output = &outf;
}
else
output = &std::cout;*/
if (program["-t"] == true && program["-s"] == true
|| program["-s"] == true && program["-c"] == true
|| program["-t"] == true && program["-c"] == true) {
fmt::print(stderr, "You can only perform tokenization analysis or output assembly or output binary at one time.");
exit(2);
}
if (program["-t"] == true) {
outf.open(output_file, std::ios::out | std::ios::trunc);
if (!outf) {
fmt::print(stderr, "Fail to open {} for writing.\n", output_file);
exit(2);
}
output = &outf;
Tokenize(*input, *output);
}
else if (program["-s"] == true) {
outf.open(output_file, std::ios::out | std::ios::trunc);
if (!outf) {
fmt::print(stderr, "Fail to open {} for writing.\n", output_file);
exit(2);
}
output = &outf;
Analyse(*input, *output);
}
else if (program["-c"] == true) {
outf.open(output_file, std::ios::binary | std::ios::out | std::ios::trunc);
if (!outf) {
fmt::print(stderr, "Fail to open {} for writing.\n", output_file);
exit(2);
}
output = &outf;
Binary(*input, *output);
}
else {
fmt::print(stderr, "You must choose tokenization analysis or output assembly or output binary at one time.");
exit(2);
}
return 0;
}