Class: Dentaku::AST::Combinator

Inherits:
Operation show all
Defined in:
lib/dentaku/ast/combinators.rb

Direct Known Subclasses

And, Or

Constant Summary

Constants inherited from Node

Node::STATIC_CONTEXT, Node::STATIC_MODE_KEY

Instance Attribute Summary

Attributes inherited from Operation

#left, #right

Instance Method Summary collapse

Methods inherited from Operation

#accept, #display_operator, max_param_count, min_param_count, #operator_spacing, right_associative?

Methods inherited from Node

arity, #name, precedence, #pure?, resolve_class

Constructor Details

#initializeCombinator

Returns a new instance of Combinator.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dentaku/ast/combinators.rb', line 7

def initialize(*)
  super

  unless valid_node?(left)
    raise NodeError.new(:logical, left.type, :left),
          "#{self.class} requires logical operands"
  end
  unless valid_node?(right)
    raise NodeError.new(:logical, right.type, :right),
          "#{self.class} requires logical operands"
  end
end

Instance Method Details

#dependencies(context = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dentaku/ast/combinators.rb', line 24

def dependencies(context = {})
  return super if static_mode?(context)

  left_deps = left.dependencies(context)
  right_deps = right.dependencies(context)
  return [] if left_deps.empty? && left.pure? && decisive?(left.value(context))
  return [] if right_deps.empty? && right.pure? && decisive?(right.value(context))

  (left_deps + right_deps).uniq
rescue Dentaku::Error
  super
end

#typeObject



20
21
22
# File 'lib/dentaku/ast/combinators.rb', line 20

def type
  :logical
end

#value(context = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dentaku/ast/combinators.rb', line 37

def value(context = {})
  left_value = begin
    left.value(context)
  rescue UnboundVariableError => unbound
    unbound
  end

  unless left_value.is_a?(UnboundVariableError)
    return left_value if decisive?(left_value)

    return right.value(context)
  end

  # The left operand is unbound; the right operand can still decide the
  # result on its own. If it does not, the left value was needed.
  right_value = right.value(context)
  raise left_value unless decisive?(right_value)

  right_value
end