Class: Duby::AST::Condition

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

Instance Attribute Summary

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?, #expr?, #initialize_copy, #insert, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

#initialize(parent, line_number, &block) ⇒ Condition

Returns a new instance of Condition.



6
7
8
# File 'lib/duby/ast/flow.rb', line 6

def initialize(parent, line_number, &block)
  super(parent, line_number, &block)
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



183
184
185
186
187
# File 'lib/duby/compiler.rb', line 183

def compile(compiler, expression)
  # TODO: can a condition ever be an expression? I don't think it can...
  compiler.line(line_number)
  predicate.compile(compiler, expression)
end

#infer(typer) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/duby/ast/flow.rb', line 10

def infer(typer)
  unless resolved?
    @inferred_type = typer.infer(predicate)
    if @inferred_type && !@inferred_type.primitive?
      call = Call.new(parent, position, '!=') do |call|
        predicate.parent = call
        [predicate, [Null.new(call, position)]]
      end
      self.predicate = call
      @inferred_type = typer.infer(predicate)
    end

    @inferred_type ? resolved! : typer.defer(self)
  end

  @inferred_type
end