|
|
|
@ -35,18 +35,18 @@ def evaluate(input)
|
|
|
|
|
ss = StringScanner.new(input)
|
|
|
|
|
until ss.eos?
|
|
|
|
|
ss.scan(/\s*/)
|
|
|
|
|
case
|
|
|
|
|
stack << case
|
|
|
|
|
when n = ss.scan(/\d+/)
|
|
|
|
|
stack << stack.pop[n.to_i]
|
|
|
|
|
stack.pop[n.to_i]
|
|
|
|
|
when ss.scan(/\+/)
|
|
|
|
|
stack << ->(x) { stack.pop + x }
|
|
|
|
|
->(x) { stack.pop + x }
|
|
|
|
|
when ss.scan(/\*/)
|
|
|
|
|
stack << ->(x) { stack.pop * x }
|
|
|
|
|
->(x) { stack.pop * x }
|
|
|
|
|
when ss.scan(/\(/)
|
|
|
|
|
stack << ->(x) { x }
|
|
|
|
|
->(x) { x }
|
|
|
|
|
when ss.scan(/\)/)
|
|
|
|
|
n = stack.pop
|
|
|
|
|
stack << stack.pop[n]
|
|
|
|
|
stack.pop[n]
|
|
|
|
|
else
|
|
|
|
|
fail
|
|
|
|
|
end
|
|
|
|
|