-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
79 lines (64 loc) · 1.91 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
#include <iostream>
#include <fstream>
#include <sstream>
#include "Exceptions/DotException.h"
#include "Lexer/Lexer.h"
#include "CliParser/CliParser.h"
#include "CliParser/CliStaticCommands.h"
using namespace std;
int main(int argc, char* argv[]) {
CliParser parser(argc, argv);
if(parser.hasError()) {
CliStaticCommands::help();
return 1;
}
if(
parser.hasFlag("--version") ||
parser.hasFlag("-v")
) {
CliStaticCommands::version();
return 0;
}
if(
parser.hasFlag("--help") ||
parser.hasFlag("-h")
) {
CliStaticCommands::help();
return 0;
}
if (!parser.hasFlag("-i")) {
cout << "No input file provided." << endl;
CliStaticCommands::help();
return 1;
}
ifstream inputFile(parser.getArgument("-i"));
if(!inputFile.is_open()) {
cout << "Failed opening input filestream \"" << parser.getArgument("-i") << "\"" << endl;
return 1;
}
stringstream buffer;
buffer << inputFile.rdbuf();
inputFile.close();
list<Token> tokenList;
try {
Lexer testLexer(buffer.str());
tokenList = testLexer.parse();
} catch (DotException& e) {
cout << "There was a problem lexing the source code: " << e.what() << endl;
return 1;
}
string outFilepath = parser.getArgument("-o").empty() ? "a.tokens" : parser.getArgument("-o");
ofstream outputFile(outFilepath);
if(!outputFile.is_open()) {
cout << "Failed opening output filestream \"" << outFilepath << "\"" << endl;
return 1;
}
for(const Token& token : tokenList) {
outputFile << endl;
outputFile << "--- Type " << token.type << " token ---" << endl;
outputFile << token.value << endl;
outputFile << "--- Type " << token.type << " token end ---" << endl;
}
outputFile.close();
return 0;
}