Class: Crapshoot::Postfixer
- Inherits:
-
Object
- Object
- Crapshoot::Postfixer
- Defined in:
- lib/crapshoot/postfixer.rb
Overview
Translate the infix-notation tokens into postfix notation to make evaluating them easier.
Instance Method Summary collapse
-
#postfixify(infix_tokens) ⇒ Object
Postfixify turns the infix list of tokens from Scanner into a postfix list by repeatedly calling “step”.
- #process_independent(candidate) ⇒ Object
- #process_operator(candidate) ⇒ Object
-
#step ⇒ Object
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.
Instance Method Details
#postfixify(infix_tokens) ⇒ Object
Postfixify turns the infix list of tokens from Scanner into a postfix list by repeatedly calling “step”
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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) ⇒ Object
32 33 34 35 |
# File 'lib/crapshoot/postfixer.rb', line 32 def process_independent(candidate) return unless candidate.independent @postfix.push candidate end |
#process_operator(candidate) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# 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 ⇒ Object
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.
26 27 28 29 30 |
# File 'lib/crapshoot/postfixer.rb', line 26 def step candidate = @infix.shift process_independent candidate process_operator candidate end |