Class: Sfp::SasTranslator::VariableCollector

Inherits:
Visitor
  • Object
show all
Defined in:
lib/sfp/sas_translator.rb

Overview

collecting all variables and put them into @bucket

Instance Method Summary collapse

Constructor Details

#initialize(main) ⇒ VariableCollector

Returns a new instance of VariableCollector.



1638
1639
1640
1641
# File 'lib/sfp/sas_translator.rb', line 1638

def initialize(main)
  super(main)
  @init = main.root['initial']
end

Instance Method Details

#add_value(type, value) ⇒ Object



1698
1699
1700
1701
1702
1703
1704
# File 'lib/sfp/sas_translator.rb', line 1698

def add_value(type, value)
  if not @types.has_key?(type)
    @types[type] = Array.new
    @types[type] << Sfp::SasTranslator.null_of(type) if @types[type].length <= 0
  end
  @types[type] << value if not (value.is_a?(Hash) and value.isnull)
end

#get_type(name, value, parent) ⇒ Object



1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
# File 'lib/sfp/sas_translator.rb', line 1675

def get_type(name, value, parent)
  return value.type if value.is_a?(Sfp::Undefined) or value.is_a?(Sfp::Unknown)

  type = nil
  if parent.has_key?('_isa')
    isa = @main.root.at?(parent['_isa'])
    if not isa.nil?
      type = isa.type?(name)
      return type if not type.nil?
    end
  end
  type = self.isa?(value)

  return "(#{value['_isa']})" if value.is_a?(Hash) and value.isset and value.has_key?('_isa')
  
  return nil if type == nil
  
  return type if type.is_a?(String) and type.isref
  
  parent_class = @root.at?( @vars[parent.ref].type )
  return parent_class[name]['_isa']
end

#is_final(value) ⇒ Object



1714
1715
1716
1717
# File 'lib/sfp/sas_translator.rb', line 1714

def is_final(value)
  return true if value.is_a?(Hash) and not value.isnull and not value.isset
  return false
end

#isa?(value) ⇒ Boolean

Returns:

  • (Boolean)


1706
1707
1708
1709
1710
1711
1712
# File 'lib/sfp/sas_translator.rb', line 1706

def isa?(value)
  return '$.Boolean' if value.is_a?(TrueClass) or value.is_a?(FalseClass)
  return '$.Integer' if value.is_a?(Numeric)
  return '$.String' if value.is_a?(String) and not value.isref
  return value['_isa'] if value.is_a?(Hash) and value.isobject
  return nil
end

#null_value(isa) ⇒ Object



1671
1672
1673
# File 'lib/sfp/sas_translator.rb', line 1671

def null_value(isa)
  return {'_context' => 'null', '_isa' => isa}
end

#visit(name, value, parent) ⇒ Object



1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
# File 'lib/sfp/sas_translator.rb', line 1643

def visit(name, value, parent)
  return false if name[0,1] == '_'
  return false if (value.is_a?(Hash) and not (value.isobject or value.isnull or value.isset))
            # or value.is_a?(Array)
  
  var_name = parent.ref.push(name)
  isfinal = self.is_final(value)
  isref = (value.is_a?(String) and value.isref)
  isset = false
  value = @init.at?(value) if isref
  type = (isfinal ? self.isa?(value) : self.get_type(name, value, parent))
  if type == nil
    raise Exception, "Unrecognized type of variable: #{var_name}:#{value.class}"
  else
    value = null_value(type) if value == nil
    isset = true if type[0,1] == '('
    var = Variable.new(var_name, type, -1, value, nil, isfinal)
    var.isset = isset
    @vars[var.name] = var
    if isfinal and value.is_a?(Hash)
      value['_classes'].each { |c| add_value(c, value) }
    elsif not isref
      add_value(type, value)
    end
  end
  return true
end