Class: Dhallish::Ast::UnionMerge

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, union) ⇒ UnionMerge

Returns a new instance of UnionMerge.



740
741
742
743
# File 'lib/ast.rb', line 740

def initialize(handler, union)
	@handler = handler
	@union = union
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



738
739
740
# File 'lib/ast.rb', line 738

def handler
  @handler
end

#unionObject

Returns the value of attribute union.



739
740
741
# File 'lib/ast.rb', line 739

def union
  @union
end

Instance Method Details

#compute_type(ctx) ⇒ Object



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/ast.rb', line 745

def compute_type(ctx)
	hdlr = @handler.compute_type ctx
	unin = @union.compute_type ctx

	assert("`merge` expects a record as first and a union as second argument (both with the same keys and they shall not be empty)") {
		hdlr.is_a? Types::Record and unin.is_a? Types::Union and hdlr.types.size == unin.types.size and hdlr.types.size > 0
	}
	rettype = nil
	hdlr.types.each { |key, fntype|
		argtype = unin.types[key]
		assert("`merge` record (of functions) and union must have the same fields") { !argtype.nil? and fntype.is_a? Types::Function }
		if rettype.nil?
			rettype = fntype.restype
		else
			# TODO: unification instead of ==
			assert("`merge` functions must all have the same return type") { fntype.restype == rettype }
		end
		assert("`merge` functions must take as argument the type of its union field") { fntype.argtype == argtype }
	}
	rettype
end

#evaluate(ctx) ⇒ Object



767
768
769
770
771
772
# File 'lib/ast.rb', line 767

def evaluate(ctx)
	hdlr = @handler.evaluate ctx
	unin = @union.evaluate ctx
	fn = hdlr[unin.init_label]
	fn.call(unin.init_val)
end