-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn.js
37 lines (30 loc) · 971 Bytes
/
rpn.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
import * as operator from './operator';
const NUMBER_REG = /^\d+(\.\d+)?$/;
const operators = {
'+': operator.add,
'-': operator.sub,
'x': operator.mul,
'/': operator.div
};
export default function rpn (expression) {
const stack = evalExpression(expression.split(' '));
if (stack.length > 1) throw new Error('The user input has too many values');
return stack[0];
}
function evalExpression (tokens) {
let stack = [];
for (const token of tokens) {
if (NUMBER_REG.test(token)) {
stack.push(parseFloat(token));
} else {
const argsCount = operators[token].length;
if (stack.length < argsCount) throw new Error('Stack must have as many parameters as operator requires to perform operation');
const args = stack.splice(stack.length - argsCount, argsCount);
stack.push(evalOperator(token, args));
}
}
return stack;
}
function evalOperator (token, args) {
return operators[token].apply(undefined, args);
}