Class: Rewrite::CallSplattedByThunk

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

Overview

Initialize with a list of names, e.g.

CallSplattedByThunk.new(:foo, :bar)

It then converts expressions of the form:

foo(expr1, expr2, ..., exprn)

into:

foo.call( 
  CallSplattedByThunk::Parameters.new(
    lambda { expr1 }, lambda { expr2 }, ..., lambda { exprn }
  )
)

This is allows you to create call-by-name pseudo-functions that take an arbitrary number of arguments

Defined Under Namespace

Classes: Parameters

Instance Method Summary collapse

Constructor Details

#initialize(*functions_to_splat_thunkify) ⇒ CallSplattedByThunk

Returns a new instance of CallSplattedByThunk.



119
120
121
122
# File 'lib/rewrite/evaluation_strategies.rb', line 119

def initialize(*functions_to_splat_thunkify)
  @functions_to_splat_thunkify = functions_to_splat_thunkify
  super()
end

Instance Method Details

#process_fcall(exp) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rewrite/evaluation_strategies.rb', line 124

def process_fcall(exp)
  qua = exp.dup
  exp.shift
  name = exp.shift
  if @functions_to_splat_thunkify.include? name
    thunked = s(:call, s(:dvar, name), :call)
    unless exp.empty?
      arguments = exp.shift
      raise "Do not understand arguments #{arguments}" unless arguments[0] == :array
      thunked << s(:array,
        s(:call,
          s(:colon2, s(:colon2, s(:const, :Rewrite), :CallSplattedByThunk), :Parameters),
          :new,
          arguments[1..-1].inject(s(:array)) { |arr, arg| 
            arr << s(:iter, s(:fcall, :lambda), nil, process(arg)) 
          }
        )
      )
    end
  else
    thunked = s(:fcall, name) # :fcall, name_of_the_function
    thunked << process(exp.shift) unless exp.empty?
  end
  thunked
end