Class: Riml::AST_Rewriter::ClassDefinitionToFunctions::InitializeSuperToObjectExtension

Inherits:
Riml::AST_Rewriter
  • Object
show all
Defined in:
lib/ast_rewriter.rb

Constant Summary

Constants included from Constants

Constants::BUILTIN_COMMANDS, Constants::BUILTIN_FUNCTIONS, Constants::COMPARISON_OPERATORS, Constants::DEFINE_KEYWORDS, Constants::END_KEYWORDS, Constants::IGNORECASE_CAPABLE_OPERATORS, Constants::KEYWORDS, Constants::REGISTERS, Constants::RIML_COMMANDS, Constants::RIML_END_KEYWORDS, Constants::RIML_KEYWORDS, Constants::SPECIAL_VARIABLE_PREFIXES, Constants::SPLAT_LITERAL, Constants::VIML_COMMANDS, Constants::VIML_END_KEYWORDS, Constants::VIML_KEYWORDS

Instance Attribute Summary collapse

Attributes inherited from Riml::AST_Rewriter

#ast, #classes, #rewritten_included_and_sourced_files

Instance Method Summary collapse

Methods inherited from Riml::AST_Rewriter

#add_SID_function!, #add_SID_function?, #do_establish_parents, #do_rewrite_on_match, #establish_parents, #rewrite, #rewrite_included_and_sourced_files!, #rewrite_on_match

Constructor Details

#initialize(constructor, classes, class_node) ⇒ InitializeSuperToObjectExtension

Returns a new instance of InitializeSuperToObjectExtension.



406
407
408
409
# File 'lib/ast_rewriter.rb', line 406

def initialize(constructor, classes, class_node)
  super(constructor, classes)
  @class_node = class_node
end

Instance Attribute Details

#class_nodeObject (readonly)

Returns the value of attribute class_node.



405
406
407
# File 'lib/ast_rewriter.rb', line 405

def class_node
  @class_node
end

Instance Method Details

#match?(constructor) ⇒ Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/ast_rewriter.rb', line 411

def match?(constructor)
  DefNode === constructor && constructor.super_node
end

#recursive?Boolean

Returns:

  • (Boolean)


456
457
458
# File 'lib/ast_rewriter.rb', line 456

def recursive?
  false
end

#replace(constructor) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/ast_rewriter.rb', line 415

def replace(constructor)
  unless class_node.superclass?
    # TODO: raise error instead of aborting
    abort "class #{class_node.full_name.inspect} called super in its " \
      " initialize function, but it has no superclass."
  end

  superclass = classes.superclass(class_node.full_name)
  super_constructor = superclass.constructor

  set_var_node = AssignNode.new('=', GetVariableNode.new(nil, superclass.constructor_obj_name),
    CallNode.new(
      super_constructor.scope_modifier,
      super_constructor.name,
      super_arguments(constructor.super_node)
    )
  )

  constructor.super_node.replace_with(set_var_node)
  constructor.expressions.insert_after(set_var_node,
    ExplicitCallNode.new(
      nil,
      "extend",
      [
        GetVariableNode.new(nil, class_node.constructor_obj_name),
        GetVariableNode.new(nil, superclass.constructor_obj_name)
      ]
    )
  )
  reestablish_parents(constructor)
end

#super_arguments(super_node) ⇒ Object



447
448
449
450
451
452
453
454
# File 'lib/ast_rewriter.rb', line 447

def super_arguments(super_node)
  if super_node.use_all_arguments?
    # here, ast is 'constructor'
    ast.parameters.map {|p| GetVariableNode.new(nil, p)}
  else
    super_node.arguments
  end
end