class TypedSexp

Public Class Methods

new(*args) click to toggle source
# 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

Public Instance Methods

==(obj) click to toggle source
# 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) click to toggle source
# File lib/typed_sexp.rb, line 19
def _set_sexp_type(o)
  @sexp_type = o
end
inspect() click to toggle source
# 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) click to toggle source
# 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_type() click to toggle source
# 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) click to toggle source
# 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_types() click to toggle source
# 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_a() click to toggle source
# 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_s() click to toggle source
# File lib/typed_sexp.rb, line 79
def to_s
  inspect
end