This repository was archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.cpp
More file actions
159 lines (153 loc) · 3.85 KB
/
Copy pathexpression.cpp
File metadata and controls
159 lines (153 loc) · 3.85 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
// expression.cpp
// Copyright (c) 2018 Ni Kesu. All rights reserved.
#include "expression.h"
using std::vector;
using std::stack;
Value Expression::evaluate()
{
vector<Token> tokenStr = infixToPostfix(getTokenStream(str_));
stack<Value> s;
for (auto iter = tokenStr.cbegin(); iter < tokenStr.cend(); ++iter) {
if (isOperand(iter->type)) {
auto t = toValue(*iter);
s.push(t);
}
else if (isOperator(iter->type)) {
int n = arity(iter->type);
if (s.size() < n) {
std::cerr << "too few operands";
exit(-1);
}
if (n == 1) {
Value t = s.top();
s.pop();
switch (iter->type) {
case T_FACTORIAL:
s.push(factorial(t));
break;
default:
std::cerr << "undefined operator!";
exit(-1);
break;
}
}
else if (n == 2) {
Value r = s.top();
s.pop();
Value l = s.top();
s.pop();
switch (iter->type) {
case T_POWER:
s.push(power(l, r));
break;
case T_ASSIGN:
s.push(assign(l, r));
break;
case T_PLUS:
s.push(plus(l, r));
break;
case T_MINUS:
s.push(minus(l, r));
break;
case T_MULTIPLY:
s.push(multiply(l, r));
break;
case T_DIVIDE:
s.push(divide(l, r));
break;
case T_MODULO:
s.push(modulo(l, r));
break;
case T_GREATERTHAN:
s.push(greaterthan(l, r));
break;
case T_LESSTHAN:
s.push(lessthan(l, r));
break;
case T_GREATEROREQUAL:
s.push(greaterorequal(l, r));
break;
case T_LESSOREQUAL:
s.push(lessorequal(l, r));
break;
case T_EQUAL:
s.push(equal(l, r));
break;
case T_NOTEQUAL:
s.push(notequal(l, r));
break;
case T_COMMA:
s.push(comma(l, r));
break;
default:
std::cerr << "undefined operator!";
exit(-1);
break;
}
}
}
}
if (s.size() == 1) {
return s.top();
}
else {
std::cerr << "too many operands";
exit(-1);
}
}
// use shunting-yard algorithm to convert a token stream from infix notation to
// postfix notation
vector<Token> infixToPostfix(const vector<Token>& a)
{
vector<Token> r;
stack<Token> s;
for (auto iter = a.cbegin(); iter < a.cend(); ++iter) {
if (isOperand(iter->type)) {
if (iter->type == T_IDENTIFIER && isFunction(iter->str)) {
// TODO
}
r.push_back(*iter);
}
else if (iter->type == T_LEFTPAREN) {
s.push(*iter);
}
else if (iter->type == T_RIGHTPAREN) {
bool flag = false;
while (!s.empty()) {
if (s.top().type == T_LEFTPAREN) {
s.pop();
flag = true;
break;
}
else {
r.push_back(s.top());
s.pop();
}
}
if (!flag) {
std::cerr << "missing parentheses";
exit(-1);
}
}
else { // isOperator(iter->type)
while (!s.empty()
&& (precedence(s.top().type) > precedence(iter->type)
|| (precedence(s.top().type) == precedence(iter->type)
&& associative(s.top().type) == LEFT_ASSOCIATIVE)
|| isFunction(s.top().str))) {
r.push_back(s.top());
s.pop();
}
s.push(*iter);
}
}
while (!s.empty()) {
if (s.top().type == T_LEFTPAREN || s.top().type == T_RIGHTPAREN) {
std::cerr << "missing parentheses";
exit(-1);
}
r.push_back(s.top());
s.pop();
}
return r;
}