Class: FunctionType

Inherits:
Object
  • Object
show all
Defined in:
lib/support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver_type, formal_types, return_type) ⇒ FunctionType

Returns a new instance of FunctionType.



114
115
116
117
118
119
# File 'lib/support.rb', line 114

def initialize(receiver_type, formal_types, return_type)
  raise "nil not allowed" if formal_types.nil? or return_type.nil?
  @receiver_type = receiver_type
  @formal_types = formal_types
  @return_type = return_type
end

Instance Attribute Details

#formal_typesObject

Returns the value of attribute formal_types.



111
112
113
# File 'lib/support.rb', line 111

def formal_types
  @formal_types
end

#receiver_typeObject

Returns the value of attribute receiver_type.



110
111
112
# File 'lib/support.rb', line 110

def receiver_type
  @receiver_type
end

#return_typeObject

Returns the value of attribute return_type.



112
113
114
# File 'lib/support.rb', line 112

def return_type
  @return_type
end

Instance Method Details

#==(other) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/support.rb', line 121

def ==(other)
  return nil unless other.class == self.class

  return false unless other.receiver_type == self.receiver_type
  return false unless other.return_type == self.return_type
  return false unless other.formal_types == self.formal_types
  return true
end

#to_sObject



144
145
146
147
148
149
150
# File 'lib/support.rb', line 144

def to_s
  formals = formal_types.map do |t|
    t.inspect
  end

  "function(#{receiver_type.inspect}, [#{formals.join ', '}], #{return_type.inspect})"
end

#unify_components(other) ⇒ Object

Raises:

  • (TypeError)


130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/support.rb', line 130

def unify_components(other)
  raise TypeError, "Unable to unify: different number of args #{self.inspect} vs #{other.inspect}" unless
    @formal_types.length == other.formal_types.length

  @formal_types.each_with_index do |t, i|
    t.unify other.formal_types[i]
  end

  @receiver_type.unify other.receiver_type
  @return_type.unify other.return_type
#  rescue RuntimeError # print more complete warning message
#    raise "Unable to unify\n#{self}\nwith\n#{other}"
end