/
Big.expression.js
192 lines (160 loc) · 6.41 KB
/
Big.expression.js
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
(function() {
Big.expression = { };
// Break the string into tokens.
Big.expression.tokenize = function(src) {
return src.split(/\s/);
};
// Tokenize in terms of infix notation.
Big.expression.tokenizeInfix = function(src) {
var
rawTokens = src
.replace(/(\()|(\))|(\d+(\.\d*)?)|(\+)|(\-)|(\*)|(\/)|(.*)/g, " $&")
.split(/\s/)
.slice(1),
result = [];
while (rawTokens.length) {
var temp = rawTokens.shift();
if (temp.length) {
result.push(temp);
}
}
return result;
};
// Matching pattern for an operator.
Big.expression.operatorPattern = /^[-+*\/]$/;
// Matching pattern for a parseable number.
Big.expression.numberPattern = /^\d+(\.\d*)?$/;
// Eval the parameter as an rpn expression string
// and return the resulting stack.
Big.expression.evalRpn = function(src) {
var
tokens = Big.expression.tokenize(src),
token,
stack = [];
// Loop through tokens:
while (tokens.length) {
token = tokens.shift();
// If the token represents a number,
// parse as a big and push.
if (Big.expression.numberPattern.test(token)) {
stack.push(new Big(token));
}
// If it's an operator,
// transform the top of the stack.
else if (Big.expression.operatorPattern.test(token)) {
if (stack.length < 2) {
throw "Big.expression.eval: Syntax Error, Insufficient Stack";
}
var
op2 = stack.pop(),
op1 = stack.pop();
// Switch on the operator type:
if (token == "+") {
stack.push(op1.plus(op2));
}
else if (token == "-") {
stack.push(op1.minus(op2));
}
else if (token == "*") {
stack.push(op1.times(op2));
}
else if (token == "/") {
stack.push(op1.over(op2));
}
}
else {
throw "Big.expression.eval: unrecognized token \"" + token + "\"";
}
}
return stack;
};
// Map operators to their precidence.
Big.expression.getPrecidence = function(op) {
if (op == "*") { return 3; }
else if (op == "/") { return 2; }
else if (op == "+") { return 1; }
else if (op == "-") { return 0; }
};
// Convert an infix expression string to RPN expression string.
Big.expression.fromInfixToRpn = function(src) {
var
tokens = Big.expression.tokenizeInfix(src),
token,
opStack = [],
result = [];
// Loop through tokens.
while (tokens.length) {
token = tokens.shift();
// If it's an open paren,
// push a marker to the operator stack.
if (token == "(") {
opStack.push(token);
}
// If it's a number,
// push the token to the result stack.
else if (Big.expression.numberPattern.test(token)) {
result.push(token);
}
// If it's an operator,
else if (Big.expression.operatorPattern.test(token)) {
var
topOperator = opStack[opStack.length - 1],
currentPrecidence = Big.expression.getPrecidence(token);
// If the operator stack is empty,
// or the top of the operator stack is an open paren marker,
// or the top of the operator stack has a lower precidence than the token,
// then push the token to the operator stack.
if (!opStack.length ||
topOperator == "(" ||
currentPrecidence > Big.expression.getPrecidence(topOperator)) {
opStack.push(token);
}
// Else pop operators from the operator stack and push them to the result stack,
// while there are operators in the stack,
// and an open paren marker has not been found,
// and the current precidence is less than the new top operator.
// Finally push the token to the operator stack.
else {
do {
result.push(opStack.pop());
}
while(
opStack.length &&
opStack[opStack.length - 1] != "(" &&
currentPrecidence < Big.expression.getPrecidence(opStack[opStack.length - 1])
);
opStack.push(token);
}
}
// If a close paren has been found,
// then pop the entire operator stack into the result stack,
// while the top of the stack is not an open paren marker.
else if (token == ")") {
while (opStack[opStack.length - 1] != "(")
{
result.push(opStack.pop());
if (!opStack.length) {
throw "Big.expression.fromInfix: No matching open paren.";
}
}
opStack.pop();
}
// The token didn't match any pattern.
else {
throw "Big.expression.fromInfix: unrecognized token \"" + token + "\"";
}
}
// After all of the tokens have been processed,
// pop the operator stack to the result stack.
while (opStack.length) {
result.push(opStack.pop());
}
//
return result.join(" ");
};
Big.expression.evalInfix = function(src) {
return Big.expression.evalRpn(
Big.expression.fromInfixToRpn(src)
);
}
})();