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



25
26
27
28
29
# File 'lib/dhall/binary.rb', line 25

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

  new(*args)
end

Instance Method Details

#&(other) ⇒ Object



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

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



38
39
40
41
42
43
44
# File 'lib/dhall/ast.rb', line 38

def *(other)
  if other.is_a?(Natural) && other.zero?
    other * self
  else
    Operator::Times.new(lhs: self, rhs: other)
  end
end

#+(other) ⇒ Object



34
35
36
# File 'lib/dhall/ast.rb', line 34

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

#annotate(type) ⇒ Object



107
108
109
# File 'lib/dhall/ast.rb', line 107

def annotate(type)
  TypeAnnotation.new(value: self, type: type)
end

#as_dhallObject



115
116
117
# File 'lib/dhall/ast.rb', line 115

def as_dhall
  self
end

#cache_keyObject



48
49
50
# File 'lib/dhall/binary.rb', line 48

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

#call(*args) ⇒ Object



16
17
18
19
20
# File 'lib/dhall/ast.rb', line 16

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

#concat(other) ⇒ Object



46
47
48
# File 'lib/dhall/ast.rb', line 46

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

#deep_merge(other) ⇒ Object



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

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



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

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



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

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

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



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

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

#fetch(k) ⇒ Object



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

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

#fusionObject



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

def fusion(*); end

#merge(other) ⇒ Object



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

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

#normalizeObject



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

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

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



523
524
525
526
527
528
529
530
531
532
533
# File 'lib/dhall/resolve.rb', line 523

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



43
44
45
46
47
# File 'lib/dhall/normalize.rb', line 43

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

#slice(*keys) ⇒ Object



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

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

#substitute(var, with_expr) ⇒ Object



49
50
51
52
53
# File 'lib/dhall/normalize.rb', line 49

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

#to_binaryObject



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

def to_binary
  CBOR.encode(::CBOR::Tagged.new(55799, self))
end

#to_cbor(packer = nil) ⇒ Object



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

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

#to_procObject



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

def to_proc
  method(:call).to_proc
end

#to_sObject



111
112
113
# File 'lib/dhall/ast.rb', line 111

def to_s
  inspect
end

#|(other) ⇒ Object



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

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