Skip to content

Instantly share code, notes, and snippets.

@daveallie
Created March 18, 2016 02:15
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 daveallie/cdb95f0960fa7761ec9f to your computer and use it in GitHub Desktop.
Save daveallie/cdb95f0960fa7761ec9f to your computer and use it in GitHub Desktop.
function evaluate(evalString)
m = match(r"(.*?)\((.*)\)(.*)", evalString)
if m != nothing
evalString = "$(m.captures[1])$(evaluate(m.captures[2]))$(m.captures[3])"
end
# Brackets are dealt with
# Add
m = match(r"(.*)\+(.*)", evalString)
if m != nothing
evalString = "$(evaluate(m.captures[1]) + evaluate(m.captures[2]))"
end
# Subtract
m = match(r"(.*)\-(.*)", evalString)
if m != nothing
evalString = "$(evaluate(m.captures[1]) - evaluate(m.captures[2]))"
end
# Multiply
m = match(r"(.*)\*(.*)", evalString)
if m != nothing
evalString = "$(evaluate(m.captures[1]) * evaluate(m.captures[2]))"
end
# Divide
m = match(r"(.*)\/(.*)", evalString)
if m != nothing
evalString = "$(evaluate(m.captures[1]) / evaluate(m.captures[2]))"
end
parse(Float64, evalString)
end
s = "2 * (4+(3 / 6 + 4 * 6)) + 6"
println(evaluate(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment