Skip to content

Instantly share code, notes, and snippets.

@jkarmel
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkarmel/a74c136ee240eb6037e9 to your computer and use it in GitHub Desktop.
Save jkarmel/a74c136ee240eb6037e9 to your computer and use it in GitHub Desktop.
fns =
'+': (a,b) -> a + b
'*': (a,b) -> a * b
parse = (input) ->
JSON.parse(input.replace(/\s/g,",").replace(/\+|\*/g, '"$&"'))
evaluate = (x)->
if (Array.isArray(x)) # x is an expression
results = x.map (expression) -> evaluate(expression)
results[0].apply {}, results[1..-1]
else if fns[x]
fns[x]
else
x
alert evaluate parse "[* [+ 1 2] 2]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment