Class: Duby::AST::Node

Inherits:
Object show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, position, children = []) ⇒ Node

Returns a new instance of Node.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/duby/ast.rb', line 20

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

#childrenObject

Returns the value of attribute children.



14
15
16
# File 'lib/duby/ast.rb', line 14

def children
  @children
end

#inferred_typeObject

Returns the value of attribute inferred_type.



18
19
20
# File 'lib/duby/ast.rb', line 18

def inferred_type
  @inferred_type
end

#newlineObject

Returns the value of attribute newline.



17
18
19
# File 'lib/duby/ast.rb', line 17

def newline
  @newline
end

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'lib/duby/ast.rb', line 15

def parent
  @parent
end

#positionObject

Returns the value of attribute position.



16
17
18
# File 'lib/duby/ast.rb', line 16

def position
  @position
end

Instance Method Details

#[](index) ⇒ Object



70
# File 'lib/duby/ast.rb', line 70

def [](index) children[index] end

#each(&b) ⇒ Object



72
# File 'lib/duby/ast.rb', line 72

def each(&b) children.each(&b) end

#expr?(compiler) ⇒ Boolean

Returns:



26
27
28
# File 'lib/duby/jvm/source_generator/precompile.rb', line 26

def expr?(compiler)
  true
end

#inspect(indent = 0) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/duby/ast.rb', line 45

def inspect(indent = 0)
  indent_str = ' ' * indent
  str = indent_str + to_s
  children.each do |child|
    if child
      if ::Array === child
        child.each {|ary_child|
          str << "\n#{ary_child.inspect(indent + 1)}"
        }
      elsif ::Hash === child
        str << "\n#{indent_str} #{child.inspect}"
      else
        str << "\n#{child.inspect(indent + 1)}"
      end
    end
  end
  str
end

#line_numberObject



33
34
35
36
37
38
39
# File 'lib/duby/ast.rb', line 33

def line_number
  if @position
    @position.start_line + 1
  else
    0
  end
end

#log(message) ⇒ Object



41
42
43
# File 'lib/duby/ast.rb', line 41

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



81
82
83
84
85
86
87
# File 'lib/duby/ast.rb', line 81

def resolve_if(typer)
  unless resolved?
    @inferred_type = yield
    @inferred_type ? resolved! : typer.defer(self)
  end
  @inferred_type
end

#resolved!Object



74
75
76
77
# File 'lib/duby/ast.rb', line 74

def resolved!
  log "#{to_s} resolved!"
  @resolved = true
end

#resolved?Boolean

Returns:



79
# File 'lib/duby/ast.rb', line 79

def resolved?; @resolved end

#simple_nameObject



64
65
66
# File 'lib/duby/ast.rb', line 64

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_sObject



68
# File 'lib/duby/ast.rb', line 68

def to_s; simple_name; end