This is a very simple prefix notation calculator implementation in JavaScript, for the purpose of demonstrating a simple lexer, parser, compiler, and interpreter for my JSConf.eu talk, "JavaScript Compilers for Fun and Profit".
They're available packaged together here:
mini-calc-golfed.js contains each component JS-golfed down to < 140 bytes.
And individually as 140byt.es forks here:
- Lexer: https://gist.github.com/1259461
- Parser: https://gist.github.com/1259487
- Interpreter: https://gist.github.com/1259489
- Compiler: https://gist.github.com/1259490
Use the
?:
operator.function(n,a){return Array.isArray(n)?"("+n.slice(1).map(c).join(n[0])+")":n}