Class: Duby::AST::Loop

Inherits:
Node
  • Object
show all
Defined in:
lib/duby/ast/flow.rb,
lib/duby/compiler.rb,
lib/duby/jvm/source_generator/precompile.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #inferred_type, #newline, #parent, #position

Instance Method Summary collapse

Methods inherited from Node

#<<, ===, #[], #_set_parent, child, child_name, #each, #empty?, #initialize_copy, #insert, #inspect, #line_number, #log, #resolve_if, #resolved!, #resolved?, #simple_name, #temp

Constructor Details

#initialize(parent, position, check_first, negative, &block) ⇒ Loop

Returns a new instance of Loop.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/duby/ast/flow.rb', line 107

def initialize(parent, position, check_first, negative, &block)
  @check_first = check_first
  @negative = negative

  @children = [
      Body.new(self, position),
      nil,
      Body.new(self, position),
      nil,
      Body.new(self, position),
  ]
  super(parent, position) do |l|
    condition, body = yield(l)
    [self.init, condition, self.pre, body, self.post]
  end
end

Instance Attribute Details

#check_firstObject

Returns the value of attribute check_first.



105
106
107
# File 'lib/duby/ast/flow.rb', line 105

def check_first
  @check_first
end

#negativeObject

Returns the value of attribute negative.



105
106
107
# File 'lib/duby/ast/flow.rb', line 105

def negative
  @negative
end

#redoObject

Returns the value of attribute redo.



105
106
107
# File 'lib/duby/ast/flow.rb', line 105

def redo
  @redo
end

Instance Method Details

#check_first?Boolean

Returns:



144
# File 'lib/duby/ast/flow.rb', line 144

def check_first?; @check_first; end

#compile(compiler, expression) ⇒ Object



212
213
214
215
# File 'lib/duby/compiler.rb', line 212

def compile(compiler, expression)
  compiler.line(line_number)
  compiler.loop(self, expression)
end

#expr?(compiler) ⇒ Boolean

Returns:



59
60
61
# File 'lib/duby/jvm/source_generator/precompile.rb', line 59

def expr?(compiler)
  false
end

#infer(typer) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/duby/ast/flow.rb', line 124

def infer(typer)
  unless resolved?
    child_types = children.map do |c|
      if c.nil? || (Body === c && c.empty?)
        typer.no_type
      else
        typer.infer(c)
      end
    end
    if child_types.any? {|t| t.nil?}
      typer.defer(self)
    else
      resolved!
      @inferred_type = typer.null_type
    end
  end

  @inferred_type
end

#init?Boolean

Returns:



167
168
169
# File 'lib/duby/ast/flow.rb', line 167

def init?
  init && !(init.kind_of?(Body) && init.empty?)
end

#negative?Boolean

Returns:



145
# File 'lib/duby/ast/flow.rb', line 145

def negative?; @negative; end

#post?Boolean

Returns:



175
176
177
# File 'lib/duby/ast/flow.rb', line 175

def post?
  post && !(post.kind_of?(Body) && post.empty?)
end

#pre?Boolean

Returns:



171
172
173
# File 'lib/duby/ast/flow.rb', line 171

def pre?
  pre && !(pre.kind_of?(Body) && pre.empty?)
end

#precompile(compiler) ⇒ Object



63
64
65
66
# File 'lib/duby/jvm/source_generator/precompile.rb', line 63

def precompile(compiler)
  compile(compiler, false)
  temp(compiler, 'null')
end

#redo?Boolean

Returns:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/duby/ast/flow.rb', line 147

def redo?
  if @redo.nil?
    nodes = @children.dup
    until nodes.empty?
      node = nodes.shift
      while node.respond_to?(:inlined) && node.inlined
        node = node.inlined
      end
      next if node.nil? || Loop === node
      if Redo === node
        return @redo = true
      end
      nodes.insert(-1, *node.children.flatten)
    end
    return @redo = false
  else
    @redo
  end
end

#to_sObject



179
180
181
# File 'lib/duby/ast/flow.rb', line 179

def to_s
  "Loop(check_first = #{check_first?}, negative = #{negative?})"
end