Class: Crapshoot::Tokens::Arithmetic
- Defined in:
- lib/crapshoot/tokens/arithmetic.rb
Instance Method Summary collapse
- #eval(stack) ⇒ Object
- #high_priority? ⇒ Boolean
- #independent ⇒ Object
-
#initialize(operation) ⇒ Arithmetic
constructor
A new instance of Arithmetic.
- #inspect ⇒ Object
- #precedent(stack_top) ⇒ Object
Constructor Details
#initialize(operation) ⇒ Arithmetic
Returns a new instance of Arithmetic.
4 5 6 |
# File 'lib/crapshoot/tokens/arithmetic.rb', line 4 def initialize(operation) @operation = operation end |
Instance Method Details
#eval(stack) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/crapshoot/tokens/arithmetic.rb', line 21 def eval(stack) r = stack.pop l = stack.pop @result = Result.new l.send(@operation.to_sym, r) @result.description = "#{l.description}#{@operation}#{r.description}" @result.detailed_description = l.detailed_description + [[@operation, @operation]] + r.detailed_description return @result end |
#high_priority? ⇒ Boolean
17 18 19 |
# File 'lib/crapshoot/tokens/arithmetic.rb', line 17 def high_priority? @operation == '*' || @operation == '/' end |
#independent ⇒ Object
8 9 10 |
# File 'lib/crapshoot/tokens/arithmetic.rb', line 8 def independent false end |
#inspect ⇒ Object
30 31 32 |
# File 'lib/crapshoot/tokens/arithmetic.rb', line 30 def inspect "<Crapshoot::Tokens::Arithmetic operation=#{@operation}>" end |
#precedent(stack_top) ⇒ Object
12 13 14 15 |
# File 'lib/crapshoot/tokens/arithmetic.rb', line 12 def precedent(stack_top) return true if high_priority? && !stack_top.high_priority? return false end |