-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (175 loc) · 7 KB
/
main.cpp
File metadata and controls
182 lines (175 loc) · 7 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "FwdTypes.hpp"
#include "lexer/Loxer.h"
#include "parser/Loxgram.h"
#include "visitor/Interpreter.hpp"
#include "visitor/Resolver.hpp"
#include "visitor/ASTPrinter.hpp"
#include "Environment.hpp"
#include <vector>
#include <functional>
#include <fstream>
#define VER_STR "1.02 (2024-Jan-06)"
#ifdef _MSC_VER
#define OPTION_SWITCH '/'
#define USE_STR "[/?|/h|/v] [/i [/a]|/b] [filename]"
#else
#define OPTION_SWITCH '-'
#define USE_STR "[-?|-h|-v] [-i [-a]|-b] [filename]"
#endif
enum class ReturnCode { Unset = -1, Success = 0, Informational, OptionsError, BadFile, ParseError, RuntimeError };
enum class RuntimeOptions { None = 0, ShowAST = 1, Batch = 2, Interactive = 4, JIT = 8 };
RuntimeOptions operator | (RuntimeOptions lhs, RuntimeOptions rhs) {
return static_cast<RuntimeOptions>(static_cast<std::underlying_type_t<RuntimeOptions>>(lhs)
| static_cast<std::underlying_type_t<RuntimeOptions>>(rhs));
}
RuntimeOptions operator & (RuntimeOptions lhs, RuntimeOptions rhs) {
return static_cast<RuntimeOptions>(static_cast<std::underlying_type_t<RuntimeOptions>>(lhs)
& static_cast<std::underlying_type_t<RuntimeOptions>>(rhs));
}
int main(const int argc, const char **argv) {
auto returnCode = ReturnCode::Unset;
std::ifstream input{};
RuntimeOptions opts = RuntimeOptions::None;
int arg = 0;
while (++arg < argc) {
if (argv[arg][0] == OPTION_SWITCH) {
switch(argv[arg][1]) {
case 'a':
opts = opts | RuntimeOptions::ShowAST;
break;
case 'b':
opts = opts | RuntimeOptions::Batch;
break;
case 'i':
opts = opts | RuntimeOptions::Interactive;
break;
case 'j':
opts = opts | RuntimeOptions::JIT;
break;
case 'h':
case '?':
std::cerr << "Usage: " << argv[0] << ' ' << USE_STR << '\n';
returnCode = ReturnCode::Informational;
break;
case 'v':
std::cerr << VER_STR << '\n';
returnCode = ReturnCode::Informational;
break;
default:
std::cerr << "Unrecognized option \'" << OPTION_SWITCH << argv[arg][1] << "\'\n";
returnCode = ReturnCode::OptionsError;
break;
}
}
else {
break;
}
}
if (returnCode != ReturnCode::Informational) {
if ((opts & (RuntimeOptions::Batch | RuntimeOptions::Interactive))
== (RuntimeOptions::Batch | RuntimeOptions::Interactive)) {
std::cerr << "Error: Only specify one of -b and -i\n";
returnCode = ReturnCode::OptionsError;
}
if (((opts & RuntimeOptions::ShowAST) != RuntimeOptions::None)
&& ((opts & RuntimeOptions::Interactive) == RuntimeOptions::None)) {
std::cerr << "Error: Use of -a needs use of -i\n";
returnCode = ReturnCode::OptionsError;
}
if ((opts & RuntimeOptions::JIT) != RuntimeOptions::None) {
std::cerr << "Error: JIT engine not yet implemented\n";
returnCode = ReturnCode::OptionsError;
}
}
if ((opts & (RuntimeOptions::Batch | RuntimeOptions::Interactive))
== RuntimeOptions::None) {
opts = opts | RuntimeOptions::Batch;
}
if (arg != argc) {
if ((opts & RuntimeOptions::Interactive) != RuntimeOptions::Interactive) {
if (arg == (argc - 1)) {
input.open(argv[arg]);
if (!input.is_open()) {
std::cerr << "Error: Could not open file \'" << argv[arg] << "\'\n";
returnCode = ReturnCode::BadFile;
}
}
else {
std::cerr << "Error: Only one input file can be specified\n";
returnCode = ReturnCode::OptionsError;
}
}
else {
std::cerr << "Error: Cannot specify input file with " << OPTION_SWITCH << "i\n";
returnCode = ReturnCode::OptionsError;
}
}
if (returnCode != ReturnCode::Unset) {
return static_cast<int>(returnCode);
}
auto environment = Environment::makeShared();
Interpreter interpreter{ environment };
Resolver resolver{ interpreter };
std::function<void(std::shared_ptr<Stmt>)> execute;
std::vector<std::shared_ptr<Stmt>> program;
if ((opts & RuntimeOptions::Batch) != RuntimeOptions::None) {
execute = [&](std::shared_ptr<Stmt> statement){
program.push_back(statement);
};
}
else {
execute = [&](std::shared_ptr<Stmt> statement){
statement->accept(resolver);
statement->accept(interpreter);
if ((std::dynamic_pointer_cast<StmtExpression>(statement)
|| std::dynamic_pointer_cast<StmtVariable>(statement))) {
if ((opts & RuntimeOptions::ShowAST) == RuntimeOptions::None) {
if (const Value& result = interpreter.lastValue();
static_cast<ValueType>(result.index()) != ValueType::Nil) {
std::cout << "=> " << toString(result) << '\n';
}
}
else {
std::shared_ptr<Expr> expression;
if (std::dynamic_pointer_cast<StmtExpression>(statement)) {
expression = std::dynamic_pointer_cast<StmtExpression>(statement)->get();
}
else {
expression = std::dynamic_pointer_cast<StmtVariable>(statement)->getInit();
}
std::cout << "=> " << toString(interpreter.lastValue())
<< ' ' << toString(expression->accept(ASTPrinter{})) << '\n';
}
}
};
}
Loxer scanner{ input.is_open() ? input : std::cin };
Loxgram parser{ scanner, &execute };
if ((opts & RuntimeOptions::Batch) == RuntimeOptions::None) {
while (returnCode != ReturnCode::Success) {
try {
returnCode = parser.parse() ? ReturnCode::ParseError : ReturnCode::Success;
}
catch (std::exception &e) {
std::cerr << e.what() << '\n';
returnCode = ReturnCode::RuntimeError;
}
}
}
else {
try {
returnCode = parser.parse() ? ReturnCode::ParseError : ReturnCode::Success;
if (returnCode != ReturnCode::ParseError) {
for (auto statement : program) {
statement->accept(resolver);
statement->accept(interpreter);
}
}
}
catch (std::exception &e) {
std::cerr << e.what() << '\n';
returnCode = ReturnCode::RuntimeError;
}
}
return static_cast<int>(returnCode);
}