class Crapshoot::Evaluator

Stack-based calculator that relies on the “eval” method of tokens.

Public Instance Methods

evaluate(tokens) click to toggle source
# File lib/crapshoot/evaluator.rb, line 4
def evaluate(tokens)
  @stack = []
  @tokens = tokens.dup
  until @tokens.empty?
    step
  end

  raise "Stack has too many entries: #{@stack.inspect}" unless @stack.length == 1

  return @stack[0]
end
step() click to toggle source
# File lib/crapshoot/evaluator.rb, line 16
def step
  candidate = @tokens.shift
  result = candidate.eval @stack
  @stack.push result
end