class Crapshoot::Postfixer

Translate the infix-notation tokens into postfix notation to make evaluating them easier.

Public Instance Methods

postfixify(infix_tokens) click to toggle source

Postfixify turns the infix list of tokens from Scanner into a postfix list by repeatedly calling “step”

# File lib/crapshoot/postfixer.rb, line 7
def postfixify(infix_tokens)
  @infix_orig = infix_tokens
  @infix = @infix_orig.dup
  @postfix = []
  @operator_stack = []
  until @infix.empty?
    step
  end

  until @operator_stack.empty?
    @postfix.push @operator_stack.pop
  end

  return @postfix
end
process_independent(candidate) click to toggle source
# File lib/crapshoot/postfixer.rb, line 32
def process_independent(candidate)
  return unless candidate.independent
  @postfix.push candidate
end
process_operator(candidate) click to toggle source
# File lib/crapshoot/postfixer.rb, line 37
def process_operator(candidate)
  return if candidate.independent

  if @operator_stack.empty? || candidate.precedent(@operator_stack.last)
    @operator_stack.push candidate
  else
    @postfix.push @operator_stack.pop
    @operator_stack.push candidate
  end
end
step() click to toggle source

step shifts an independent (Constant or Series) token to the postfix list, or loads a dependent (Arithmetic) token into the operator_stack for future postfixing.

# File lib/crapshoot/postfixer.rb, line 26
def step
  candidate = @infix.shift
  process_independent candidate
  process_operator candidate
end