{
print_values: %i[stringify],
concat: %i[stringify],
method_path_value: %i[kebab_to_snake],
set_method_path: %i[kebab_to_snake],
get_ivar: %i[kebab_to_snake],
set_ivar: %i[kebab_to_snake],
get_cvar: %i[current_class_scope kebab_to_snake],
set_cvar: %i[current_class_scope kebab_to_snake],
get_gvar: %i[kebab_to_snake],
set_gvar: %i[kebab_to_snake],
destructure: %i[destructure_into],
match_pattern: %i[match_pattern_into]
}.freeze
{
kebab_to_snake: <<~RUBY.chomp,
call: <<~'RUBY'.chomp,
send_call: <<~RUBY.chomp,
invoke_self: <<~RUBY.chomp,
stringify: <<~'RUBY'.chomp,
print_values: <<~'RUBY'.chomp,
concat: <<~RUBY.chomp,
get_path: <<~RUBY.chomp,
qget_path: <<~RUBY.chomp,
set_path: <<~RUBY.chomp,
method_path_value: <<~RUBY.chomp,
set_method_path: <<~'RUBY'.chomp,
current_class_scope: <<~RUBY.chomp,
get_ivar: <<~'RUBY'.chomp,
set_ivar: <<~'RUBY'.chomp,
get_cvar: <<~'RUBY'.chomp,
set_cvar: <<~'RUBY'.chomp,
get_gvar: <<~'RUBY'.chomp,
set_gvar: <<~'RUBY'.chomp,
ensure_module: <<~RUBY.chomp,
ensure_class: <<~RUBY.chomp,
destructure: <<~RUBY.chomp,
destructure_into: <<~'RUBY'.chomp,
match_pattern: <<~RUBY.chomp,
match_pattern_into: <<~'RUBY'.chomp
def __kap_match_pattern_into(pattern, value, bindings)
case pattern[0]
when :sym
name = pattern[1]
bindings[name] = value unless name == '_'
true
when :vec
return false unless value.is_a?(Array) || value.respond_to?(:to_ary)
array = value.is_a?(Array) ? value : value.to_ary
items = pattern[1]
rest_idx = items.index { |item| item.is_a?(Array) && item[0] == :rest }
if rest_idx
before = items[0...rest_idx]
rest_pattern = items[rest_idx][1]
return false if array.length < before.length
before.each_with_index do |item, i|
return false unless __kap_match_pattern_into(item, array[i], bindings)
end
__kap_match_pattern_into(rest_pattern, array[rest_idx..], bindings)
else
return false unless array.length == items.length
items.each_with_index do |item, i|
return false unless __kap_match_pattern_into(item, array[i], bindings)
end
true
end
when :hash
return false unless value.is_a?(Hash)
pattern[1].each do |key, subpattern|
return false unless value.key?(key)
return false unless __kap_match_pattern_into(subpattern, value[key], bindings)
end
true
when :lit
value == pattern[1]
when :nil
value.nil?
else
raise "bad pattern: #{pattern.inspect}"
end
end
RUBY
}.transform_values(&:freeze).freeze