Class: Mirah::AST::Loop

Inherits:
Node
  • Object
show all
Defined in:
lib/mirah/ast/flow.rb,
lib/mirah/compiler/flow.rb,
lib/mirah/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

#<<, ===, #[], #[]=, #_dump, _load, #_set_parent, child, child_name, #child_nodes, #each, #empty?, #inferred_type!, #initialize_copy, #insert, #inspect, #inspect_children, #line_number, #log, #resolve_if, #resolved!, #resolved?, #simple_name, #string_value, #temp, #top_level?, #validate_child, #validate_children

Constructor Details

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

Returns a new instance of Loop.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mirah/ast/flow.rb', line 103

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.



101
102
103
# File 'lib/mirah/ast/flow.rb', line 101

def check_first
  @check_first
end

#negativeObject

Returns the value of attribute negative.



101
102
103
# File 'lib/mirah/ast/flow.rb', line 101

def negative
  @negative
end

#redoObject

Returns the value of attribute redo.



101
102
103
# File 'lib/mirah/ast/flow.rb', line 101

def redo
  @redo
end

Instance Method Details

#check_first?Boolean

Returns:



140
# File 'lib/mirah/ast/flow.rb', line 140

def check_first?; @check_first; end

#compile(compiler, expression) ⇒ Object



38
39
40
41
42
43
# File 'lib/mirah/compiler/flow.rb', line 38

def compile(compiler, expression)
  compiler.line(line_number)
  compiler.loop(self, expression)
rescue Exception => ex
  raise Mirah::InternalCompilerError.wrap(ex, self)
end

#expr?(compiler) ⇒ Boolean

Returns:



74
75
76
# File 'lib/mirah/jvm/source_generator/precompile.rb', line 74

def expr?(compiler)
  false
end

#infer(typer, expression) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mirah/ast/flow.rb', line 120

def infer(typer, expression)
  unless resolved?
    child_types = children.map do |c|
      if c.nil? || (Body === c && c.empty?)
        typer.no_type
      else
        typer.infer(c, true)
      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:



163
164
165
# File 'lib/mirah/ast/flow.rb', line 163

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

#negative?Boolean

Returns:



141
# File 'lib/mirah/ast/flow.rb', line 141

def negative?; @negative; end

#post?Boolean

Returns:



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

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

#pre?Boolean

Returns:



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

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

#precompile(compiler) ⇒ Object



78
79
80
81
# File 'lib/mirah/jvm/source_generator/precompile.rb', line 78

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

#redo?Boolean

Returns:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mirah/ast/flow.rb', line 143

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



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

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