Class: Riml::DefNode

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

Overview

Method definition.

Direct Known Subclasses

DefMethodNode

Constant Summary collapse

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

Constants included from Visitable

Visitable::EMPTY_CHILDREN

Instance Attribute Summary collapse

Attributes included from Visitable

#compiled_output, #force_newline, #parent_node, #parser_info, #scope

Instance Method Summary collapse

Methods included from FullyNameable

#full_name, included

Methods included from Indentable

#indent, #indented?, #outdent

Methods included from Visitable

#accept, #force_newline_if_child_call_node?, #location_info

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

Constructor Details

#initialize(*args) ⇒ DefNode

Returns a new instance of DefNode.



611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/riml/nodes.rb', line 611

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

Instance Attribute Details

#bangObject

Returns the value of attribute bang

Returns:

  • (Object)

    the current value of bang



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

def bang
  @bang
end

#expressionsObject

Returns the value of attribute expressions

Returns:

  • (Object)

    the current value of expressions



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

def expressions
  @expressions
end

#keywordsObject

Returns the value of attribute keywords

Returns:

  • (Object)

    the current value of keywords



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

def keywords
  @keywords
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

#original_nameObject



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

def original_name
  @original_name ||= name
end

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



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

def parameters
  @parameters
end

#private_functionObject Also known as: private_function?

Returns the value of attribute private_function.



608
609
610
# File 'lib/riml/nodes.rb', line 608

def private_function
  @private_function
end

#scope_modifierObject

Returns the value of attribute scope_modifier

Returns:

  • (Object)

    the current value of scope_modifier



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

def scope_modifier
  @scope_modifier
end

#sidObject Also known as: sid?

Returns the value of attribute sid

Returns:

  • (Object)

    the current value of sid



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

def sid
  @sid
end

Instance Method Details

#argument_variable_namesObject

[“arg1”, “arg2”}



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

def argument_variable_names
  parameters.reject(&SPLAT)
end

#autoload?Boolean

Returns:

  • (Boolean)


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

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

#childrenObject



698
699
700
701
702
703
704
705
# File 'lib/riml/nodes.rb', line 698

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

#default_param_nodesObject



688
689
690
# File 'lib/riml/nodes.rb', line 688

def default_param_nodes
  parameters.select(&DEFAULT_PARAMS)
end

#defined_on_dictionary?Boolean

Returns:

  • (Boolean)


666
667
668
# File 'lib/riml/nodes.rb', line 666

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

#is_splat_arg?(node) ⇒ Boolean

Returns:

  • (Boolean)


692
693
694
695
696
# File 'lib/riml/nodes.rb', line 692

def is_splat_arg?(node)
  splat = splat()
  return false unless splat
  GetVariableNode === node && node.scope_modifier.nil? && node.name == splat[1..-1]
end

#nested_function?Boolean

Returns:

  • (Boolean)


649
650
651
# File 'lib/riml/nodes.rb', line 649

def nested_function?
  not nested_within.empty?
end

#nested_withinObject



645
646
647
# File 'lib/riml/nodes.rb', line 645

def nested_within
  @nested_within ||= []
end

#shadowed_argument?(var_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#shadowed_argument_variable_namesObject



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

def shadowed_argument_variable_names
  @shadowed_argument_variable_names ||= Set.new
end

#splatObject

returns the splat argument or nil



654
655
656
# File 'lib/riml/nodes.rb', line 654

def splat
  parameters.detect(&SPLAT)
end

#super_nodeObject

FIXME: only detects top-level super nodes



677
678
679
# File 'lib/riml/nodes.rb', line 677

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

#to_scopeObject



681
682
683
684
685
686
# File 'lib/riml/nodes.rb', line 681

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