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

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

Constant Summary

Constants included from Constants

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

Instance Attribute Summary collapse

Attributes inherited from Riml::AST_Rewriter

#ast, #classes, #options

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, #reorder_includes_based_on_class_dependencies!, #resolve_class_dependencies!, #resolve_class_dependencies?, #rewrite, #rewrite_included_and_sourced_files!, #rewrite_on_match, #watch_for_class_pickup

Constructor Details

#initialize(constructor, classes, class_node) ⇒ InitializeSuperToObjectExtension

Returns a new instance of InitializeSuperToObjectExtension.



664
665
666
667
# File 'lib/riml/ast_rewriter.rb', line 664

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.



663
664
665
# File 'lib/riml/ast_rewriter.rb', line 663

def class_node
  @class_node
end

Instance Method Details

#match?(constructor) ⇒ Boolean

Returns:

  • (Boolean)


669
670
671
# File 'lib/riml/ast_rewriter.rb', line 669

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

#max_recursion_lvlObject



715
716
717
# File 'lib/riml/ast_rewriter.rb', line 715

def max_recursion_lvl
  1
end

#replace(constructor) ⇒ Object



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/riml/ast_rewriter.rb', line 673

def replace(constructor)
  unless class_node.superclass?
    error_msg "class #{class_node.full_name.inspect} called super in its " \
      " initialize function, but it has no superclass."
    error = InvalidSuper.new(error_msg, constructor)
    raise error
  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



706
707
708
709
710
711
712
713
# File 'lib/riml/ast_rewriter.rb', line 706

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