-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.cpp
More file actions
290 lines (263 loc) · 7.27 KB
/
expression.cpp
File metadata and controls
290 lines (263 loc) · 7.27 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <sstream>
#include <cctype>
#include "expression.hpp"
#include "tokenize.hpp"
bool match_open(token::Token token) {
bool correct_type = token.getType() == token::OPEN_PAREN;
bool correct_string = token.getText() == "(";
if (correct_type && !correct_string) {
throw InvalidTokenException(token);
}
if (!correct_type && correct_string) {
throw InvalidTokenException(token);
}
return correct_type && correct_string;
}
bool match_close(token::Token token) {
bool correct_type = token.getType() == token::CLOSE_PAREN;
bool correct_string = token.getText() == ")";
if (correct_type && !correct_string) {
throw InvalidTokenException(token);
}
if (!correct_type && correct_string) {
throw InvalidTokenException(token);
}
return correct_type && correct_string;
}
bool match_bool(token::Token token) {
bool correct_type = token.getType() == token::ATOM;
bool true_string = token.getText() == "True";
bool false_string = token.getText() == "False";
if (!correct_type && (true_string || false_string)) {
throw InvalidTokenException(token);
}
return correct_type && (true_string || false_string);
}
bool match_none(token::Token token) {
bool correct_type = token.getType() == token::ATOM;
bool none_string = token.getText() == "None";
if (!correct_type && none_string) {
throw InvalidTokenException(token);
}
return correct_type && none_string;
}
bool match_number(token::Token token) {
// TODO! Make this less of a hack.
if (token.getText() == "+" or token.getText() == "-") {
return false;
}
bool first_char = true;
bool hit_dot = false;
bool hit_e = false;
bool number_after_e = false;
for (auto character : token.getText()) {
switch (character) {
case '+':
case '-':
if (!first_char) {
return false;
}
break;
case '.':
if (first_char || hit_dot || hit_e) {
return false;
}
break;
case 'e':
if (first_char || hit_e) {
return false;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (hit_e) {
number_after_e = true;
}
break;
default:
return false;
}
first_char = false;
}
if (hit_e) {
return number_after_e;
} else {
return true;
}
}
bool match_symbol(token::Token token) {
bool not_number = !match_number(token);
bool not_none = !match_none(token);
bool not_bool = !match_bool(token);
bool first_alpha = !isdigit(token.getText().at(0));
return not_number && not_none && not_bool && first_alpha;
}
Expression::Expression() {
this->type = NONE;
}
bool Expression::operator==(const Expression & other) const noexcept {
if (type != other.type) {
return false;
}
if (type == NUMBER) {
return number_value == other.number_value;
}
if (type == BOOL) {
return bool_value == other.bool_value;
}
if (type == SYMBOL) {
return symbol_value == other.symbol_value;
}
if (type == NONE) {
return true; // There's only one NONE.
}
if (type == LIST) {
return children == other.children;
}
return false;
}
Expression::Expression(double value) {
this->type = NUMBER;
this->number_value = value;
}
Expression::Expression(bool value) {
this->type = BOOL;
this->bool_value = value;
}
Expression::Expression(const std::string value) {
this->type = SYMBOL;
this->symbol_value = value;
}
Expression::Expression(const std::vector<Expression> children) {
this->type = LIST;
this->children = children;
}
const char * InvalidTokenException::what () const noexcept {
std::stringstream stream;
stream << token;
std::string output = stream.str();
return output.c_str();
}
AtomType Expression::getType() const {
return type;
}
Expression parse_tokens_iter(std::list<token::Token> & tokens) {
std::vector<Expression> top;
while (!tokens.empty()) {
if (match_open(tokens.front())) {
tokens.pop_front();
top.push_back(parse_tokens_iter(tokens));
} else if (match_close(tokens.front())) {
tokens.pop_front();
if (top.size() == 0) {
// TODO! This should return an error if the vector is empty.
throw InvalidTokenException(token::Token(token::ATOM, "TODO", 0));
}
return Expression(top);
} else {
top.push_back(parse_atom(tokens.front()));
tokens.pop_front();
}
}
// TODO! This should return an error if the vector is empty.
throw InvalidTokenException(token::Token(token::ATOM, "TODO", 0));
}
Expression parse_atom(token::Token token) {
if (match_bool(token)) {
if (token.getText() == "True") {
return Expression(true);
} else if (token.getText() == "False") {
return Expression(false);
} else {
throw InvalidTokenException(token);
}
} else if (match_none(token)) {
return Expression();
} else if (match_number(token)) {
std::stringstream stream(token.getText());
double value;
stream >> value;
// TODO! This should check if we were actually given a
// double, since match_number might not be valid.
return Expression(value);
} else if (match_symbol(token)) {
return Expression(std::string(token.getText()));
}
throw InvalidTokenException(token);
}
Expression parse_tokens(std::list<token::Token> tokens) {
if (tokens.empty()) {
//TODO Make betterr eror message
throw InvalidTokenException(token::Token(token::ATOM, "Empty tokens.", 1));
}
if ((tokens.size() == 1) && (match_symbol(tokens.front()))) {
//TODO Make betterr eror message
throw InvalidTokenException(token::Token(token::ATOM, "Bare word.", 1));
}
std::vector<Expression> top;
top.push_back(Expression(std::string("begin")));
Expression parse_tree;
if (match_open(tokens.front())) {
tokens.pop_front();
parse_tree = parse_tokens_iter(tokens);
} else if (match_close(tokens.front())) {
//TODO Make betterr eror message
throw InvalidTokenException(token::Token(token::ATOM, "too many tokens", 1));
} else {
parse_tree = parse_atom(tokens.front());
}
if (!tokens.empty()) {
throw InvalidTokenException(token::Token(token::ATOM, "too many tokens", 1));
}
return parse_tree;
}
std::ostream & operator << (std::ostream & stream, const Expression & expr) {
if (expr.type == NONE) {
stream << "(None|None)";
} else if (expr.type == BOOL) {
stream << "(Bool|";
if (expr.bool_value) {
stream << "True";
} else {
stream << "False";
}
stream << ")";
} else if (expr.type == SYMBOL) {
stream << "(Symbol|" << expr.symbol_value << ")";
} else if (expr.type == NUMBER) {
stream << "(Number|" << expr.number_value << ")";
} else if (expr.type == LIST) {
stream << "(Parent|{";
for (auto const & child : expr.children) {
stream << child << "|";
}
stream << "}";
}
return stream;
}
std::vector<Expression> Expression::getChildren() const {
return this->children;
}
bool Expression::getBool() const {
return bool_value;
}
double Expression::getNumber() const {
return number_value;
}
std::string Expression::getSymbol() const {
return symbol_value;
}
Expression::Expression(const Expression & other) {
this->type = other.getType();
this->bool_value = other.getBool();
this->number_value = other.getNumber();
this->symbol_value = other.getSymbol();
this->children = other.getChildren();
}