Class: Duby::JVM::Types::Number

Inherits:
PrimitiveType show all
Defined in:
lib/duby/jvm/types/number.rb

Direct Known Subclasses

FloatType, IntegerType, LongType

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 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, #init_value, #initialize, #inner_class?, #inner_class_getter, #inspect, #interface?, #interfaces, #intrinsics, #is_parent, #iterable?, #java_method, #jvm_type, #load, #log, #meta, #meta?, #newarray, #prefix, #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_delegates(name, return_type = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/duby/jvm/types/number.rb', line 24

def add_delegates(name, return_type = nil)
  index = TYPE_ORDERING.index(math_type)
  larger_types = TYPE_ORDERING[index + 1, TYPE_ORDERING.size]
  larger_types.each do |type|
    delegate_intrinsic(name, type, return_type || type)
  end
end

#add_intrinsicsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/duby/jvm/types/number.rb', line 75

def add_intrinsics
  boolean_operator('<', :lt)
  boolean_operator('<=', :le)
  boolean_operator('==', :eq)
  boolean_operator('!=', :ne)
  boolean_operator('>=', :ge)
  boolean_operator('>', :gt)
  math_operator('+', :add)
  math_operator('-', :sub)
  math_operator('*', :mul)
  math_operator('/', :div)
  math_operator('%', :rem)
  unary_operator('-@', :neg)
  unary_operator('+@', nil)
end

#boolean_operator(name, op) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/duby/jvm/types/number.rb', line 38

def boolean_operator(name, op)
  add_method(name, [math_type], Boolean) do |compiler, call, expression|
    if expression
      # Promote the target or the argument if necessary
      convert_args(compiler,
                   [call.target, *call.parameters],
                   [math_type, math_type])
      compiler.method.op_to_bool do |label|
        jump_if(compiler.method, op, label)
      end
    end
  end
  add_delegates(name, Boolean)
end

#box(builder) ⇒ Object



91
92
93
# File 'lib/duby/jvm/types/number.rb', line 91

def box(builder)
  builder.invokestatic box_type, "valueOf", [box_type, math_type]
end

#delegate_intrinsic(name, type, return_type) ⇒ Object

Adds an intrinsic that delegates to an intrinsic in another primitive type. That type must support promoting the “this” argument.



14
15
16
17
18
19
20
21
22
# File 'lib/duby/jvm/types/number.rb', line 14

def delegate_intrinsic(name, type, return_type)
  args = [type]
  delegate = type.intrinsics[name][args]
  add_method(name, args, return_type) do |compiler, call, expression|
    if expression
      delegate.call(compiler, call, expression)
    end
  end
end

#jump_if(builder, op, label) ⇒ Object

if_cmpxx for non-ints



33
34
35
36
# File 'lib/duby/jvm/types/number.rb', line 33

def jump_if(builder, op, label)
  builder.send "#{prefix}cmp#{suffix}"
  builder.send "if#{op}", label
end

#math_operator(name, op) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/duby/jvm/types/number.rb', line 53

def math_operator(name, op)
  add_method(name, [math_type], math_type) do |compiler, call, expression|
    if expression
      # Promote the target or the argument if necessary
      convert_args(compiler,
                   [call.target, *call.parameters],
                   [math_type, math_type])
      compiler.method.send "#{prefix}#{op}"
    end
  end
  add_delegates(name)
end

#math_typeObject

The type returned by arithmetic operations with this type.



4
5
6
# File 'lib/duby/jvm/types/number.rb', line 4

def math_type
  self
end

#suffixObject



8
9
10
# File 'lib/duby/jvm/types/number.rb', line 8

def suffix
  ''
end

#unary_operator(name, op) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/duby/jvm/types/number.rb', line 66

def unary_operator(name, op)
  add_method(name, [], math_type) do |compiler, call, expression|
    if expression
      call.target.compile(compiler, true)
      compiler.method.send("#{prefix}#{op}") if op
    end
  end
end