Class: Rubex::AST::Statement::Return

Inherits:
Base
  • Object
show all
Defined in:
lib/rubex/ast/statement/return.rb

Instance Attribute Summary

Attributes inherited from Base

#location

Instance Method Summary collapse

Methods inherited from Base

#==, #statement?

Constructor Details

#initialize(expression, location) ⇒ Return

Returns a new instance of Return.



5
6
7
8
# File 'lib/rubex/ast/statement/return.rb', line 5

def initialize(expression, location)
  super(location)
  @expression = expression
end

Instance Method Details

#analyse_statement(local_scope) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubex/ast/statement/return.rb', line 10

def analyse_statement(local_scope)
  unless @expression
    if local_scope.type.ruby_method?
      @expression = Rubex::AST::Expression::Literal::Nil.new 'Qnil'
    elsif local_scope.type.c_function?
      @expression = Rubex::AST::Expression::Empty.new
    end # FIXME: print a warning for type mismatch if none of above
  end

  @expression.analyse_types local_scope
  @expression.allocate_temps local_scope
  @expression.release_temps local_scope
  t = @expression.type

  @type =
    if t.c_function? || t.alias_type?
      t.type
    else
      t
    end

  @expression = @expression.to_ruby_object if local_scope.type.type.object?

  # TODO: Raise error if type as inferred from the
  # is not compatible with the return statement type.
end

#generate_code(code, local_scope) ⇒ Object



37
38
39
40
41
42
# File 'lib/rubex/ast/statement/return.rb', line 37

def generate_code(code, local_scope)
  super
  @expression.generate_evaluation_code code, local_scope
  code << "return #{@expression.c_code(local_scope)};"
  code.nl
end