class Opal::Rewriters::PatternMatching::PatternConverter

def array(*args)

def array(*args)
  to_ast(args)
end

def initialize(pat)

def initialize(pat)
  @pat = pat
  @variables = []
end

def on_array_pattern(node, tail = false)

[0, 1, 2] or [*, 0, 1] or [0, 1, *]
def on_array_pattern(node, tail = false)
  children = *node
  children << s(:match_rest) if tail
  fixed_size = true
  array_size = 0
  children = children.each do |i|
    case i.type
    when :match_rest
      fixed_size = false
    else
      array_size += 1
    end
  end
  array(
    s(:sym, :array),
    to_ast(fixed_size),
    to_ast(array_size),
    to_ast(children.map(&method(:process)))
  )
end

def on_array_pattern_with_tail(node)

[0, 1, 2,]
def on_array_pattern_with_tail(node)
  on_array_pattern(node, true)
end

def on_const_pattern(node)

MyStructName
def on_const_pattern(node)
  array(s(:sym, :all), *node.children.map(&method(:process)))
end

def on_find_pattern(node)

[*, a, b, *]
def on_find_pattern(node)
  children = *node
  children = children.map(&method(:process))
  array(s(:sym, :find), array(*children))
end

def on_hash_pattern(node)

{a:, b:}
def on_hash_pattern(node)
  children = *node
  any_size = children.empty? ? to_ast(false) : to_ast(true)
  children = children.map do |i|
    case i.type
    when :pair
      array(i.children[0], process(i.children[1]))
    when :match_var
      array(s(:sym, i.children[0]), process(i))
    when :match_nil_pattern
      any_size = to_ast(false)
      nil
    when :match_rest
      # Capturing rest?
      if i.children.first
        any_size = process(i.children.first)
      else
        any_size = to_ast(true)
      end
      nil
    end
  end.compact
  array(s(:sym, :hash), any_size, array(*children))
end

def on_literal(node)

def on_literal(node)
  array(s(:sym, :lit), node)
end

def on_match_alt(node)

{} | []
def on_match_alt(node)
  array(s(:sym, :any), *node.children.map(&method(:process)))
end

def on_match_as(node)

[...] => a
def on_match_as(node)
  pat, save = *node
  process(save)
  array(s(:sym, :save), process(pat))
end

def on_match_rest(node)

*
def on_match_rest(node)
  if node.children.empty?
    array(s(:sym, :rest))
  else
    array(s(:sym, :rest), process(node.children.first))
  end
end

def on_match_var(node)

a
def on_match_var(node)
  var, = *node
  @variables << var
  s(:sym, :var)
end

def on_pin(node)

^a
def on_pin(node)
  on_literal(node.children.first)
end

def pattern

def pattern
  @outpat
end

def run!

def run!
  @outpat = process(@pat)
end

def to_ast(val)

def to_ast(val)
  case val
  when Array
    s(:array, *val)
  when Integer
    s(:int, val)
  when true
    s(:true)
  when false
    s(:false)
  when nil
    s(:nil)
  end
end

def variables

def variables
  @variables.map { |i| s(:lvasgn, i) }
end