Class: Riml::DefNode

Inherits:
Struct
  • Object
show all
Includes:
FullyNameable, Indentable, Visitable, Walkable
Defined in:
lib/nodes.rb

Overview

Method definition.

Direct Known Subclasses

DefMethodNode

Constant Summary collapse

SPLAT =
lambda {|arg| arg == Riml::Constants::SPLAT_LITERAL || arg[0] == "*"}
DEFAULT_PARAMS =
lambda {|p| DefaultParamNode === p}

Constants included from Visitable

Visitable::DESCENDANT_OF_REGEX

Instance Attribute Summary collapse

Attributes included from Visitable

#compiled_output, #force_newline, #parent_node, #scope

Instance Method Summary collapse

Methods included from Walkable

#child_after, #child_previous_to, #each, #index_by_children, #index_by_member, #insert_after, #insert_before, #next, #previous, #remove, #replace_with

Methods included from FullyNameable

#full_name, included

Methods included from Indentable

#indent, #indented?, #outdent

Methods included from Visitable

#accept, #respond_to?

Constructor Details

#initialize(*args) ⇒ DefNode

Returns a new instance of DefNode.



606
607
608
609
610
611
612
613
614
615
# File 'lib/nodes.rb', line 606

def initialize(*args)
  super
  # max number of arguments in viml
  if parameters.reject(&DEFAULT_PARAMS).size > 20
    raise Riml::UserArgumentError, "can't have more than 20 parameters for #{full_name}"
  end
  expressions.nodes.select { |node| DefNode === node}.each do |nested_func|
    nested_func.nested_within.unshift(self)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



693
694
695
696
697
698
699
# File 'lib/nodes.rb', line 693

def method_missing(method, *args, &blk)
  if children.respond_to?(method)
    children.send(method, *args, &blk)
  else
    super
  end
end

Instance Attribute Details

#bangObject

Returns the value of attribute bang

Returns:

  • (Object)

    the current value of bang



598
599
600
# File 'lib/nodes.rb', line 598

def bang
  @bang
end

#expressionsObject

Returns the value of attribute expressions

Returns:

  • (Object)

    the current value of expressions



598
599
600
# File 'lib/nodes.rb', line 598

def expressions
  @expressions
end

#keywordsObject

Returns the value of attribute keywords

Returns:

  • (Object)

    the current value of keywords



598
599
600
# File 'lib/nodes.rb', line 598

def keywords
  @keywords
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



598
599
600
# File 'lib/nodes.rb', line 598

def name
  @name
end

#original_nameObject



620
621
622
# File 'lib/nodes.rb', line 620

def original_name
  @original_name ||= name
end

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



598
599
600
# File 'lib/nodes.rb', line 598

def parameters
  @parameters
end

#private_functionObject

Returns the value of attribute private_function.



604
605
606
# File 'lib/nodes.rb', line 604

def private_function
  @private_function
end

#scope_modifierObject

Returns the value of attribute scope_modifier

Returns:

  • (Object)

    the current value of scope_modifier



598
599
600
# File 'lib/nodes.rb', line 598

def scope_modifier
  @scope_modifier
end

#sidObject Also known as: sid?

Returns the value of attribute sid

Returns:

  • (Object)

    the current value of sid



598
599
600
# File 'lib/nodes.rb', line 598

def sid
  @sid
end

Instance Method Details

#argument_variable_namesObject

[“arg1”, “arg2”}



626
627
628
# File 'lib/nodes.rb', line 626

def argument_variable_names
  parameters.reject(&SPLAT)
end

#autoload?Boolean

Returns:

  • (Boolean)


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

def autoload?
  name.include?('#')
end

#childrenObject



684
685
686
687
688
689
690
691
# File 'lib/nodes.rb', line 684

def children
  children = if sid?
    [sid, expressions]
  else
    [expressions]
  end
  children.concat(default_param_nodes)
end

#default_param_nodesObject



680
681
682
# File 'lib/nodes.rb', line 680

def default_param_nodes
  parameters.select(&DEFAULT_PARAMS)
end

#defined_on_dictionary?Boolean

Returns:

  • (Boolean)


659
660
661
# File 'lib/nodes.rb', line 659

def defined_on_dictionary?
  keywords.include?('dict')
end

#nested_function?Boolean

Returns:

  • (Boolean)


642
643
644
# File 'lib/nodes.rb', line 642

def nested_function?
  not nested_within.empty?
end

#nested_withinObject



638
639
640
# File 'lib/nodes.rb', line 638

def nested_within
  @nested_within ||= []
end

#shadowed_argument?(var_name) ⇒ Boolean

Returns:

  • (Boolean)


630
631
632
# File 'lib/nodes.rb', line 630

def shadowed_argument?(var_name)
  shadowed_argument_variable_names.include?(var_name)
end

#shadowed_argument_variable_namesObject



634
635
636
# File 'lib/nodes.rb', line 634

def shadowed_argument_variable_names
  @shadowed_argument_variable_names ||= Set.new
end

#splatObject

returns the splat argument or nil



647
648
649
# File 'lib/nodes.rb', line 647

def splat
  parameters.detect(&SPLAT)
end

#super_nodeObject



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

def super_node
  expressions.nodes.detect {|n| SuperNode === n}
end

#to_scopeObject



673
674
675
676
677
678
# File 'lib/nodes.rb', line 673

def to_scope
  ScopeNode.new.tap do |scope|
    scope.argument_variable_names += argument_variable_names
    scope.function = self
  end
end