Class: Type

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

Constant Summary collapse

KNOWN_TYPES =

REFACTOR: nuke this

{
  :unknown => "Unknown",
  :unknown_list => "Unknown list",
  :long => "Integer",
  :long_list => "Integer list",
  :str => "String",
  :str_list => "String list",
  :void => "Void",
  :bool => "Bool",
  :bool_list => "Bool list",
  :value => "Value",
  :value_list => "Value list",
  :function => "Function",
  :file => "File",
  :float => "Float",
  :float_list => "Float list",
  :symbol => "Symbol",
  :zclass => "Class",
  :homo => "Homogenous",
  :hetero => "Heterogenous",
  :fucked => "Untranslatable type",
}
TYPES =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, list = false) ⇒ Type

Returns a new instance of Type.



226
227
228
229
230
231
232
233
# File 'lib/support.rb', line 226

def initialize(type, list=false)
  # HACK
  unless KNOWN_TYPES.has_key? type or type.class.name =~ /Type$/ then
    raise "Unknown type Type.new(#{type.inspect})"
  end
  @type = Handle.new type
  @list = list
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



224
225
226
# File 'lib/support.rb', line 224

def list
  @list
end

#typeObject

Returns the value of attribute type.



223
224
225
# File 'lib/support.rb', line 223

def type
  @type
end

Class Method Details

.method_missing(type, *args) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/support.rb', line 197

def self.method_missing(type, *args)
  raise "Unknown type Type.#{type}" unless KNOWN_TYPES.has_key?(type)
  case type 
  when :unknown then
    return self.new(type)
  when :function then
    if args.size == 2 then
      $stderr.puts "\nWARNING: adding Type.unknown for #{caller[0]}" if $DEBUG
      args.unshift Type.unknown
    end
    return self.new(FunctionType.new(*args))
  else
    if type.to_s =~ /(.*)_list$/ then
      TYPES[type] = self.new($1.intern, true) unless TYPES.has_key?(type)
      return TYPES[type]
    else
      TYPES[type] = self.new(type) unless TYPES.has_key?(type)
      return TYPES[type]
    end
  end
end

.unknown_listObject



219
220
221
# File 'lib/support.rb', line 219

def self.unknown_list
  self.new(:unknown, true)
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/support.rb', line 252

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

  other.type == self.type && other.list? == self.list?
end

#function?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/support.rb', line 235

def function?
  not KNOWN_TYPES.has_key? self.type.contents
end

#hashObject



260
261
262
# File 'lib/support.rb', line 260

def hash
  type.contents.hash ^ @list.hash
end

#inspectObject



297
298
299
# File 'lib/support.rb', line 297

def inspect
  to_s
end

#list?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/support.rb', line 243

def list?
  @list
end

#list_typeObject

REFACTOR: this should be named type, but that’ll break code at the moment



248
249
250
# File 'lib/support.rb', line 248

def list_type
  @type.contents
end

#to_sObject



291
292
293
294
295
# File 'lib/support.rb', line 291

def to_s
  str = "Type.#{self.type.contents}"
  str << "_list" if self.list?
  str
end

#unify(other) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/support.rb', line 264

def unify(other)
  return other.unify(self) if Array === other
  return self if other == self and (not self.unknown?)
  return self if other.nil?
  if self.unknown? and other.unknown? then
    # link types between unknowns
    self.type = other.type
    self.list = other.list? or self.list? # HACK may need to be tri-state
  elsif self.unknown? then
    # other's type is now my type
    self.type.contents = other.type.contents
    self.list = other.list?
  elsif other.unknown? then
    # my type is now other's type
    other.type.contents = self.type.contents
    other.list = self.list?
  elsif self.function? and other.function? then
    self_fun = self.type.contents
    other_fun = other.type.contents

    self_fun.unify_components other_fun
  else
    raise TypeError, "Unable to unify #{self.inspect} with #{other.inspect}"
  end
  return self
end

#unknown?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/support.rb', line 239

def unknown?
  self.type.contents == :unknown
end