Class: Dhall::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/resolve.rb,
lib/dhall/normalize.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decode(*args) ⇒ Object



23
24
25
26
27
# File 'lib/dhall/binary.rb', line 23

def self.decode(*args)
	return new(value: args.first) if args.length == 1

	new(*args)
end

Instance Method Details

#&(other) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/dhall/ast.rb', line 48

def &(other)
	if self == other
		self
	elsif other.is_a?(Bool)
		other & self
	else
		Operator::And.new(lhs: self, rhs: other)
	end
end

#*(other) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/dhall/ast.rb', line 35

def *(other)
	case other
	when Natural
		other * self
	else
		Operator::Times.new(lhs: self, rhs: other)
	end
end

#+(other) ⇒ Object



31
32
33
# File 'lib/dhall/ast.rb', line 31

def +(other)
	Operator::Plus.new(lhs: self, rhs: other)
end

#as_dhallObject



109
110
111
# File 'lib/dhall/ast.rb', line 109

def as_dhall
	self
end

#cache_keyObject



43
44
45
# File 'lib/dhall/binary.rb', line 43

def cache_key
	"sha256:#{digest.hexdigest}"
end

#call(*args) ⇒ Object



13
14
15
16
17
# File 'lib/dhall/ast.rb', line 13

def call(*args)
	args.reduce(self) { |f, arg|
		Application.new(function: f, argument: arg)
	}.normalize
end

#concat(other) ⇒ Object



44
45
46
# File 'lib/dhall/ast.rb', line 44

def concat(other)
	Operator::ListConcatenate.new(lhs: self, rhs: other)
end

#deep_merge(other) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/dhall/ast.rb', line 78

def deep_merge(other)
	case other
	when EmptyRecord
		other.deep_merge(self)
	else
		Operator::RecursiveRecordMerge.new(lhs: self, rhs: other)
	end
end

#deep_merge_type(other) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/dhall/ast.rb', line 96

def deep_merge_type(other)
	case other
	when EmptyRecordType
		other.deep_merge_type(self)
	else
		Operator::RecursiveRecordTypeMerge.new(lhs: self, rhs: other)
	end
end

#dhall_eq(other) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/dhall/ast.rb', line 68

def dhall_eq(other)
	if self == other
		Bool.new(value: true)
	elsif other.is_a?(Bool)
		other.dhall_eq(self)
	else
		Operator::Equal.new(lhs: self, rhs: other)
	end
end

#digest(digest: Digest::SHA2.new(256)) ⇒ Object



39
40
41
# File 'lib/dhall/binary.rb', line 39

def digest(digest: Digest::SHA2.new(256))
	(digest << normalize.to_binary).freeze
end

#fetch(k) ⇒ Object



23
24
25
# File 'lib/dhall/ast.rb', line 23

def fetch(k)
	RecordSelection.new(record: self, selector: k)
end

#fusionObject



44
# File 'lib/dhall/normalize.rb', line 44

def fusion(*); end

#merge(other) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dhall/ast.rb', line 87

def merge(other)
	case other
	when EmptyRecord
		other.merge(self)
	else
		Operator::RightBiasedRecordMerge.new(lhs: self, rhs: other)
	end
end

#normalizeObject



28
29
30
# File 'lib/dhall/normalize.rb', line 28

def normalize
	with(ExpressionVisitor.new(&:normalize).visit(self))
end

#resolve(resolver: Resolvers::Default.new, relative_to: Import::Path.from_string(Pathname.pwd + "file")) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
# File 'lib/dhall/resolve.rb', line 513

def resolve(
	resolver: Resolvers::Default.new,
	relative_to: Import::Path.from_string(Pathname.pwd + "file")
)
	p = ExpressionResolver.for(self).resolve(
		resolver:    resolver,
		relative_to: relative_to
	)
	resolver.finish!
	p
end

#shift(amount, name, min_index) ⇒ Object



32
33
34
35
36
# File 'lib/dhall/normalize.rb', line 32

def shift(amount, name, min_index)
	with(ExpressionVisitor.new { |expr|
		expr.shift(amount, name, min_index)
	}.visit(self))
end

#slice(*keys) ⇒ Object



27
28
29
# File 'lib/dhall/ast.rb', line 27

def slice(*keys)
	RecordProjection.new(record: self, selectors: keys)
end

#substitute(var, with_expr) ⇒ Object



38
39
40
41
42
# File 'lib/dhall/normalize.rb', line 38

def substitute(var, with_expr)
	with(ExpressionVisitor.new { |expr|
		expr.substitute(var, with_expr)
	}.visit(self))
end

#to_cbor(packer = nil) ⇒ Object Also known as: to_binary



29
30
31
32
33
34
35
36
# File 'lib/dhall/binary.rb', line 29

def to_cbor(packer=nil)
	if packer
		packer.write(as_json)
		packer
	else
		CBOR.encode(as_json)
	end
end

#to_procObject



19
20
21
# File 'lib/dhall/ast.rb', line 19

def to_proc
	method(:call).to_proc
end

#to_sObject



105
106
107
# File 'lib/dhall/ast.rb', line 105

def to_s
	inspect
end

#|(other) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/dhall/ast.rb', line 58

def |(other)
	if self == other
		self
	elsif other.is_a?(Bool)
		other | self
	else
		Operator::Or.new(lhs: self, rhs: other)
	end
end