Class: Duby::JVM::Types::IntegerType

Inherits:
Number show all
Defined in:
lib/duby/jvm/types/integers.rb

Constant Summary

Constants inherited from AST::TypeReference

AST::TypeReference::BlockType, AST::TypeReference::ErrorType, AST::TypeReference::NoType, AST::TypeReference::NullType, AST::TypeReference::UnreachableType

Instance Attribute Summary

Attributes inherited from Type

#inner_class

Attributes inherited from AST::TypeReference

#array, #meta

Attributes included from AST::Named

#name

Attributes inherited from AST::Node

#children, #inferred_type, #newline, #parent, #position

Instance Method Summary collapse

Methods inherited from Number

#add_delegates, #boolean_operator, #box, #delegate_intrinsic, #math_operator, #suffix, #unary_operator

Methods inherited from PrimitiveType

#convertible_to?, #initialize, #interfaces, #newarray, #primitive?, #primitive_type, #superclass

Methods inherited from Type

#add_enumerable_macros, #add_macro, #add_method, #aload, #array?, #array_type, #assignable_from?, #astore, #basic_type, #compatible?, #component_type, #constructor, #declared_class_methods, #declared_constructors, #declared_instance_methods, #declared_intrinsics, #dynamic?, #expand_each, #field_getter, #field_setter, #get_method, #initialize, #inner_class?, #inner_class_getter, #inspect, #interface?, #interfaces, #intrinsics, #is_parent, #iterable?, #java_method, #jvm_type, #log, #meta, #meta?, #newarray, #primitive?, #return, #store, #superclass, #to_source, #unmeta, #void?, #wide?

Methods included from MethodLookup

#each_is_exact, #each_is_exact_or_subtype_or_convertible, #field_lookup, #find_jls, #find_method, #inner_class, #is_more_specific?, #log, #phase1, #phase2, #phase3, #primitive_convertible?

Methods inherited from AST::TypeReference

#==, #block?, #compatible?, #component_type, #eql?, #error?, #hash, #initialize, #is_parent, #iterable?, #meta?, #narrow, #null?, #primitive?, #to_s, #unmeta, #unreachable?

Methods included from AST::Named

#to_s

Methods inherited from AST::Node

#<<, ===, #[], #_set_parent, child, child_name, #each, #empty?, #expr?, #initialize, #initialize_copy, #insert, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

This class inherits a constructor from Duby::JVM::Types::PrimitiveType

Instance Method Details

#add_intrinsicsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/duby/jvm/types/integers.rb', line 96

def add_intrinsics
  super
  math_operator('<<', 'shl')
  math_operator('>>', 'shr')
  math_operator('>>>', 'ushr')
  math_operator('|', 'or')
  math_operator('&', 'and')
  math_operator('^', 'xor')
  unary_operator('~', 'not')

  add_macro('downto', Int, Duby::AST.block_type) do |transformer, call|
    build_loop(call.parent, call.position, transformer,
               call.block, call.target, call.parameters[0], false, true)
  end
  add_macro('upto', Int, Duby::AST.block_type) do |transformer, call|
    build_loop(call.parent, call.position, transformer,
               call.block, call.target, call.parameters[0], true, true)
  end
  add_macro('times', Duby::AST.block_type) do |transformer, call|
    build_loop(call.parent, call.position, transformer,
               call.block, Duby::AST::fixnum(nil, call.position, 0),
               call.target, true, false)
  end
end

#box_typeObject



53
54
55
# File 'lib/duby/jvm/types/integers.rb', line 53

def box_type
  java.lang.Integer
end

#build_loop(parent, position, duby, block, first_value, last_value, ascending, inclusive) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/duby/jvm/types/integers.rb', line 61

def build_loop(parent, position, duby, block, first_value,
               last_value, ascending, inclusive)
  if ascending
    comparison = "<"
    op = "+="
  else
    comparison = ">"
    op = "-="
  end
  comparison << "=" if inclusive
  forloop = Duby::AST::Loop.new(parent, position, true, false) do |forloop|
    first, last = duby.tmp, duby.tmp
    init = duby.eval("#{first} = 0; #{last} = 0;")
    init.children[-2].value = first_value
    init.children[-1].value = last_value
    forloop.init << init

    var = (block.args.args || [])[0]
    if var
      forloop.pre << duby.eval(
          "#{var.name} = #{first}", '', forloop, first, last)
    end
    forloop.post << duby.eval("#{first} #{op} 1")
    [
      Duby::AST::Condition.new(forloop, position) do |c|
        [duby.eval("#{first} #{comparison} #{last}",
                   '', forloop, first, last)]
      end,
      nil
    ]
  end
  forloop.body = block.body
  forloop
end

#init_value(builder) ⇒ Object



22
23
24
# File 'lib/duby/jvm/types/integers.rb', line 22

def init_value(builder)
  builder.iconst_0
end

#jump_if(builder, op, label) ⇒ Object



57
58
59
# File 'lib/duby/jvm/types/integers.rb', line 57

def jump_if(builder, op, label)
  builder.send "if_icmp#{op}", label
end

#literal(builder, value) ⇒ Object



18
19
20
# File 'lib/duby/jvm/types/integers.rb', line 18

def literal(builder, value)
  builder.push_int(value)
end

#load(builder, index) ⇒ Object



26
27
28
# File 'lib/duby/jvm/types/integers.rb', line 26

def load(builder, index)
  builder.iload(index)
end

#math_typeObject



49
50
51
# File 'lib/duby/jvm/types/integers.rb', line 49

def math_type
  Int
end

#prefixObject



45
46
47
# File 'lib/duby/jvm/types/integers.rb', line 45

def prefix
  'i'
end

#widen(builder, type) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/duby/jvm/types/integers.rb', line 30

def widen(builder, type)
  case type
  when Byte, Short, Int
    # do nothing
  when Long
    builder.i2l
  when Float
    builder.i2f
  when Double
    builder.i2d
  else
    raise ArgumentError, "Invalid widening conversion from #{name} to #{type}"
  end
end