phrase-engine
Version:
Language files on steroids for conversational UIs that aren't boring.
52 lines • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const peek_1 = require("./peek");
exports.isOperator = (o) => (o in {
'&': true,
'|': true,
'!': true,
});
exports.precedence = (o) => ({
'&': 2,
'|': 1,
'!': 3
}[o]);
exports.default = (expression) => {
const operator_stack = [];
const working = [];
let identifier = [];
const pushIdentifier = () => !!identifier.length
&& working.push(identifier.join(''))
&& (identifier = [])
&& true;
expression
.replace(/\s/ig, '')
.split('')
.forEach(char => {
if (char === '(') {
pushIdentifier();
operator_stack.push('(');
}
else if (char === ')') {
pushIdentifier();
let popped = operator_stack.pop();
while (!!popped && popped !== '(') {
working.push(popped);
popped = operator_stack.pop();
}
}
else if (exports.isOperator(char)) {
pushIdentifier();
while (!!peek_1.default(operator_stack) && exports.precedence(peek_1.default(operator_stack)) > exports.precedence(char)) {
working.push(operator_stack.pop());
}
operator_stack.push(char);
}
else {
identifier.push(char);
}
});
pushIdentifier();
return working.concat(operator_stack.reverse());
};
//# sourceMappingURL=expressionStack.js.map