Class: Dhallish::Ast::UnionLiteral

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map, init_label, init_val) ⇒ UnionLiteral

Returns a new instance of UnionLiteral.



683
684
685
686
687
# File 'lib/ast.rb', line 683

def initialize(map, init_label, init_val)
	@map = map
	@init_label = init_label
	@init_val = init_val
end

Instance Attribute Details

#init_labelObject

Returns the value of attribute init_label.



679
680
681
# File 'lib/ast.rb', line 679

def init_label
  @init_label
end

#init_valObject

Returns the value of attribute init_val.



680
681
682
# File 'lib/ast.rb', line 680

def init_val
  @init_val
end

#mapObject

map: label -> type, can be empty



678
679
680
# File 'lib/ast.rb', line 678

def map
  @map
end

#typeObject

Returns the value of attribute type.



681
682
683
# File 'lib/ast.rb', line 681

def type
  @type
end

Instance Method Details

#compute_type(ctx) ⇒ Object



689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/ast.rb', line 689

def compute_type(ctx)
	types = {}
	@map.keys.each { |key|
		node = @map[key]
		assert ("duplicate labels not allowed in union literals") { !types.include? key }
		type_type = node.compute_type(ctx)
		assert ("Type annotation in Union Literal not a type") { type_type.is_a? Types::Type }
		@map[key] = types[key] = type_type.
	}
	assert ("duplicate labels not allowed in union literals") { !types.include? @init_label }
	types[@init_label] = @init_val.compute_type(ctx)
	@type = Types::Union.new(types)
	@type
end

#evaluate(ctx) ⇒ Object



704
705
706
# File 'lib/ast.rb', line 704

def evaluate(ctx)
	Union.new @init_label, @init_val.evaluate(ctx), @type
end