Class: Keisan::Tokens::ArithmeticOperator

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

Constant Summary collapse

EXPONENT =
/(?:\*\*)/
TIMES =
/(?:\*)/
DIVIDE =
/(?:\/)/
MODULO =
/(?:\%)/
PLUS_OR_MINUS =
/(?:[\+\-]+)/
REGEX =
/(#{EXPONENT}|#{TIMES}|#{DIVIDE}|#{MODULO}|#{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



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

def self.regex
  REGEX
end

Instance Method Details

#operator_typeObject



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

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