-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshell.cpp
More file actions
executable file
·56 lines (48 loc) · 1.42 KB
/
shell.cpp
File metadata and controls
executable file
·56 lines (48 loc) · 1.42 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
#include "shell.hpp"
extern ast::node_ptr<ast::Program> program;
cplus::Shell shell;
namespace cplus {
Shell::Shell() : lexer(*this), parser(lexer, *this) {}
int Shell::parse_program() {
return parser.parse();
}
void Shell::readFrom(std::istream *is) {
lexer.switch_streams(is, nullptr);
}
void Shell::show_help() {
std::cout << "usage: cplus [options] infile\n";
std::cout << "\tinfile\t\t\tpath to the source code file (*.cp) to compile.\n\n";
std::cout << "options:\n";
std::cout << "\t-h, --help\t\tshow this help message and exit.\n";
std::cout << "\t-d, --debug\t\tshow debug messages.\n";
std::cout << "\t-o, --outfile outfile\texecutable file name.\n";
std::exit(1);
}
int Shell::parse_args(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
show_help();
}
else if (arg == "-d" || arg == "--debug") {
debug = true;
}
else if (arg == "-o" || arg == "--outfile") {
outfile = argv[++i];
continue;
}
else {
infile.open(arg);
if (!infile.good()) {
std::cout << "Error: no such file: " << arg << '\n';
return 1;
}
readFrom(&infile);
}
}
if (!infile.is_open()) {
show_help();
}
return 0;
}
} // namespace cplus