Class: Duby::AST::Node
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/duby/ast.rb,
lib/duby/jvm/source_generator/precompile.rb
Overview
The top of the AST class hierarchy, this represents an abstract AST node. It provides accessors for children, an array of all child nodes, parent, a reference to this node’s parent (nil if none), and newline, whether this node represents a new line.
Direct Known Subclasses
Annotation, Argument, Arguments, Array, BindingReference, Block, Body, Boolean, Break, Call, ClassDefinition, Colon2, Condition, Constant, EmptyArray, Ensure, ErrorNode, Field, FieldAssignment, FieldDeclaration, Fixnum, Float, FunctionalCall, Hash, If, Import, Local, LocalAssignment, LocalDeclaration, Loop, MacroDefinition, MethodDefinition, NodeProxy, Noop, Not, Null, Raise, Regexp, Rescue, RescueClause, Return, Script, Self, String, StringConcat, Super, Symbol, ToString, TypeReference, VoidType
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(parent, position, children = []) ⇒ Node
Returns a new instance of Node.
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/duby/ast.rb', line 42
def initialize(parent, position, children = [])
@parent = parent
@newline = false
@inferred_type = nil
@resolved = false
@position = position
if block_given?
@children = yield(self) || []
else
@children = children
end
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
17
18
19
|
# File 'lib/duby/ast.rb', line 17
def children
@children
end
|
#inferred_type ⇒ Object
Returns the value of attribute inferred_type.
21
22
23
|
# File 'lib/duby/ast.rb', line 21
def inferred_type
@inferred_type
end
|
#newline ⇒ Object
Returns the value of attribute newline.
20
21
22
|
# File 'lib/duby/ast.rb', line 20
def newline
@newline
end
|
#parent ⇒ Object
Returns the value of attribute parent.
18
19
20
|
# File 'lib/duby/ast.rb', line 18
def parent
@parent
end
|
#position ⇒ Object
Returns the value of attribute position.
19
20
21
|
# File 'lib/duby/ast.rb', line 19
def position
@position
end
|
Class Method Details
.===(other) ⇒ Object
137
138
139
|
# File 'lib/duby/ast.rb', line 137
def self.===(other)
super || (other.kind_of?(NodeProxy) && (self === other.__getobj__))
end
|
.child(name) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/duby/ast.rb', line 23
def self.child(name)
@children ||= []
index = @children.size
class_eval <<-EOF
def #{name}
@children[#{index}]
end
def #{name}=(node)
@children[#{index}] = _set_parent(node)
end
EOF
@children << name
end
|
.child_name(i) ⇒ Object
38
39
40
|
# File 'lib/duby/ast.rb', line 38
def self.child_name(i)
@children[i] if @children
end
|
Instance Method Details
#<<(node) ⇒ Object
108
109
110
111
|
# File 'lib/duby/ast.rb', line 108
def <<(node)
@children << _set_parent(node)
self
end
|
#[](index) ⇒ Object
104
|
# File 'lib/duby/ast.rb', line 104
def [](index) children[index] end
|
#_set_parent(node) ⇒ Object
141
142
143
144
145
146
147
148
149
|
# File 'lib/duby/ast.rb', line 141
def _set_parent(node)
case node
when Node
node.parent = self
when ::Array
node.each {|x| x.parent = self if x}
end
node
end
|
#each(&b) ⇒ Object
106
|
# File 'lib/duby/ast.rb', line 106
def each(&b) children.each(&b) end
|
118
119
120
|
# File 'lib/duby/ast.rb', line 118
def empty?
@children.empty?
end
|
#expr?(compiler) ⇒ Boolean
26
27
28
|
# File 'lib/duby/jvm/source_generator/precompile.rb', line 26
def expr?(compiler)
true
end
|
#initialize_copy(other) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/duby/ast.rb', line 151
def initialize_copy(other)
@parent = nil
@children = []
other.children.each do |child|
case child
when ::Array
self << child.map {|x| x.dup}
when nil
self << nil
else
self << child.dup
end
end
end
|
#insert(index, node) ⇒ Object
113
114
115
116
|
# File 'lib/duby/ast.rb', line 113
def insert(index, node)
node.parent = self
@children.insert(index, node)
end
|
#inspect(indent = 0) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/duby/ast.rb', line 67
def inspect(indent = 0)
indent_str = ' ' * indent
str = indent_str + to_s
children.each_with_index do |child, i|
= 0
if child
name = self.class.child_name(i)
if Duby::AST.verbose && name
str << "\n#{indent_str} #{name}:"
= 1
end
if ::Array === child
child.each {|ary_child|
if Duby::AST.verbose && Node === ary_child && ary_child.parent != self
str << "\n#{indent_str} (wrong parent)"
end
str << "\n#{ary_child.inspect(indent + + 1)}"
}
elsif ::Hash === child
str << "\n#{indent_str} #{child.inspect}"
else
if Duby::AST.verbose && Node === child && child.parent != self
str << "\n#{indent_str} (wrong parent)"
end
str << "\n#{child.inspect(indent + + 1)}"
end
end
end
str
end
|
#line_number ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/duby/ast.rb', line 55
def line_number
if @position
@position.start_line + 1
else
0
end
end
|
#log(message) ⇒ Object
63
64
65
|
# File 'lib/duby/ast.rb', line 63
def log(message)
puts "* [AST] [#{simple_name}] " + message if AST.verbose
end
|
#precompile(compiler) ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/duby/jvm/source_generator/precompile.rb', line 30
def precompile(compiler)
if expr?(compiler)
self
else
temp(compiler)
end
end
|
#resolve_if(typer) ⇒ Object
129
130
131
132
133
134
135
|
# File 'lib/duby/ast.rb', line 129
def resolve_if(typer)
unless resolved?
@inferred_type = yield
@inferred_type ? resolved!(typer) : typer.defer(self)
end
@inferred_type
end
|
#resolved!(typer = nil) ⇒ Object
122
123
124
125
|
# File 'lib/duby/ast.rb', line 122
def resolved!(typer=nil)
log "#{to_s} resolved!"
@resolved = true
end
|
127
|
# File 'lib/duby/ast.rb', line 127
def resolved?; @resolved end
|
#simple_name ⇒ Object
98
99
100
|
# File 'lib/duby/ast.rb', line 98
def simple_name
self.class.name.split("::")[-1]
end
|
#temp(compiler, value = nil) ⇒ Object
38
39
40
|
# File 'lib/duby/jvm/source_generator/precompile.rb', line 38
def temp(compiler, value=nil)
TempValue.new(self, compiler, value)
end
|
#to_s ⇒ Object
102
|
# File 'lib/duby/ast.rb', line 102
def to_s; simple_name; end
|