Class: Dhallish::Ast::RecordUnionSelector

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

Overview

rec: ast node of something that will be a record, key: name of key to access in record (string)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rec, key) ⇒ RecordUnionSelector

Returns a new instance of RecordUnionSelector.



429
430
431
432
# File 'lib/ast.rb', line 429

def initialize(rec, key)
	@rec = rec
	@key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



428
429
430
# File 'lib/ast.rb', line 428

def key
  @key
end

#recObject

Returns the value of attribute rec.



427
428
429
# File 'lib/ast.rb', line 427

def rec
  @rec
end

Instance Method Details

#compute_type(ctx) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/ast.rb', line 434

def compute_type(ctx)
	rectype = @rec.compute_type ctx
	assert("`.` can only be used on records and unions, the key `#{@key}` must exist") {
		((rectype.is_a? Types::Record or rectype.is_a? Types::Union) and !rectype.types[@key].nil?) \
		or (rectype.is_a? Types::Type and rectype..is_a? Types::Union)
	}
	if rectype.is_a? Types::Union
		Types::Optional.new rectype.types[@key]
	elsif rectype.is_a? Types::Record
		rectype.types[@key]
	else
		union_type = rectype.
		Types::Function.new union_type.types[@key], union_type
	end
end

#evaluate(ctx) ⇒ Object



450
451
452
453
454
455
456
457
458
459
# File 'lib/ast.rb', line 450

def evaluate(ctx)
	rec = @rec.evaluate ctx
	if rec.is_a? Union
		rec.select @key
	elsif rec.is_a? Hash  # <== Record
		rec[@key]
	else
		BuiltinFunction.new { |val| Union.new @key, val, rec }
	end
end