-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (73 loc) · 1.98 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (73 loc) · 1.98 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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "interpreter.cpp"
using namespace std::string_literals;
using MiniLisp::Interpreter;
using MiniLisp::Type::Expression;
using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::ifstream;
using std::quoted;
using std::stringstream;
const string RED = "\033[1;31m";
const string BLUE = "\033[1;36m";
const string GREEN = "\033[1;32m";
const string NORM = "\033[0m";
const string HELP_INFOMATION = R"(use -i for command line interpreter
use -r for repl mode
or directly give a filename.
ex:
./run -i
./run -r
./run ./mini.lsp
./run -h
To get this infomation
)";
void eval(Interpreter &interpreter, istream &in) {
Expression result;
try {
result = interpreter.parse(in);
interpreter << ":=" << GREEN << result << NORM << endl;
} catch (const std::exception &exc) {
interpreter << RED << exc.what() << NORM << endl;
} catch (...) {
interpreter << RED << "unexpected unknown error" << NORM << endl;
}
}
int main(const int argc, const char **argv) {
Interpreter interpreter(cout);
if (argc != 2) {
interpreter << RED << HELP_INFOMATION << NORM;
return EXIT_FAILURE;
}
if (argv[1] == "-h"s) {
interpreter << RED << HELP_INFOMATION << NORM;
return EXIT_SUCCESS;
}
if (argv[1] == "-i"s) {
interpreter << BLUE << "<<<" << NORM << " " << flush;
eval(interpreter, cin);
return EXIT_SUCCESS;
}
if (argv[1] == "-r"s) {
string inputs;
while (interpreter << BLUE << "<<<" << NORM << " " << flush && getline(cin, inputs)) {
stringstream ss(inputs);
eval(interpreter, ss);
}
interpreter << GREEN << "BYE! BYE!" << NORM << flush;
return EXIT_SUCCESS;
}
ifstream file_in(argv[1]);
if (!file_in.good()) {
interpreter << RED << "Can't open file: " << NORM << quoted(argv[1]) << endl;
return EXIT_FAILURE;
}
interpreter << interpreter.parse(file_in) << endl;
return EXIT_SUCCESS;
}