Class: Crapshoot::Tokens::Arithmetic

Inherits:
Base
  • Object
show all
Defined in:
lib/crapshoot/tokens/arithmetic.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


17
18
19
# File 'lib/crapshoot/tokens/arithmetic.rb', line 17

def high_priority?
  @operation == '*' || @operation == '/'
end

#independentObject



8
9
10
# File 'lib/crapshoot/tokens/arithmetic.rb', line 8

def independent
  false
end

#inspectObject



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