Class: Duby::AST::Condition

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

Instance Attribute Summary collapse

Attributes inherited from Node

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

Instance Method Summary collapse

Methods inherited from Node

#[], #each, #expr?, #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
9
# File 'lib/duby/ast/flow.rb', line 6

def initialize(parent, line_number, &block)
  super(parent, line_number, &block)
  @predicate = children[0]
end

Instance Attribute Details

#predicateObject

Returns the value of attribute predicate.



4
5
6
# File 'lib/duby/ast/flow.rb', line 4

def predicate
  @predicate
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



131
132
133
134
135
# File 'lib/duby/compiler.rb', line 131

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



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

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
      @predicate = children[0] = call
      @inferred_type = typer.infer(predicate)
    end

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

  @inferred_type
end