Hacky Polish Notation
const f = (o, ...a) => eval(a.join(o));
const polish = eq => eval(
eq.replace(/\s+/g, ' ')
.replace(/(\))\s([0-9])/g, '$1,$2')
.replace(/([0-9]+)[^\)]/g, '$1,')
.replace(/\(\s?([\+\-\*\\/])/g, 'f(`$1`,')
);
console.log(polish('(* 2 2)'));
console.log(polish('(* 2 2 (+ 3 2 1))'));
console.log(polish('(- 10 3)'));
console.log(polish('(/ (+ 10 10 (* 2 2)) 3)'));
Hacky way to parse polish notation. This uses regular expressions to transform polish notation into javascript that can be run with eval
. Just a weird/fun idea…