forked from P4-ACMMMRW/dpli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
118 lines (96 loc) · 3.55 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
#include "Main.hpp"
using namespace antlr4;
using namespace dplgrammar;
using namespace dplsrc;
int main(int argc, char **argv) {
std::string description = "DPL Interpreter";
std::string version = "0.0.1";
std::string author = "P4-ACMMMRW";
std::string usage = std::string(argv[0]) + " [options] <input-file> [options]";
argz::about about{description, version, author, usage};
about.print_help_when_no_options = false;
bool debug = false;
bool interpretFromCli = false;
argz::options options{{{"debug", 'd'}, debug, "enable debug output"},
{{"cli", 'c'}, interpretFromCli, "interpret from command line"}};
if (argc < 2) {
argz::help(about, options);
return EXIT_SUCCESS;
}
int fileArgIndex = 0;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
fileArgIndex = i;
break;
}
}
// Parse all arguments except fileArgIndex and argv[0]
try {
argz::parse(about, options, argc, argv, fileArgIndex);
} catch (const DplException &e) {
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
ANTLRInputStream input;
if (!interpretFromCli) {
// No source code provided, and options have already been evaluated so exit
// program.
if (fileArgIndex == 0) {
return EXIT_SUCCESS;
}
// Check if input file exists
if (!std::filesystem::exists(argv[fileArgIndex])) {
std::cerr << "Error: file \"" << argv[fileArgIndex] << "\" not found\n";
return EXIT_FAILURE;
}
std::ifstream file{std::filesystem::path(argv[fileArgIndex])};
if (!file.is_open()) {
std::cerr << "Error: file \"" << argv[fileArgIndex] << "\" could not be opened\n";
return EXIT_FAILURE;
}
input = ANTLRInputStream(file);
} else {
std::cout << "> ";
std::string source;
std::getline(std::cin, source);
input = ANTLRInputStream(source);
}
DplLexer lexer(&input);
lexer.removeErrorListeners();
std::string filename = std::string(argv[fileArgIndex])
.substr(std::string(argv[fileArgIndex]).find_last_of('/') + 1);
std::shared_ptr<ANTLRErrorListener> lexerErrorListener =
std::make_shared<LexerErrorListener>(filename);
lexer.addErrorListener(lexerErrorListener.get());
CommonTokenStream tokens(&lexer);
try {
tokens.fill();
DplParser parser(&tokens);
parser.removeErrorListeners();
std::shared_ptr<ANTLRErrorListener> parserErrorListener =
std::make_shared<ParserErrorListener>(filename);
parser.addErrorListener(parserErrorListener.get());
std::shared_ptr<ANTLRErrorStrategy> strategy = std::make_shared<DplErrorStrategy>();
parser.setErrorHandler(strategy);
tree::ParseTree *tree = nullptr;
tree = parser.prog();
AstBuilder builder{&parser, &lexer};
builder.visit(tree);
std::shared_ptr<AstNode> root;
root = builder.getRoot();
std::shared_ptr<Evaluator> evaluator;
evaluator = std::make_shared<Evaluator>();
root->accept(evaluator);
if (debug) {
// Ast print
builder.getRoot()->print();
// print eval
evaluator->getVtable().print();
evaluator->getPtable().print();
}
} catch (const DplException &e) {
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}