-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression-parser.js
168 lines (140 loc) · 5.32 KB
/
expression-parser.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
/**
* @file Used for parsing infix expressions, and converting them to postfix.
*
* @copyright Oscar Litorell 2019
*/
/**
* Used for parsing infix expressions, and converting them to postfix.
* @hideconstructor
*/
class ExpressionParser {
/**
* Check if a character is a number or decimal.
* @param {string} char
* @returns {boolean}
*/
static isNumeral(char) {
return "0123456789.".includes(char);
}
/**
* Check if a character is a letter of the alphabet.
* @param {string} char
* @returns {boolean}
*/
static isAlpha(char) {
return (char.toLowerCase() !== char.toUpperCase());
}
/**
* Get the type of a character, i.e. of it's a number, letter of the alphabet or something else.
* @param {string} char
* @returns {string} "number", "alpha" or "other"
*/
static getType(char) {
if (ExpressionParser.isNumeral(char)) return "number";
if (ExpressionParser.isAlpha(char)) return "alpha";
return "other";
}
/**
* Get the token at a given index in an expression.
* @param {string} expression - The expression that should be parsed.
* @param {number} index - The index where the token begins.
* @returns {string} The complete token.
*/
static parseToken(expression, index) {
let char = expression[index];
let oneChars = "+-*/^()";
let type;
if (oneChars.includes(char)) {
return char;
} else {
type = ExpressionParser.getType(expression[index]);
}
for (var i = index + 1; i < expression.length; i++) {
if (ExpressionParser.getType(expression[i]) !== type) {
// If last character is not i
if (!(expression[i] === "i" && (ExpressionParser.getType(expression[i - 1]) === type))) break;
};
}
let token = expression.substring(index, i);
return token;
}
/**
* Check if a token is a function.
* @param {string} token
* @returns {boolean}
*/
static isFunction(token) {
return (token in operations && operations[token].args === 1);
}
/**
* Check if a token is an operator.
* @param {string} token
* @returns {boolean}
*/
static isOperator(token) {
return (token in operations && operations[token].args === 2);
}
/**
* Check if a token is a number
* @param {string} token
* @returns {boolean}
*/
static isNumber(token) {
return (token === "i" || ExpressionParser.isNumeral(token[0]));
}
/**
* Parse an infix notation expression into a postfix expression. Uses the shunting-yard algorithm:
* <br>
* <a href="https://en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail">https://en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail</a>
* @param {string} expression - The expression to parse written in infix notation.
* @returns {string[]} The postfix expression stack.
* @example
* // returns ["2", "4", "3", "-", "*"]
* ExpressionParser.parseExpression("2*(4-3)")
*/
static parseExpression(expression) {
let operators = {
"^": {associativity: "right", precedence: 4},
"*": {associativity: "left", precedence: 3},
"/": {associativity: "left", precedence: 3},
"+": {associativity: "left", precedence: 2},
"-": {associativity: "left", precedence: 2}
}
expression = expression.replace(/\s/g, "").replace(/\(-/g, "(0-");
if (expression[0] === "-") expression = "0" + expression;
let operatorStack = [];
let outputQueue = [];
for (let i = 0; i < expression.length;) {
let token = ExpressionParser.parseToken(expression, i);
i += token.length;
if (ExpressionParser.isFunction(token)) {
operatorStack.push(token);
} else if (ExpressionParser.isOperator(token)) {
let top = operatorStack[operatorStack.length - 1];
while (operatorStack.length > 0 && (ExpressionParser.isFunction(top)
|| (ExpressionParser.isOperator(top) && operators[top].precedence > operators[token].precedence)
|| (ExpressionParser.isOperator(top) && operators[top].precedence === operators[token].precedence && operators[top].associativity === "left"))
&& (top !== "(")) {
outputQueue.push(operatorStack.pop());
top = operatorStack[operatorStack.length - 1];
}
operatorStack.push(token);
} else if (token === "(") {
operatorStack.push(token);
} else if (token === ")") {
while (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] !== "(") {
outputQueue.push(operatorStack.pop())
}
if (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] === "(") {
operatorStack.pop();
}
} else {
outputQueue.push(token);
}
}
while (operatorStack.length > 0) {
outputQueue.push(operatorStack.pop())
}
return outputQueue;
}
}