Class: CType

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

Constant Summary collapse

KNOWN_TYPES =

REFACTOR: nuke this

{
  :bool         => "Bool",
  :bool_list    => "Bool list",
  :const        => "Const",
  :file         => "File",
  :float        => "Float",
  :float_list   => "Float list",
  :function     => "Function",
  :long         => "Integer",
  :long_list    => "Integer list",
  :range        => "Range",
  :regexp       => "Regular Expression",
  :str          => "String",
  :str_list     => "String list",
  :symbol       => "Symbol",
  :value        => "Value",
  :value_list   => "Value list",
  :void         => "Void",
  :zclass       => "Class",

  :fucked       => "Untranslatable type",
  :hetero       => "Heterogenous",
  :homo         => "Homogenous",
  :unknown      => "Unknown",
  :unknown_list => "Unknown list",
}
TYPES =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, list = false) ⇒ CType

Returns a new instance of CType.



73
74
75
76
77
78
79
80
# File 'lib/type.rb', line 73

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.



71
72
73
# File 'lib/type.rb', line 71

def list
  @list
end

#typeObject

Returns the value of attribute type.



70
71
72
# File 'lib/type.rb', line 70

def type
  @type
end

Class Method Details

.function(lhs_type, arg_types, return_type = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/type.rb', line 37

def self.function lhs_type, arg_types, return_type = nil
  unless return_type then
    $stderr.puts "\nWARNING: adding Type.unknown for #{caller[0]}" if $DEBUG
    # TODO: gross, maybe go back to the *args version from method_missing
    return_type = arg_types
    arg_types = lhs_type
    lhs_type = CType.unknown
  end

  self.new FunctionType.new(lhs_type, arg_types, return_type)
end

.method_missing(type, *args) ⇒ Object



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

def self.method_missing(type, *args)
  raise "Unknown type Type.#{type} (#{type.inspect})" unless
    KNOWN_TYPES.has_key?(type)

  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

.unknownObject



49
50
51
# File 'lib/type.rb', line 49

def self.unknown
  self.new :unknown
end

.unknown_listObject



66
67
68
# File 'lib/type.rb', line 66

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

Instance Method Details

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

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/type.rb', line 99

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

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

#function?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/type.rb', line 82

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

#hashObject



107
108
109
# File 'lib/type.rb', line 107

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

#inspectObject



144
145
146
# File 'lib/type.rb', line 144

def inspect
  to_s
end

#list?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/type.rb', line 90

def list?
  @list
end

#list_typeObject

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



95
96
97
# File 'lib/type.rb', line 95

def list_type
  @type.contents
end

#to_sObject



138
139
140
141
142
# File 'lib/type.rb', line 138

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

#unify(other) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/type.rb', line 111

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)


86
87
88
# File 'lib/type.rb', line 86

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