Class: Keisan::Tokens::ArithmeticOperator

Inherits:
Operator show all
Defined in:
lib/keisan/tokens/arithmetic_operator.rb

Constant Summary collapse

EXPONENT =
/(?:\*\*)/
TIMES =
/(?:\*)/
DIVIDE =
/(?:\/)/
PLUS_OR_MINUS =
/(?:[\+\-]+)/
REGEX =
/(#{EXPONENT}|#{TIMES}|#{DIVIDE}|#{PLUS_OR_MINUS})/

Instance Attribute Summary

Attributes inherited from Keisan::Token

#string

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operator

#type

Methods inherited from Keisan::Token

#initialize, #regex, #type, type

Constructor Details

This class inherits a constructor from Keisan::Token

Class Method Details

.regexObject



10
11
12
# File 'lib/keisan/tokens/arithmetic_operator.rb', line 10

def self.regex
  REGEX
end

Instance Method Details

#operator_typeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/keisan/tokens/arithmetic_operator.rb', line 14

def operator_type
  case string
  when EXPONENT
    # Must match first to override matching against single "*"
    :**
  when TIMES
    :*
  when DIVIDE
    :/
  when PLUS_OR_MINUS
    string.count("-").even? ? :+ : :-
  end
end