Class: TypedSexp

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TypedSexp

Returns a new instance of TypedSexp.



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

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

Instance Method Details

#==(obj) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/typed_sexp.rb', line 10

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

#_set_sexp_type(o) ⇒ Object



19
20
21
# File 'lib/typed_sexp.rb', line 19

def _set_sexp_type(o)
  @sexp_type = o
end

#inspectObject



29
30
31
32
33
# File 'lib/typed_sexp.rb', line 29

def inspect
  sexp_str = self.map {|x|x.inspect}.join(', ')
  sexp_type_str = (sexp_str.empty? ? "" : ", ") + "#{array_type? ? sexp_types.inspect : sexp_type}" unless sexp_type.nil?
  return "t(#{sexp_str}#{sexp_type_str})"
end

#pretty_print(q) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/typed_sexp.rb', line 35

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

#sexp_typeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/typed_sexp.rb', line 45

def sexp_type
  unless array_type? then
    @sexp_type
  else
    types = self.sexp_types.flatten.uniq

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

#sexp_type=(o) ⇒ Object



59
60
61
62
63
64
# File 'lib/typed_sexp.rb', line 59

def sexp_type=(o)
  raise "You shouldn't call this on an #{first}" if array_type?
  raise "You shouldn't call this a second time, ever" unless
    @sexp_type.nil? or @sexp_type == Type.unknown
  _set_sexp_type(o)
end

#sexp_typesObject



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

def sexp_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.sexp_type }
end

#to_aObject



71
72
73
74
75
76
77
# File 'lib/typed_sexp.rb', line 71

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

#to_sObject



79
80
81
# File 'lib/typed_sexp.rb', line 79

def to_s
  inspect
end