Class: TypedSexp

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

Instance Method Summary collapse

Methods inherited from Sexp

#array_type?

Constructor Details

#initialize(*args) ⇒ TypedSexp

Returns a new instance of TypedSexp.



41
42
43
44
45
# File 'lib/typed_sexp.rb', line 41

def initialize(*args)
  # TODO: should probably be CType.unknown
  @c_type = CType === args.last ? args.pop : nil
  super(*args)
end

Instance Method Details

#==(obj) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/typed_sexp.rb', line 22

def ==(obj)
  case obj
  when TypedSexp
    super && c_type == obj.c_type
  else
    false
  end
end

#_set_c_type(o) ⇒ Object



37
38
39
# File 'lib/typed_sexp.rb', line 37

def _set_c_type(o)
  @c_type = o
end

#c_typeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/typed_sexp.rb', line 68

def c_type
  unless array_type? then
    defined?(@c_type) && @c_type
  else
    types = self.c_types.flatten.uniq

    if types.size > 1 then
      CType.hetero
    else
      CType.homo
    end
  end
end

#c_type=(o) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/typed_sexp.rb', line 82

def c_type=(o)
  # HACK raise "You shouldn't call this on an #{first}" if array_type?
  # c_type is different in ruby2c than from sexp_processor. need renames
  raise "You shouldn't call this a second time, ever" unless
    @c_type.nil? or @c_type == CType.unknown
  _set_c_type(o)
end

#c_typesObject



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

def c_types
  raise "You shouldn't call this if not an #{@@array_types.join(' or ')}, was #{first} (#{self.inspect})" unless array_type?
  self.grep(Sexp).map { |x| x.c_type }
end

#inspectObject



47
48
49
50
51
52
53
# File 'lib/typed_sexp.rb', line 47

def inspect
  sexp_str = self.map {|x|x.inspect}.join(', ')
  c_type_str = (sexp_str.empty? ? "" : ", ") + "#{array_type? ? c_types.inspect : c_type}" unless c_type.nil?
  nnd = ")"
  nnd += ".line(#{line})" if line && ENV["VERBOSE"]
  "t(#{sexp_str}#{c_type_str}#{nnd}"
end

#new(*stuff) ⇒ Object



31
32
33
34
35
# File 'lib/typed_sexp.rb', line 31

def new(*stuff)
  r = super
  r.c_type = self.c_type if self.c_type
  r
end

#pretty_print(q) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/typed_sexp.rb', line 55

def pretty_print(q)
  nnd = ")"
  nnd << ".line(#{line})" if line && ENV["VERBOSE"]

  q.group(1, 't(', nnd) do
    q.seplist(self) {|v| q.pp v }
    unless @c_type.nil? then
      q.text ", " unless self.empty?
      q.pp @c_type
    end
  end
end

#to_aObject



95
96
97
98
99
100
101
# File 'lib/typed_sexp.rb', line 95

def to_a
  result = super
  if defined?(@c_type) and not @c_type.nil? then
    result += [ @c_type ]
  end
  result
end

#to_sObject



103
104
105
# File 'lib/typed_sexp.rb', line 103

def to_s
  inspect
end