class Yadriggy::ASTnode

The common ancestor class of AST nodes.

Attributes

parent[RW]

@return [ASTnode] the parent node.

usertype[RW]

The user type (or non-terminal symbol) corresponding to this node. This is effective only after checking syntax by {Syntax#check}.

@return [Symbol|nil] the user type.

Public Instance Methods

add_child(node) click to toggle source

@param [ASTnode] node adds a child node. @return [void]

# File lib/yadriggy/ast.rb, line 57
def add_child(node)
  node.parent = self unless node.nil?
end
add_children(nodes) click to toggle source

@param [Array<ASTnode>] nodes adds child nodes. @return [void]

# File lib/yadriggy/ast.rb, line 63
def add_children(nodes)
  nodes.map {|e| e.parent = self unless e.nil? }
end
const_value() click to toggle source

The value of the name represented by this AST node if the value is immutable. Otherwise, {Yadriggy::Undef}.

# File lib/yadriggy/ast_value.rb, line 11
def const_value() Undef end
const_value_in_class(clazz) click to toggle source

The immutable value of the name represented by this AST node when it is evaluated in the context of `clazz`. If the value is mutable, {Yadriggy::Undef}.

@param [Module] clazz the context.

# File lib/yadriggy/ast_value.rb, line 18
def const_value_in_class(clazz) const_value end
get_context_class() click to toggle source

Gets the class including this AST node. @return [Module] the context.

# File lib/yadriggy/ast_value.rb, line 35
def get_context_class
  c = root.context
  if c.is_a?(Proc)
    c.binding.receiver.class
  else # c is Method or UnboundMethod
    c.owner
  end
end
get_receiver_object() click to toggle source

Gets the receiver object. @return [Object] the receiver object that this proc or method

is bound to.
# File lib/yadriggy/ast_value.rb, line 47
def get_receiver_object
  c = root.context
  if c.is_a?(Proc)
    c.binding.receiver
  elsif c.is_a?(Method)
    c.receiver
  else
    nil
  end
end
is_proc?(p) click to toggle source

Checks whether the object is a `Proc` or a method. @param [Object] p the tested object. @return [Booelan] true if `p` is a `Proc`, `Method`, or lambda object.

# File lib/yadriggy/ast_value.rb, line 61
def is_proc?(p)
  p.is_a?(Proc) || p.is_a?(Method)
end
pretty_print(pp) click to toggle source

Overrides the printer printer.

# File lib/yadriggy/ast.rb, line 51
def pretty_print(pp)
  Yadriggy::simpler_pretty_print(pp, self, '@parent')
end
root() click to toggle source

@return [ASTnode] the root node.

# File lib/yadriggy/ast.rb, line 68
def root
  parent&.root || self
end
source_location() click to toggle source

@return [Array] a tuple of the file name, the line number,

and the column of this AST node.
# File lib/yadriggy/ast_location.rb, line 20
def source_location
  g = GetLocation.new
  g.evaluate(self)
  if g.unknown? && !self.parent.nil?
    self.parent.source_location
  else
    g.result(root.file_name)
  end
end
source_location_string() click to toggle source

@return [String] the human-readable location of this AST node.

# File lib/yadriggy/ast_location.rb, line 13
def source_location_string
  loc = source_location
  "#{loc[0]}:#{loc[1]}"
end
value() click to toggle source

The runtime value of the variable, method name, etc. represented by this AST node. The default behavior returns {Yadriggy::Undef}. Subclasses override this method.

# File lib/yadriggy/ast_value.rb, line 25
def value() Undef end
value_in_class(clazz) click to toggle source

The runtime value of this AST node when it is evaluated in the context of `clazz`.

@param [Module] clazz the context.

# File lib/yadriggy/ast_value.rb, line 31
def value_in_class(clazz) value end