-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
391 lines (349 loc) · 8.6 KB
/
Copy pathparser.cpp
File metadata and controls
391 lines (349 loc) · 8.6 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
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
#include "parser.h"
namespace parser{
template <typename T> string toString(T t) {
stringstream s;
s.setf(std::ios::fixed, std::ios::floatfield);
s << t;
return s.str();
}
template<> string toString(cx c) {
stringstream s;
s.setf(std::ios::fixed, std::ios::floatfield);
s << c.real() << "+" << c.imag() << "i";
return s.str();
}
string Tree::getFct() {
return m_fct;
}
string Tree::getVarFct(string var) {
return variables[var]->getFct();
}
string toStringRounded(cx c) {
stringstream s;
double real = c.real(), imag = c.imag();
if(real > 0)
real = (int)(c.real() * 10000 + .5) / 10000.0;
else
real = (int)(c.real() * 10000 - .5) / 10000.0;
if(imag > 0)
imag = (int)(c.imag() * 10000 + .5) / 10000.0;
else
imag = (int)(c.imag() * 10000 - .5) / 10000.0;
s << real << "+" << imag << "i";
return s.str();
}
//Converts a string WITHOUT OPERATORS into a complex number
//Must be of form __number__i or __number__
cx stringToCx(string s) {
static string permitted = ".i-";
int len = 0;
for(char c : s)
if((c > '9' || c < '0') && permitted.find(c) == -1)
throw std::invalid_argument("Not a number: " + s);
else len++;
if(s.find("i") == -1)
return cx(strtod(s.c_str(), NULL), 0.0);
else if(len > 1)
return cx(0.0, strtod(s.c_str(), NULL));
else if (s[0] == 'i' && len == 1)
return cx(0.0, 1.0);
}
cx add(cx a, cx b) {
return a + b;
}
cx sub(cx a, cx b) {
return a - b;
}
cx mul(cx a, cx b) {
return a * b;
}
cx div(cx a, cx b) {
return a / b;
}
cx pow(cx base, cx a) {
return std::pow(base, a);
}
cx log(cx base, cx a) {
if(base == PARSER_E || base ==0.0)
return std::log(a);
else return std::log(a) / std::log(base);
}
cx sin(cx a, cx b) {
return std::sin(b);
}
cx cos(cx a, cx b) {
return std::cos(b);
}
cx tan(cx a, cx b) {
return std::tan(b);
}
cx asin(cx a, cx b) {
return std::asin(b);
}
cx acos(cx a, cx b) {
return std::acos(b);
}
cx atan(cx a, cx b) {
return std::atan(b);
}
cx sqrt(cx a, cx b) {
return std::sqrt(b);
}
cx abs(cx a, cx b) {
return std::abs(b);
}
cx rpm(cx a, cx b) { //random + or -
return (std::rand() % 2 == 0) ? 1 : -1;
}
Node::Node(Node * parent, string val) {
m_parent = parent;
m_val = val;
m_left = NULL;
m_right = NULL;
}
Node::~Node() {
delete m_left;
delete m_right;
}
string Node::toString() {
return m_val;
}
// TREE
string Tree::delim[] = {"+", "-", "*", "/", "^", "ln;", "rpm;sin;cos;tan;log;abs;", "sqrt;asin;acos;atan;", "ABCDEFGHIJKLMNOPQRSTUVWXYZ","pi;e;phi;"};
bool Tree::initd = false;
std::unordered_map<std::string, cx (* const)(cx,cx)> Tree::parseops = std::unordered_map<std::string, cx (* const)(cx,cx)>();
std::unordered_map<std::string, Tree*> Tree::variables = std::unordered_map<std::string, Tree*>();
Tree::Tree(string expr) {
if(!initd) {
initd = true;
init();
}
m_fct = expr;
for(int i = m_fct.length()-1; i >= 0; i--)
if(m_fct[i] == 32) //Whitespace character
m_fct.erase(i, 1);
m_root = new Node(NULL, m_fct);
parse();
}
Tree::~Tree() {
delete m_root;
}
void Tree::init() {
Tree::parseops.emplace("+", padd);
Tree::parseops.emplace("-", psub);
Tree::parseops.emplace("*",pmul);
Tree::parseops.emplace("/",pdiv);
Tree::parseops.emplace("^",ppow);
Tree::parseops.emplace("ln",plog);
Tree::parseops.emplace("sin",psin);
Tree::parseops.emplace("cos",pcos);
Tree::parseops.emplace("tan",ptan);
Tree::parseops.emplace("asin",pasin);
Tree::parseops.emplace("acos",pacos);
Tree::parseops.emplace("atan",patan);
Tree::parseops.emplace("sqrt",psqrt);
Tree::parseops.emplace("abs", pabs);
Tree::parseops.emplace("rpm", prpm);
Tree::variables.emplace("pi", new Tree(PARSER_SPI));
Tree::variables.emplace("phi", new Tree(PARSER_SPHI));
Tree::variables.emplace("e", new Tree(PARSER_SE));
Tree::variables.emplace("i", new Tree(PARSER_SI));
Tree::initd = true;
}
string Tree::toString(Node* n, string path) {
if(n->m_left != NULL && n->m_right != NULL)
return n->toString() + "; " + path + "L"+ " " + toString(n->m_left, path+"L") + "; " + path +"R" + " "+ toString(n->m_right, path+"R") + "; ";
else if(n->m_left != NULL)
return n->toString() + "; " +path + "L" + toString(n->m_left, path+"L") + "; ";
else if(n->m_right != NULL)
return n->toString() + "; " + path + "R" + toString(n->m_right, path+"R") + "; ";
else return n->toString();
}
string Tree::toString() {
return toString(m_root, "O");
}
// Check if parenthesis use is valid:
// Return 2 if parenthesis are balanced at any point before the end and are balanced overall
// Return 1 if parenthesis present and balanced
// Return -1 if unbalanced
// Return 0 if no parenthesis
int Tree::checkParenthesis(string s) {
bool hitParen = false;
bool balancedBeforeEnd = false;
int count = 0;
for(int i = 0; i < s.length(); i++) {
if(s[i] == '(') {
count++;
hitParen = true;
}
else if(s[i] == ')') count--;
if(hitParen && count == 0 && i < s.length()-1) {
balancedBeforeEnd = true;
}
}
if(!hitParen) return 0;
else if(count != 0) return -1;
else if(count == 0)
if(balancedBeforeEnd) return 2;
else return 1;
}
// Parse, avoids having to pass m_root
int Tree::parse() {
return parse(m_root);
}
int Tree::parse(Node *root) {
string& s = root->m_val;
int length = s.length();
switch(checkParenthesis(s)) {
case -1:
std::cout << "Invalid use of parenthesis \n";
return -1;
break;
case 0:
break;
case 1:
if(s[0] == '(') {
s.erase(length-1, 1);
s.erase(0, 1);
length -= 2;
}
break;
case 2:
break;
}
bool foundDelim = false;
for(int delimind = 0; delimind < 9 && !foundDelim; delimind++) {
for(int i = 0; i < length && !foundDelim; i++) {
// Skip parenthesis
if(s[i] == '(') {
int parenthCount = 1;
while(parenthCount != 0) {
i++;
if(s[i] == '(') {
parenthCount++;
}
else if(s[i] == ')') {
parenthCount--;
}
}
}
if(delimind < NUM_ARITH_OPS + NUM_FCT_LEN) {
// Arithmetic
if(delimind < NUM_ARITH_OPS && i > 0 && s[i] == (delim[delimind])[0]) {
foundDelim = true;
root->m_left = new Node(root, s.substr(0, i));
root->m_right = new Node(root, s.substr(i+1));
root->m_val = s[i];
}
// Functions
else if(delimind >= NUM_ARITH_OPS) {
int fLen = delimind + FCT_LEN_OFFSET;
if(delim[delimind].find( s.substr(i, i + fLen) + ";" ) != -1) {
foundDelim = true;
root->m_left = new Node(root, s.substr(0, i));
root->m_right = new Node(root, s.substr(i + fLen));
root->m_val = s.substr(i, i + fLen);
}
}
if(foundDelim) {
parse(root->m_left);
parse(root->m_right);
}
}
// Variables
else if(delimind == VAR && delim[VAR].find(s[i]) != -1) {
foundDelim = true;
variables.emplace(&s[i], new Tree("0") );
root->m_val = s[i];
}
// Constants
else if(delimind == CON) {
for(int cLen = 1; i + cLen < length && cLen <= MAX_CONST_LEN; cLen++) {
if(delim[CON].find( s.substr(i, i + cLen) + ";") != -1) {
foundDelim = true;
root->m_val = s.substr(i, i+cLen);
}
}
}
}
}
return 0;
}
//Calculate the value of a tree
cx Tree::value(Node *root) {
try {
return parseops.at(root->m_val)(value(root->m_left), value(root->m_right));
}
catch(const out_of_range& err0) {
try {
return variables.at(root->m_val)->eval();
}
catch(const out_of_range& err1) {
return stringToCx(root->m_val);
}
}
}
//set the value of a variable
void Tree::setVar(string var, string a) {
try {
delete variables[var];
variables[var] = new Tree(a);
}
catch(const std::out_of_range& err) {
std::cout << "variables does not exist \n";
}
}
void Tree::setVar(string var, cx a) {
try {
delete variables[var];
variables[var] = new Tree(parser::toString(a));
}
catch(const std::out_of_range& err) {
std::cout << "variables does not exist \n";
}
}
// Return the toString a variable's tree
string Tree::getVar(string var) {
try {
return variables.at(var)->toString();
}
catch(const std::out_of_range& err) {
std::cout << "variable undefined or does not exist \n";
}
}
// Evaluate a variable's numerical value
cx Tree::evalVar(string var) {
try {
return variables.at(var)->eval();
}
catch(const std::out_of_range& err) {
std::cout << "variable undefined or does not exist \n";
}
}
// Evalutate the tree's numerical value
cx Tree::eval() {
return value(m_root);
}
//True if parseops and constants have been filled
bool Tree::isInitd() {
return Tree::initd;
}
// s is operator => return 1
// s is variable => return 2
// s is invalid => return 0
int Tree::isToken(string s) {
try {
parseops.at(s);
return 1;
}
catch (const out_of_range& e) {
try {
variables.at(s);
return 2;
}
catch (const out_of_range& e1) {
return 0;
}
}
}
}