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.



1577
1578
1579
1580
# File 'lib/sfp/sas_translator.rb', line 1577

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

Instance Method Details

#add_value(type, value) ⇒ Object



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

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



1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
# File 'lib/sfp/sas_translator.rb', line 1614

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



1653
1654
1655
1656
# File 'lib/sfp/sas_translator.rb', line 1653

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)


1645
1646
1647
1648
1649
1650
1651
# File 'lib/sfp/sas_translator.rb', line 1645

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



1610
1611
1612
# File 'lib/sfp/sas_translator.rb', line 1610

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

#visit(name, value, parent) ⇒ Object



1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
# File 'lib/sfp/sas_translator.rb', line 1582

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