Sha256: 1ecaa86f85af2e63277ffc626c7335916c9b86cd57a48cba65b6fdfa4eca96b8

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

require 'delegate'

module Duby::JVM::Types

  # Represents a literal number that can be represented
  # in multiple types
  class NarrowingType < DelegateClass(PrimitiveType)
    def initialize(default_type, narrowed_type)
      super(default_type)
      @narrowed = default_type != narrowed_type && narrowed_type
    end
    
    def hash
      __getobj__.hash
    end

    # Changes this type to the smallest type that will hold
    # its literal value.
    def narrow!
      if @narrowed
        __setobj__(@narrowed)
        true
      end
    end
  end
  
  class FixnumLiteral < NarrowingType
    def self.range(type)
      type::MIN_VALUE .. type::MAX_VALUE
    end
    
    BYTE_RANGE = range(java.lang.Byte)
    SHORT_RANGE = range(java.lang.Short)
    INT_RANGE = range(java.lang.Integer)
    LONG_RANGE = range(java.lang.Long)

    def initialize(literal)
      default_type = case literal
      when INT_RANGE
        Int
      else
        Long
      end
      
      # TODO chars?
      # There's not really any way to tell if we should narrow to a char
      # or a byte/short.  I suppose we could try both, but that seems ugly.
      # Maybe it's the right thing to do though?
      narrowed_type = case literal
      when BYTE_RANGE
        Byte
      when SHORT_RANGE
        Short
      when INT_RANGE
        Int
      else
        Long
      end
      
      super(default_type, narrowed_type)
    end
  end

  class FloatLiteral < NarrowingType
    FLOAT_RANGE = java.lang.Float::MIN_VALUE .. java.lang.Float::MAX_VALUE
    NaN = java.lang.Float::NaN
    POSITIVE_INFINITY = java.lang.Float::POSITIVE_INFINITY
    NEGATIVE_INFINITY = java.lang.Float::NEGATIVE_INFINITY

    def initialize(literal)
      narrowed_type = case literal
      when FLOAT_RANGE, NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY
        Float
      else
        Double
      end
      
      # TODO Double should be default once it supports operators
      super(narrowed_type, narrowed_type)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
duby-0.0.3-java lib/duby/jvm/types/literals.rb
duby-0.0.2-java lib/duby/jvm/types/literals.rb
duby-0.0.1 lib/duby/jvm/types/literals.rb