Created
August 5, 2018 04:47
-
-
Save mgechev/67b42d8eadc590e2900a61a632114f58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isOp = t => /\+|\-|\*|\//.test(t); | |
const isNum = n => /\d/.test(n); | |
const parse = (ts, i = 0) => { | |
if (i >= ts.length) return [null, i]; | |
if (isOp(ts[i])) { | |
const node = { op: ts[i] }; | |
const children = []; | |
let current; | |
let idx = i + 1; | |
while (idx < ts.length) { | |
[current, idx] = parse(ts, idx); | |
if (current) children.push(current); | |
} | |
node.children = children; | |
return [node, idx]; | |
} else { | |
return [{ val: parseInt(ts[i]) }, i + 1]; | |
} | |
}; | |
const ops = { | |
'+': a => a.reduce((a, c) => a + c, 0), | |
'-': a => a.reduce((a, c) => a - c), | |
'*': a => a.reduce((a, c) => a * c), | |
'/': a => a.reduce((a, c) => a / c), | |
}; | |
const eval = node => { | |
if (node.val !== undefined) { | |
return node.val; | |
} | |
return ops[node.op](node.children.map(eval)); | |
}; | |
const evaluate = s => { | |
const tokens = s.split(' ').map(c => c.trim()); | |
const [root, _] = parse(tokens); | |
return eval(root); | |
}; | |
console.log(evaluate('+ 1 2 3 * 3 2 2 - 2 + 2 3 4 + 1 2')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment