forked from cparse/cparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shunting-yard.cpp
695 lines (600 loc) · 19.2 KB
/
shunting-yard.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#include "./shunting-yard.h"
#include "./shunting-yard-exceptions.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <exception>
#include <string>
#include <stack>
#include <utility> // For std::pair
#include <cstring> // For strchr()
/* * * * * Operation class: * * * * */
// Convert a type into an unique mask for bit wise operations:
uint32_t Operation::mask(tokType_t type) {
if (type == ANY_TYPE) {
return 0xFFFF;
} else {
return ((type & 0xE0) << 24) | (1 << (type & 0x1F));
}
}
// Build a mask for each pair of operands
opID_t Operation::build_mask(tokType_t left, tokType_t right) {
opID_t result = mask(left);
return (result << 32) | mask(right);
}
/* * * * * Utility functions: * * * * */
bool match_op_id(opID_t id, opID_t mask) {
uint64_t result = id & mask;
uint32_t* val = reinterpret_cast<uint32_t*>(&result);
if (val[0] && val[1]) return true;
return false;
}
TokenBase* exec_operation(const packToken& left, const packToken& right,
evaluationData* data, const std::string& OP_MASK) {
auto it = data->opMap.find(OP_MASK);
if (it == data->opMap.end()) return 0;
for (const Operation& operation : it->second) {
if (match_op_id(data->opID, operation.getMask())) {
try {
return operation.exec(left, right, data).release();
} catch (const Operation::Reject& e) {
continue;
}
}
}
return 0;
}
inline std::string normalize_op(std::string op) {
if (op[0] == 'L' || op[0] == 'R') {
op.erase(0, 1);
return op;
} else {
return op;
}
}
// Use this function to discard a reference to an object
// And obtain the original TokenBase*.
// Please note that it only deletes memory if the token
// is of type REF.
TokenBase* resolve_reference(TokenBase* b, TokenMap* scope = 0) {
if (b->type & REF) {
// Resolve the reference:
RefToken* ref = static_cast<RefToken*>(b);
TokenBase* value = ref->resolve(scope);
delete ref;
return value;
}
return b;
}
/* * * * * Static containers: * * * * */
// Build configurations once only:
Config_t& calculator::Default() {
static Config_t conf;
return conf;
}
typeMap_t& calculator::type_attribute_map() {
static typeMap_t type_map;
return type_map;
}
/* * * * * rpnBuilder Class: * * * * */
void rpnBuilder::cleanRPN(TokenQueue_t* rpn) {
while (rpn->size()) {
delete resolve_reference(rpn->front());
rpn->pop();
}
}
/**
* Consume operators with precedence >= than op
* and add them to the RPN
*
* The algorithm works as follows:
*
* Let p(o) denote the precedence of an operator o.
*
* If the token is an operator, o1, then
* While there is an operator token, o2, at the top
* and p(o1) >= p(o2) (`>` for Right to Left associativity)
* then:
*
* pop o2 off the stack onto the output queue.
* Push o1 on the stack.
*/
void rpnBuilder::handle_opStack(const std::string& op) {
std::string cur_op;
// If it associates from left to right:
if (opp.assoc(op) == 0) {
while (!opStack.empty() &&
opp.prec(op) >= opp.prec(opStack.top())) {
cur_op = normalize_op(opStack.top());
rpn.push(new Token<std::string>(cur_op, OP));
opStack.pop();
}
} else {
while (!opStack.empty() &&
opp.prec(op) > opp.prec(opStack.top())) {
cur_op = normalize_op(opStack.top());
rpn.push(new Token<std::string>(cur_op, OP));
opStack.pop();
}
}
}
void rpnBuilder::handle_binary(const std::string& op) {
// Handle OP precedence
handle_opStack(op);
// Then push the current op into the stack:
opStack.push(op);
}
// Convert left unary operators to binary and handle them:
void rpnBuilder::handle_left_unary(const std::string& unary_op) {
this->rpn.push(new TokenUnary());
// Only put it on the stack and wait to check op precedence:
opStack.push(unary_op);
}
// Convert right unary operators to binary and handle them:
void rpnBuilder::handle_right_unary(const std::string& unary_op) {
// Handle OP precedence:
handle_opStack(unary_op);
// Add the unary token:
this->rpn.push(new TokenUnary());
// Then add the current op directly into the rpn:
rpn.push(new Token<std::string>(normalize_op(unary_op), OP));
}
// Find out if op is a binary or unary operator and handle it:
void rpnBuilder::handle_op(const std::string& op) {
// If its a left unary operator:
if (this->lastTokenWasOp) {
if (opp.exists("L"+op)) {
handle_left_unary("L"+op);
this->lastTokenWasUnary = true;
this->lastTokenWasOp = op[0];
} else {
cleanRPN(&(this->rpn));
throw std::domain_error(
"Unrecognized unary operator: '" + op + "'.");
}
// If its a right unary operator:
} else if (opp.exists("R"+op)) {
handle_right_unary("R"+op);
// Set it to false, since we have already added
// an unary token and operand to the stack:
this->lastTokenWasUnary = false;
this->lastTokenWasOp = false;
// If it is a binary operator:
} else {
if (opp.exists(op)) {
handle_binary(op);
} else {
cleanRPN(&(rpn));
throw std::domain_error(
"Undefined operator: `" + op + "`!");
}
this->lastTokenWasUnary = false;
this->lastTokenWasOp = op[0];
}
}
void rpnBuilder::handle_token(TokenBase* token) {
rpn.push(token);
lastTokenWasOp = false;
lastTokenWasUnary = false;
}
void rpnBuilder::open_bracket(const std::string& bracket) {
opStack.push(bracket);
lastTokenWasOp = bracket[0];
lastTokenWasUnary = false;
++bracketLevel;
}
void rpnBuilder::close_bracket(const std::string& bracket) {
if (lastTokenWasOp == bracket[0]) {
rpn.push(new Tuple());
}
std::string cur_op;
while (opStack.size() && opStack.top() != bracket) {
cur_op = normalize_op(opStack.top());
rpn.push(new Token<std::string>(cur_op, OP));
opStack.pop();
}
if (opStack.size() == 0) {
rpnBuilder::cleanRPN(&rpn);
throw syntax_error("Extra '" + bracket + "' on the expression!");
}
opStack.pop();
lastTokenWasOp = false;
lastTokenWasUnary = false;
--bracketLevel;
}
/* * * * * RAII_TokenQueue_t struct * * * * */
// Used to make sure an rpn is dealloc'd correctly
// even when an exception is thrown.
//
// Note: This is needed because C++ does not
// allow a try-finally block.
struct calculator::RAII_TokenQueue_t : TokenQueue_t {
RAII_TokenQueue_t() {}
RAII_TokenQueue_t(const TokenQueue_t& rpn) : TokenQueue_t(rpn) {}
~RAII_TokenQueue_t() { rpnBuilder::cleanRPN(this); }
RAII_TokenQueue_t(const RAII_TokenQueue_t& rpn) {
throw std::runtime_error("You should not copy this class!");
}
RAII_TokenQueue_t& operator=(const RAII_TokenQueue_t& rpn) {
throw std::runtime_error("You should not copy this class!");
}
};
/* * * * * calculator class * * * * */
TokenQueue_t calculator::toRPN(const char* expr,
TokenMap vars, const char* delim,
const char** rest, Config_t config) {
rpnBuilder data(vars, config.opPrecedence);
char* nextChar;
static char c = '\0';
if (!delim) delim = &c;
while (*expr && isspace(*expr) && !strchr(delim, *expr)) ++expr;
if (*expr == '\0' || strchr(delim, *expr)) {
throw std::invalid_argument("Cannot build a calculator from an empty expression!");
}
// In one pass, ignore whitespace and parse the expression into RPN
// using Dijkstra's Shunting-yard algorithm.
while (*expr && (data.bracketLevel || !strchr(delim, *expr))) {
if (isdigit(*expr)) {
// If the token is a number, add it to the output queue.
int64_t _int = strtoll(expr, &nextChar, 10);
// If the number was not a float:
if (!strchr(".eE", *nextChar)) {
data.handle_token(new Token<int64_t>(_int, INT));
} else {
double digit = strtod(expr, &nextChar);
data.handle_token(new Token<double>(digit, REAL));
}
expr = nextChar;
} else if (rpnBuilder::isvarchar(*expr)) {
rWordParser_t* parser;
// If the token is a variable, resolve it and
// add the parsed number to the output queue.
std::string key = rpnBuilder::parseVar(expr, &expr);
if ((parser=config.parserMap.find(key))) {
// Parse reserved words:
try {
parser(expr, &expr, &data);
} catch (...) {
rpnBuilder::cleanRPN(&data.rpn);
throw;
}
} else {
packToken* value = vars.find(key);
if (value) {
// Save a reference token:
TokenBase* copy = (*value)->clone();
data.handle_token(new RefToken(key, copy));
} else {
// Save the variable name:
data.handle_token(new Token<std::string>(key, VAR));
}
}
} else if (*expr == '\'' || *expr == '"') {
// If it is a string literal, parse it and
// add to the output queue.
char quote = *expr;
++expr;
std::stringstream ss;
while (*expr && *expr != quote && *expr != '\n') {
if (*expr == '\\') {
switch (expr[1]) {
case 'n':
expr+=2;
ss << '\n';
break;
case 't':
expr+=2;
ss << '\t';
break;
default:
if (strchr("\"'\n", expr[1])) ++expr;
ss << *expr;
++expr;
}
} else {
ss << *expr;
++expr;
}
}
if (*expr != quote) {
std::string squote = (quote == '"' ? "\"": "'");
rpnBuilder::cleanRPN(&data.rpn);
throw syntax_error("Expected quote (" + squote +
") at end of string declaration: " + squote + ss.str() + ".");
}
++expr;
data.handle_token(new Token<std::string>(ss.str(), STR));
} else {
// Otherwise, the variable is an operator or paranthesis.
switch (*expr) {
case '(':
// If it is a function call:
if (data.lastTokenWasOp == false) {
// This counts as a bracket and as an operator:
data.handle_op("()");
// Add it as a bracket to the op stack:
}
data.open_bracket("(");
++expr;
break;
case '[':
if (data.lastTokenWasOp == false) {
// If it is an operator:
data.handle_op("[]");
} else {
// If it is the list constructor:
// Add the list constructor to the rpn:
data.handle_token(new CppFunction(&TokenList::default_constructor, "list"));
// We make the program see it as a normal function call:
data.handle_op("()");
}
// Add it as a bracket to the op stack:
data.open_bracket("[");
++expr;
break;
case '{':
// Add a map constructor call to the rpn:
data.handle_token(new CppFunction(&TokenMap::default_constructor, "map"));
// We make the program see it as a normal function call:
data.handle_op("()");
data.open_bracket("{");
++expr;
break;
case ')':
data.close_bracket("(");
++expr;
break;
case ']':
data.close_bracket("[");
++expr;
break;
case '}':
data.close_bracket("{");
++expr;
break;
default:
{
// Then the token is an operator
const char* start = expr;
std::stringstream ss;
ss << *expr;
++expr;
while (*expr && ispunct(*expr) && !strchr("+-'\"()[]{}_", *expr)) {
ss << *expr;
++expr;
}
std::string op = ss.str();
// Check if the word parser applies:
rWordParser_t* parser = config.parserMap.find(op);
// Evaluate the meaning of this operator in the following order:
// 1. Is there a word parser for it?
// 2. Is it a valid operator?
// 3. Is there a character parser for its first character?
if (parser) {
// Parse reserved operators:
try {
parser(expr, &expr, &data);
} catch (...) {
rpnBuilder::cleanRPN(&data.rpn);
throw;
}
} else if (data.opp.exists(op)) {
data.handle_op(op);
} else if ((parser=config.parserMap.find(op[0]))) {
expr = start+1;
try {
parser(expr, &expr, &data);
} catch (...) {
rpnBuilder::cleanRPN(&data.rpn);
throw;
}
} else {
rpnBuilder::cleanRPN(&data.rpn);
throw syntax_error("Invalid operator: " + op);
}
}
}
}
// Ignore spaces but stop on delimiter if not inside brackets.
while (*expr && isspace(*expr)
&& (data.bracketLevel || !strchr(delim, *expr))) ++expr;
}
// Check for syntax errors (excess of operators i.e. 10 + + -1):
if (data.lastTokenWasUnary) {
rpnBuilder::cleanRPN(&data.rpn);
throw syntax_error("Expected operand after unary operator `" + data.opStack.top() + "`");
}
std::string cur_op;
while (!data.opStack.empty()) {
cur_op = normalize_op(data.opStack.top());
data.rpn.push(new Token<std::string>(cur_op, OP));
data.opStack.pop();
}
// In case one of the custom parsers left an empty expression:
if (data.rpn.size() == 0) data.rpn.push(new TokenNone());
if (rest) *rest = expr;
return data.rpn;
}
packToken calculator::calculate(const char* expr, TokenMap vars,
const char* delim, const char** rest) {
// Convert to RPN with Dijkstra's Shunting-yard algorithm.
RAII_TokenQueue_t rpn = calculator::toRPN(expr, vars, delim, rest);
TokenBase* ret = calculator::calculate(rpn, vars);
return packToken(resolve_reference(ret));
}
void cleanStack(std::stack<TokenBase*> st) {
while (st.size() > 0) {
delete resolve_reference(st.top());
st.pop();
}
}
TokenBase* calculator::calculate(const TokenQueue_t& rpn, TokenMap scope,
const Config_t& config) {
evaluationData data(rpn, scope, config.opMap);
// Evaluate the expression in RPN form.
std::stack<TokenBase*> evaluation;
while (!data.rpn.empty()) {
TokenBase* base = data.rpn.front()->clone();
data.rpn.pop();
// Operator:
if (base->type == OP) {
data.op = static_cast<Token<std::string>*>(base)->val;
delete base;
/* * * * * Resolve operands Values and References: * * * * */
if (evaluation.size() < 2) {
cleanStack(evaluation);
throw std::domain_error("Invalid equation.");
}
TokenBase* r_token = evaluation.top(); evaluation.pop();
TokenBase* l_token = evaluation.top(); evaluation.pop();
if (r_token->type & REF) {
data.right.reset(static_cast<RefToken*>(r_token));
r_token = data.right->resolve(&data.scope);
} else if (r_token->type == VAR) {
packToken key = static_cast<Token<std::string>*>(r_token)->val;
data.right.reset(new RefToken(key));
} else {
data.right.reset(new RefToken());
}
if (l_token->type & REF) {
data.left.reset(static_cast<RefToken*>(l_token));
l_token = data.left->resolve(&data.scope);
} else if (l_token->type == VAR) {
packToken key = static_cast<Token<std::string>*>(l_token)->val;
data.left.reset(new RefToken(key));
} else {
data.left.reset(new RefToken());
}
if (l_token->type == FUNC && data.op == "()") {
// * * * * * Resolve Function Calls: * * * * * //
Function* l_func = static_cast<Function*>(l_token);
// Collect the parameter tuple:
Tuple right;
if (r_token->type == TUPLE) {
right = *static_cast<Tuple*>(r_token);
} else {
right = Tuple(r_token);
}
delete r_token;
packToken _this;
if (data.left->origin->type != NONE) {
_this = data.left->origin;
} else {
_this = data.scope;
}
// Execute the function:
packToken ret;
try {
ret = Function::call(_this, l_func, &right, data.scope);
} catch (...) {
cleanStack(evaluation);
delete l_func;
throw;
}
delete l_func;
evaluation.push(ret->clone());
} else {
// * * * * * Resolve All Other Operations: * * * * * //
data.opID = Operation::build_mask(l_token->type, r_token->type);
packToken l_pack(l_token);
packToken r_pack(r_token);
TokenBase* result = 0;
try {
// Resolve the operation:
result = exec_operation(l_pack, r_pack, &data, data.op);
if (!result) {
result = exec_operation(l_pack, r_pack, &data, ANY_OP);
}
} catch (...) {
cleanStack(evaluation);
throw;
}
if (result) {
evaluation.push(result);
} else {
cleanStack(evaluation);
throw undefined_operation(data.op, l_pack, r_pack);
}
}
} else if (base->type == VAR) { // Variable
packToken* value = NULL;
std::string key = static_cast<Token<std::string>*>(base)->val;
value = data.scope.find(key);
if (value) {
TokenBase* copy = (*value)->clone();
evaluation.push(new RefToken(key, copy));
delete base;
} else {
evaluation.push(base);
}
} else {
evaluation.push(base);
}
}
return evaluation.top();
}
/* * * * * Non Static Functions * * * * */
calculator::~calculator() {
rpnBuilder::cleanRPN(&this->RPN);
}
calculator::calculator(const calculator& calc) {
TokenQueue_t _rpn = calc.RPN;
// Deep copy the token list, so everything can be
// safely deallocated:
while (!_rpn.empty()) {
TokenBase* base = _rpn.front();
_rpn.pop();
this->RPN.push(base->clone());
}
}
// Work as a sub-parser:
// - Stops at delim or '\0'
// - Returns the rest of the string as char* rest
calculator::calculator(const char* expr, TokenMap vars, const char* delim,
const char** rest, const Config_t& config) {
this->RPN = calculator::toRPN(expr, vars, delim, rest, config);
}
void calculator::compile(const char* expr, TokenMap vars, const char* delim,
const char** rest) {
// Make sure it is empty:
rpnBuilder::cleanRPN(&this->RPN);
this->RPN = calculator::toRPN(expr, vars, delim, rest, Config());
}
packToken calculator::eval(TokenMap vars, bool keep_refs) const {
TokenBase* value = calculate(this->RPN, vars, Config());
packToken p = packToken(value->clone());
if (keep_refs) {
return packToken(value);
} else {
return packToken(resolve_reference(value));
}
}
calculator& calculator::operator=(const calculator& calc) {
// Make sure the RPN is empty:
rpnBuilder::cleanRPN(&this->RPN);
// Deep copy the token list, so everything can be
// safely deallocated:
TokenQueue_t _rpn = calc.RPN;
while (!_rpn.empty()) {
TokenBase* base = _rpn.front();
_rpn.pop();
this->RPN.push(base->clone());
}
return *this;
}
/* * * * * For Debug Only * * * * */
std::string calculator::str() const {
return str(this->RPN);
}
std::string calculator::str(TokenQueue_t rpn) {
std::stringstream ss;
ss << "calculator { RPN: [ ";
while (rpn.size()) {
ss << packToken(resolve_reference(rpn.front()->clone())).str();
rpn.pop();
ss << (rpn.size() ? ", ":"");
}
ss << " ] }";
return ss.str();
}