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.

Defined Under Namespace

Classes: GetLocation

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentASTnode



46
47
48
# File 'lib/yadriggy/ast.rb', line 46

def parent
  @parent
end

#usertypeSymbol|nil

The user type (or non-terminal symbol) corresponding to this node.



26
27
28
# File 'lib/yadriggy/syntax.rb', line 26

def usertype
  @usertype
end

Instance Method Details

#add_child(node) ⇒ void



55
56
57
# File 'lib/yadriggy/ast.rb', line 55

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

#add_children(nodes) ⇒ void



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

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.



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.



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.



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.



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.



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

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

#rootASTnode



66
67
68
# File 'lib/yadriggy/ast.rb', line 66

def root
  parent&.root || self
end

#source_locationArray



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



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`.



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

def value_in_class(clazz) value end