-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
223 lines (217 loc) · 8.46 KB
/
Copy pathInterpreter.cpp
File metadata and controls
223 lines (217 loc) · 8.46 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
//
// Created by yuvalshechter
//
#include "ex1.h"
regex negNum("(\\-)(0|[1-9][0-9]*)(\\.[0-9]+)?");
regex equalOpr("=");
regex validNum("^(\\()(\\-|\\+)?(0|[1-9][0-9]*)(\\.[0-9]+)?(\\))");
regex colon(";");
regex newOpr("\\+|\\-|\\/|\\*|\\(|\\$|\\#|\\)");
regex varAndNum("[a-zA-Z][0-9]*");
regex numOrVar("^((0|[1-9][0-9]*)(\\.[0-9]+)?)|^[a-zA-Z][0-9]*");
regex oprNumVar ("(\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\))|([0-9]+[a-zA-Z]+)");
regex validOpr("(\\+|\\-|\\/|\\*)+(\\+|\\-|\\/|\\*)+");
stack<string>oprStack;
queue<string>numQueue;
string temp;
string tempVars;
smatch ma;
bool ok;
Expression* r;
Expression* l;
Expression *Interpreter::interpret(string infix) {
int counter = 0;
if(regex_search(infix, match, validOpr)){
throw (const char*)"illegal math expression";
}
for (int unsigned i= 0; i < infix.size(); i++) {
if (infix[i] == '-' && infix[i + 1] == '(') {
infix[i] = '$';
}
if (infix[i] == '+' && infix[i + 1] == '(') {
infix[i] = '#';
}
if(infix[i] == '('){
counter++;
}
if(infix[i] == ')'){
counter--;
}
}
if(counter != 0 ){
throw (const char*)"illegal math expression";
}
while (!infix.empty()) {
if (regex_search(infix, match, validNum)) {
numQueue.push(match.str());
} else {
if (regex_search(infix, match, numOrVar)) {
numQueue.push(match.str());
} else {
if (regex_search(infix, match, newOpr)) {
if (oprStack.empty() || match.str() == "(" || oprStack.top() == "(") {
oprStack.push(match.str());
} else {
if ((oprStack.top() == "+" || oprStack.top() == "-" || oprStack.top() == "$"||oprStack.top() == "#")&& match.str()!= ")") {
oprStack.push(match.str());
} else {
if(match.str() == ")"){
while (temp != "(" && !oprStack.empty()) {
temp = oprStack.top();
if (temp != "(") {
oprStack.pop();
numQueue.push(temp);
} else {
oprStack.pop();
}
}
} else{
if (oprStack.top() == "*" || oprStack.top() == "/") {
if (match.str() == "+" || match.str() == "-" || match.str() == "$" || oprStack.top() == "#") {
temp = oprStack.top();
oprStack.pop();
oprStack.push(match.str());
numQueue.push(temp);
}
if (match.str() == "*" || match.str() == "/") {
temp = oprStack.top();
oprStack.pop();
oprStack.push(match.str());
numQueue.push(temp);
}
}
}
}
}
}
}
}
infix = match.suffix();
}
while (!oprStack.empty()) {
temp = oprStack.top();
oprStack.pop();
if (temp != "(") {
numQueue.push(temp);
}
}
stack<Expression *> expStack;
string qstr;
while (!numQueue.empty()) {
ok = false;
qstr = numQueue.front();
if (regex_search(qstr, match, numOrVar)) {
if (regex_search(qstr, ma, varAndNum)) {
int i = 0;
while (ma.str() != variables.at(i)->getName()) {
i++;
}
expStack.push(variables.at(i));
} else {
expStack.push(new Value(stod(match.str())));
}
ok = true;
}
if (regex_search(qstr, match, validNum)) {
string s = match.str();
regex_search(s, ma, negNum);
expStack.push(new Value(stod(ma.str())));
ok = true;
}
if (regex_search(qstr, match, newOpr) && ok == false) {
if (match.str() == "$" || match.str() == "#") {
r = expStack.top();
expStack.pop();
if (match.str() == "$")
expStack.push(new UMinus(r));
if (match.str() == "#"){
if(expStack.size()>1){
l=expStack.top();
expStack.pop();
expStack.push(new Value((new Plus(r, l))->calculate()));
}else{
expStack.push(new UPlus(r));
}
}
} else {
r = expStack.top();
expStack.pop();
l = expStack.top();
expStack.pop();
if (match.str() == "*") {
expStack.push(new Value((new Mul(r, l))->calculate()));
}
if (match.str() == "+") {
expStack.push(new Value((new Plus(r, l))->calculate()));
}
if (match.str() == "/") {
expStack.push(new Value((new Div(l, r))->calculate()));
}
if (match.str() == "-") {
expStack.push(new Value((new Minus(r, l))->calculate()));
}
}
}
numQueue.pop();
}
if(expStack.size()>1){
r=expStack.top();
expStack.pop();
l=expStack.top();
expStack.pop();
expStack.push(new Value((new Plus(r, l))->calculate()));
}
return expStack.top();
}
void Interpreter::setVariables(string variable) {
//if the expression is not number or operator or variable throw exception.
if(regex_search(variable,match,oprNumVar)){
throw(const char*)"illegal variable assignment";
}else {
//variable is a string that we get.
while(!variable.empty()){
//This function take only the first expression each time until the ; such as x=4.
if(regex_search(variable,match,colon)){
//tempVars is a string the contain the expression until the ;.
tempVars = match.prefix().str();
//update string variable to be the suffix string that is after the ;.
variable = match.suffix().str();
//we separate the expression such as x=4 to string variable-var and double number-val
regex_search(tempVars,ma,equalOpr);
//if the size of the variables vector is 0
if(variables.size()==0){
variables.push_back(new Variable(ma.prefix().str(),stod( ma.suffix())));
} else {
ok= false;
for (int unsigned i = 0; i < variables.size(); i++) {
if (variables.at(i)->getName() == ma.prefix().str()) {
variables[i]->setValue(stod(ma.suffix()));
ok=true;
}
}
if(ok==false){
variables.push_back(new Variable(ma.prefix().str(), stod(ma.suffix())));
}
variable = match.suffix();
}
} else{
regex_search(variable,match,equalOpr);
if(variables.size()==0){
variables.push_back(new Variable(match.prefix().str(),stod( match.suffix())));
} else{
ok= false;
for(int unsigned i=0 ;i<variables.size();i++){
if (variables.at(i)->getName() == match.prefix().str()) {
variables[i]->setValue(stod(match.suffix()));
ok=true;
}
}
if(ok==false){
variables.push_back(new Variable(match.prefix().str(),stod( match.suffix())));
}
}
variable.clear();
}
}
}
}