Class: Rewrite::VariableRewriter

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/rewrite/variables.rb

Overview

TODO: Implement splat variables somehow

def test_splat_variables

assert_equal(
  lambda { |*a| a[0].call }.to_sexp.to_a,
  Rewrite::RewriteVariablesAsThunkCalls.new(:a).process(
    lambda { |*a| a[0] }.to_sexp
  ).to_a
)

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, replacement_sexp) ⇒ VariableRewriter

Returns a new instance of VariableRewriter.



29
30
31
32
# File 'lib/rewrite/variables.rb', line 29

def initialize symbol, replacement_sexp
  @symbol, @replacement_sexp = symbol, replacement_sexp
  super()
end

Instance Attribute Details

#replacement_sexpObject (readonly)

Returns the value of attribute replacement_sexp.



27
28
29
# File 'lib/rewrite/variables.rb', line 27

def replacement_sexp
  @replacement_sexp
end

Instance Method Details

#process_dvar(exp) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rewrite/variables.rb', line 40

def process_dvar(exp)
  exp.shift
  variable = exp.shift
  if @symbol == variable
    replacement_sexp # not a deep copy. problem? replace with a block call??
  else
    s(:dvar, variable)
  end
end

#process_iter(exp) ⇒ Object

don’t handle subtrees where we redefine the variable, including lambda and proc



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rewrite/variables.rb', line 50

def process_iter(exp) # don't handle subtrees where we redefine the variable, including lambda and proc
  original = exp.to_a.dup
  exp.shift
  callee_expr = exp.shift # we process in our context
  params_exp = exp.shift
  block_body = exp.shift
  params = if params_exp.nil?
    []
  elsif params_exp.first == :dasgn || params_exp.first == :dasgn_curr
    [ params_exp[1] ]
  elsif params_exp.first == :masgn
    raise "Can't handle  #{original.inspect}" unless params_exp[1].first == :array
    params_exp[1][1..-1].map { |assignment| assignment[1] }
  else
    raise "Can't handle #{original.inspect}"
  end
  if params.include? @symbol
    s(:iter, subprocess(callee_expr), params_exp, block_body) # we're DONE
  else
    s(:iter, subprocess(callee_expr), params_exp, subprocess(block_body)) # we're still in play
  end
end

#subprocess(something) ⇒ Object

missing: rewriting assignment. Hmmm.



36
37
38
# File 'lib/rewrite/variables.rb', line 36

def subprocess (something)
  process(something) if something
end