Repository navigation
Expand file tree
/
Copy pathsimpleEval.js
More file actions
208 lines (187 loc) · 4.84 KB
/
Copy pathsimpleEval.js
File metadata and controls
208 lines (187 loc) · 4.84 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
const ASSOC = {
Left: 0,
Right: 1
}
const OPERATORS = {
"^": {
precedence: 4,
associativity: ASSOC.Right,
eval: (a, b) => {
return Math.pow(b, a);
}
},
"/": {
precedence: 3,
associativity: ASSOC.Left,
eval: (a, b) => {
return b / a;
}
},
"*": {
precedence: 3,
associativity: ASSOC.Left,
eval: (a, b) => {
return a * b;
}
},
"+": {
precedence: 2,
associativity: ASSOC.Left,
eval: (a, b) => {
return a + b;
}
},
"-": {
precedence: 2,
associativity: ASSOC.Left,
eval: (a, b) => {
return b - a;
}
}
}
const FUNCTIONS = {
"sin": {
precedence: 10,
associativity: ASSOC.Right,
num_operands: 1,
eval: (args) => {
return Math.sin(args[0]);
}
},
"cos": {
precedence: 10,
associativity: ASSOC.Right,
num_operands: 1,
eval: (args) => {
return Math.cos(args[0]);
}
},
"tan": {
precedence: 10,
associativity: ASSOC.Right,
num_operands: 1,
eval: (args) => {
return Math.tan(args[0]);
}
},
"log": {
precedence: 10,
associativity: ASSOC.Right,
num_operands: 1,
eval: (args) => {
return Math.log(args[0]);
}
},
"pi": {
precedence: 10,
associativity: ASSOC.Right,
num_operands: 0,
eval: (args) => {
return Math.PI;
}
}
}
function evaluate(postfixStack) {
if(postfixStack.length < 1) {
return null;
}
let tempStack = [];
while(postfixStack.length > 0) {
let token = postfixStack.pop();
if(isOperator(token)) {
tempStack.push(OPERATORS[token].eval(parseFloat(tempStack.pop()), parseFloat(tempStack.pop())))
} else if(isFunction(token)) {
let args = [];
for(let i = 0; i < FUNCTIONS[token].num_operands; i++) {
args[i] = tempStack.pop();
}
tempStack.push(FUNCTIONS[token].eval(args));
} else {
tempStack.push(token)
}
}
return tempStack.pop();
}
function reverse(array) {
let result = [];
while(array.length > 0) {
result.push(array.pop());
}
return result;
}
function isFunction(string) {
return string in FUNCTIONS;
}
function isOperator(string) {
return string in OPERATORS;
}
function isNumber(string) {
return string.match(/^[0-9]+|\.?[0-9]+$/)
}
function getAssociativity(token) {
if(token in OPERATORS) {
return OPERATORS[token].associativity;
} else if (token in FUNCTIONS) {
return FUNCTIONS[token].associativity
} else {
throw `token ${token} undefined`
}
}
function getPrecedence(token) {
if(token in OPERATORS) {
return OPERATORS[token].precedence;
} else if (token in FUNCTIONS) {
return FUNCTIONS[token].precedence;
} else {
throw `token ${token} undefined`
}
}
function print(str) {
console.log(str);
}
function infixToPostfix(rawInfix) {
infix = rawInfix.replace(/\s+/g, "");
infix = infix.split(/([\+\-\*\/\^\(\)])/).filter((item) => {
if(item !== '') {
return item;
}
});
// print(infix);
let outputQueue = [];
let operatorsStack = [];
let token;
for(let i = 0; i < infix.length; i++) {
// print(outputQueue);
token = infix[i];
if(isNumber(token)) {
outputQueue.push(parseFloat(token));
} else if(isOperator(token)) {
let o1 = token;
let o2 = operatorsStack[operatorsStack.length - 1];
while((isOperator(o2) || isFunction(o2)) && ((getAssociativity(o1) === ASSOC.Left && getPrecedence(o1) <= getPrecedence(o2))
|| (getAssociativity(o1) === ASSOC.Right && getPrecedence(o1) < getPrecedence(o2)))) {
outputQueue.push(operatorsStack.pop());
o2 = operatorsStack[operatorsStack.length - 1];
}
operatorsStack.push(o1);
} else if(isFunction(token)) {
operatorsStack.push(token);
} else if(token === "(") {
operatorsStack.push(token);
} else if(token === ")") {
while(operatorsStack[operatorsStack.length - 1] !== "(") {
outputQueue.push(operatorsStack.pop());
}
operatorsStack.pop();
} else {
console.log(`Unknown token: ${token}`)
exit();
}
}
while(operatorsStack.length > 0) {
outputQueue.push(operatorsStack.pop());
}
return reverse(outputQueue);
}
postfix = infixToPostfix(process.argv[2])
console.log(evaluate(postfix));