class Dentaku::AST::Case

Attributes

conditions[R]
else[R]
switch[R]

Public Class Methods

max_param_count() click to toggle source
# File lib/dentaku/ast/case.rb, line 17
def self.max_param_count
  Float::INFINITY
end
min_param_count() click to toggle source
# File lib/dentaku/ast/case.rb, line 13
def self.min_param_count
  2
end
new(*nodes) click to toggle source
# File lib/dentaku/ast/case.rb, line 21
def initialize(*nodes)
  @switch = nodes.shift

  unless @switch.is_a?(AST::CaseSwitchVariable)
    raise ParseError.for(:node_invalid), 'Case missing switch variable'
  end

  @conditions = nodes

  @else = nil
  @else = @conditions.pop if @conditions.last.is_a?(AST::CaseElse)

  @conditions.each do |condition|
    unless condition.is_a?(AST::CaseConditional)
      raise ParseError.for(:node_invalid), "#{condition} is not a CaseConditional"
    end
  end
end

Public Instance Methods

accept(visitor) click to toggle source
# File lib/dentaku/ast/case.rb, line 62
def accept(visitor)
  visitor.visit_case(self)
end
dependencies(context = {}) click to toggle source
# File lib/dentaku/ast/case.rb, line 55
def dependencies(context = {})
  # TODO: should short-circuit
  switch_dependencies(context) +
  condition_dependencies(context) +
  else_dependencies(context)
end
value(context = {}) click to toggle source
# File lib/dentaku/ast/case.rb, line 40
def value(context = {})
  switch_value = @switch.value(context)
  @conditions.each do |condition|
    if condition.when.value(context) == switch_value
      return condition.then.value(context)
    end
  end

  if @else
    return @else.value(context)
  else
    raise ArgumentError.for(:invalid_value), "No block matched the switch value '#{switch_value}'"
  end
end

Private Instance Methods

condition_dependencies(context = {}) click to toggle source
# File lib/dentaku/ast/case.rb, line 72
def condition_dependencies(context = {})
  @conditions.flat_map { |condition| condition.dependencies(context) }
end
else_dependencies(context = {}) click to toggle source
# File lib/dentaku/ast/case.rb, line 76
def else_dependencies(context = {})
  @else ? @else.dependencies(context) : []
end
switch_dependencies(context = {}) click to toggle source
# File lib/dentaku/ast/case.rb, line 68
def switch_dependencies(context = {})
  @switch.dependencies(context)
end