Class: Keisan::Tokens::Number

Inherits:
Keisan::Token show all
Defined in:
lib/keisan/tokens/number.rb

Constant Summary collapse

INTEGER_REGEX =
/\d+/
BINARY_REGEX =
/0b[0-1]+/
OCTAL_REGEX =
/0o[0-7]+/
HEX_REGEX =
/0x[0-9a-fA-F]+/
FLOATING_POINT_REGEX =
/\d+\.\d+/
SCIENTIFIC_NOTATION_REGEX =
/\d+(?:\.\d+)?e(?:\+|\-)?\d+/
REGEX =
/(#{BINARY_REGEX}|#{OCTAL_REGEX}|#{HEX_REGEX}|\d+(?:\.\d+)?(?:e(?:\+|\-)?\d+)?)/

Instance Attribute Summary

Attributes inherited from Keisan::Token

#string

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Keisan::Token

#initialize, #regex, #type, type

Constructor Details

This class inherits a constructor from Keisan::Token

Class Method Details

.regexObject



13
14
15
# File 'lib/keisan/tokens/number.rb', line 13

def self.regex
  REGEX
end

Instance Method Details

#valueObject



17
18
19
20
21
22
23
24
# File 'lib/keisan/tokens/number.rb', line 17

def value
  case string
  when /\A#{SCIENTIFIC_NOTATION_REGEX}\z/.freeze, /\A#{FLOATING_POINT_REGEX}\z/.freeze
    Float(string)
  else
    Integer(string)
  end
end