Class: CodeTools::AST::DefineFunction

Inherits:
ClosedScope show all
Defined in:
lib/rubinius/code/ast/definitions.rb

Instance Attribute Summary collapse

Attributes inherited from ClosedScope

#body

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from ClosedScope

#assign_local_reference, #attach_and_call, #module?, #nest_scope, #new_local, #new_nested_local, #search_local, #to_sexp

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #to_sexp, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, name, block) ⇒ DefineFunction

Returns a new instance of DefineFunction.



191
192
193
194
195
196
197
# File 'lib/rubinius/code/ast/definitions.rb', line 191

def initialize(line, name, block)
  @line = line
  @name = name
  @arguments = block.extract_parameters
  block.array << NilLiteral.new(line) if block.array.empty?
  @body = block
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



189
190
191
# File 'lib/rubinius/code/ast/definitions.rb', line 189

def arguments
  @arguments
end

#nameObject

Returns the value of attribute name.



189
190
191
# File 'lib/rubinius/code/ast/definitions.rb', line 189

def name
  @name
end

Instance Method Details

#bytecode(g) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/rubinius/code/ast/definitions.rb', line 223

def bytecode(g)
  pos(g)

  g.push_rubinius
  g.push_literal @name
  g.push_generator compile_body(g)
  g.push_scope

  g.send :add_function, 3
end

#compile_body(g) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rubinius/code/ast/definitions.rb', line 199

def compile_body(g)
  function = new_generator(g, @name, @arguments)

  function.push_state self
  function.state.push_super self
  function.definition_line(@line)

  function.state.push_name @name

  @arguments.bytecode(function)
  @body.bytecode(function)

  function.state.pop_name

  function.local_count = local_count
  function.local_names = local_names

  function.ret
  function.close
  function.pop_state

  return function
end

#sexp_nameObject



234
235
236
# File 'lib/rubinius/code/ast/definitions.rb', line 234

def sexp_name
  [:fun, @name, @arguments.to_sexp, [:scope, @body.to_sexp]]
end