15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/destructure/sexp_transformer.rb', line 15
def transform(sp)
_ = DMatch::_
klass_sym = DMatch::Var.new(&method(:is_constant?))
case
when e = dmatch([:call, _, :_, _], sp); _
when e = dmatch([:const, klass_sym], sp)
make_obj(e[klass_sym], {})
when e = dmatch([:colon2, splat(:args)], sp)
make_obj(read_fq_const(sp), {})
when e = dmatch([:call, var(:identifier_sexp), :~, [:arglist]], sp); splat(unwind_receivers_and_clean(e[:identifier_sexp]))
when e = dmatch([:not, var(:value_sexp)], sp)
@caller_binding.eval(unwind_receivers_and_clean(e[:value_sexp]).to_s)
when e = dmatch([:call, var(:rest), :|, [:arglist, var(:alt)]], sp); DMatch::Or.new(*[e[:rest], e[:alt]].map(&method(:transform)))
when e = dmatch([:call, var(:receiver), var(:msg), var(:arglist)], sp)
transform_call(e[:receiver], e[:msg], e[:arglist])
when e = dmatch([:ivar, var(:name)], sp); var(e[:name].to_s)
when e = dmatch([DMatch::Or.new(:lasgn, :iasgn), var(:lhs), var(:rhs)], sp)
let_var(e[:lhs], transform(e[:rhs]))
when e = dmatch([:attrasgn, var(:obj), var(:attr), [:arglist, var(:rhs)]], sp)
var_name = unwind_receivers_and_clean([:call, e[:obj], e[:attr].to_s.sub(/=$/,'').to_sym, [:arglist]])
let_var(var_name, transform(e[:rhs]))
when e = dmatch([:lit, var(:value)], sp); e[:value]
when e = dmatch([:true], sp); true
when e = dmatch([:false], sp); false
when e = dmatch([:nil], sp); nil
when e = dmatch([:str, var(:s)], sp); e[:s]
when e = dmatch([:array, splat(:items)], sp); e[:items].map(&method(:transform))
when e = dmatch([:hash, splat(:kvs)], sp); Hash[*e[:kvs].map(&method(:transform))]
else; raise "Unexpected sexp: #{sp.inspect}"
end
end
|