Class: Yadriggy::ASTnode

Inherits:
Object
  • Object
show all
Defined in:
lib/yadriggy/ast.rb,
lib/yadriggy/syntax.rb,
lib/yadriggy/ast_value.rb,
lib/yadriggy/ast_location.rb

Overview

The common ancestor class of AST nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentASTnode

Returns the parent node.

Returns:



48
49
50
# File 'lib/yadriggy/ast.rb', line 48

def parent
  @parent
end

#usertypeSymbol|nil

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

Returns:

  • (Symbol|nil)

    the user type.



28
29
30
# File 'lib/yadriggy/syntax.rb', line 28

def usertype
  @usertype
end

Instance Method Details

#add_child(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (ASTnode)

    adds a child node.



57
58
59
# File 'lib/yadriggy/ast.rb', line 57

def add_child(node)
  node.parent = self unless node.nil?
end

#add_children(nodes) ⇒ void

This method returns an undefined value.

Parameters:

  • nodes (Array<ASTnode>)

    adds child nodes.



63
64
65
# File 'lib/yadriggy/ast.rb', line 63

def add_children(nodes)
  nodes.map {|e| e.parent = self unless e.nil? }
end

#const_valueObject

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



11
# File 'lib/yadriggy/ast_value.rb', line 11

def const_value() Undef end

#const_value_in_class(clazz) ⇒ Object

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, Undef.

Parameters:

  • clazz (Module)

    the context.



18
# File 'lib/yadriggy/ast_value.rb', line 18

def const_value_in_class(clazz) const_value end

#get_context_classModule

Gets the class including this AST node.

Returns:

  • (Module)

    the context.



35
36
37
38
39
40
41
42
# 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_objectObject

Gets the receiver object.

Returns:

  • (Object)

    the receiver object that this proc or method is bound to.



47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Booelan

Checks whether the object is a Proc or a method.

Parameters:

  • p (Object)

    the tested object.

Returns:

  • (Booelan)

    true if p is a Proc, Method, or lambda object.



61
62
63
# File 'lib/yadriggy/ast_value.rb', line 61

def is_proc?(p)
  p.is_a?(Proc) || p.is_a?(Method)
end

#pretty_print(pp) ⇒ Object

Overrides the printer printer.



51
52
53
# File 'lib/yadriggy/ast.rb', line 51

def pretty_print(pp)
  Yadriggy::simpler_pretty_print(pp, self, '@parent')
end

#rootASTnode

Returns the root node.

Returns:



68
69
70
# File 'lib/yadriggy/ast.rb', line 68

def root
  parent&.root || self
end

#source_locationArray

Returns a tuple of the file name, the line number, and the column of this AST node.

Returns:

  • (Array)

    a tuple of the file name, the line number, and the column of this AST node.



20
21
22
23
24
25
26
27
28
# 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_stringString

Returns the human-readable location of this AST node.

Returns:

  • (String)

    the human-readable location of this AST node.



13
14
15
16
# File 'lib/yadriggy/ast_location.rb', line 13

def source_location_string
  loc = source_location
  "#{loc[0]}:#{loc[1]}"
end

#valueObject

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



25
# File 'lib/yadriggy/ast_value.rb', line 25

def value() Undef end

#value_in_class(clazz) ⇒ Object

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

Parameters:

  • clazz (Module)

    the context.



31
# File 'lib/yadriggy/ast_value.rb', line 31

def value_in_class(clazz) value end