Class: Duby::AST::ForLoop

Inherits:
Loop show all
Defined in:
lib/duby/ast/flow.rb,
lib/duby/compiler.rb

Instance Attribute Summary collapse

Attributes inherited from Loop

#redo

Attributes inherited from Node

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

Instance Method Summary collapse

Methods inherited from Loop

#expr?, #precompile

Methods inherited from Node

#[], #each, #expr?, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

#initialize(parent, position, &block) ⇒ ForLoop

Returns a new instance of ForLoop.



135
136
137
138
139
140
141
# File 'lib/duby/ast/flow.rb', line 135

def initialize(parent, position, &block)
  super(parent, position, &block)
  var = children.shift
  @body, @iter = children
  @scope = var.scope
  @name = var.name
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



133
134
135
# File 'lib/duby/ast/flow.rb', line 133

def body
  @body
end

#iterObject (readonly)

Returns the value of attribute iter.



133
134
135
# File 'lib/duby/ast/flow.rb', line 133

def iter
  @iter
end

#nameObject (readonly)

Returns the value of attribute name.



133
134
135
# File 'lib/duby/ast/flow.rb', line 133

def name
  @name
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



160
161
162
163
# File 'lib/duby/compiler.rb', line 160

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

#infer(typer) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/duby/ast/flow.rb', line 143

def infer(typer)
  super
  iter_type = @iter.inferred_type
  if iter_type
    if !iter_type.iterable?
      raise "#{iter_type} is not iterable."
    end
    typer.learn_local_type(@scope, name, iter_type.component_type)
  end
  @inferred_type
end