Class: Rewrite::RewriteParametersAsThunkCalls

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

Overview

Takes a sexp representing a lambda and rewrites all of its parameter references as thunk calls.

Example:

lambda { |a,b| a + b }
  => lambda { |a,b| a.call + b.call }

Instance Method Summary collapse

Instance Method Details

#process(sexp) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rewrite/evaluation_strategies.rb', line 26

def process(sexp)
  raise "Expected a proc" unless sexp[0] == :proc
  raise "Expected a proc with arity >= 1" if sexp[1] == nil
  arguments = sexp[1]
  variable_symbols = if arguments[0] == :dasgn || arguments[0] == :dasgn_curr
    [arguments[1]]
  elsif arguments[0] == :masgn
    arguments[1][1..-1].map { |pair| pair[1] }
  else
    raise "don't know how to extract paramater names from #{arguments}"
  end
  
  s(:iter,
    s(:fcall, :lambda),
    sexp(arguments),
    variable_symbols.inject(sexp[2]) { |result, variable|
      VariableRewriter.new(variable, s(:call, s(:dvar, variable), :call)).process(eval(result.inspect))
    }
  )
  
end

#sexp(exp) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rewrite/evaluation_strategies.rb', line 18

def sexp(exp)
  if exp.kind_of? Array
    s(*exp.map { |e| sexp(e) })
  else
    exp
  end
end