Class: Rubex::AST::Expression::Binary

Inherits:
Base
  • Object
show all
Includes:
Helpers::NodeTypeMethods
Defined in:
lib/rubex/ast/expression/binary.rb

Overview

Binary expression Base class.

Instance Attribute Summary

Attributes inherited from Base

#entry, #type, #typecast

Instance Method Summary collapse

Methods inherited from Base

#allocate_temp, #allocate_temps, #analyse_for_target_type, #expression?, #from_ruby_object, #generate_and_dispose_subexprs, #generate_assignment_code, #generate_disposal_code, #has_temp, #possible_typecast, #release_temp, #release_temps, #to_ruby_object

Constructor Details

#initialize(left, operator, right) ⇒ Binary

Returns a new instance of Binary.



7
8
9
10
# File 'lib/rubex/ast/expression/binary.rb', line 7

def initialize left, operator, right
  @left, @operator, @right = left, operator, right
  @subexprs = []
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
45
46
# File 'lib/rubex/ast/expression/binary.rb', line 42

def == other
  self.class == other.class && @type  == other.type &&
  @left == other.left  && @right == other.right &&
  @operator == other.operator
end

#analyse_types(local_scope) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubex/ast/expression/binary.rb', line 12

def analyse_types local_scope
  @left.analyse_types local_scope
  @right.analyse_types local_scope
  if type_of(@left).object? || type_of(@right).object?
    @left = @left.to_ruby_object
    @right = @right.to_ruby_object
    @has_temp = true
  end
  @type = Rubex::Helpers.result_type_for(type_of(@left), type_of(@right))
  @subexprs << @left
  @subexprs << @right
end

#c_code(local_scope) ⇒ Object



38
39
40
# File 'lib/rubex/ast/expression/binary.rb', line 38

def c_code local_scope
  super + @c_code
end

#generate_evaluation_code(code, local_scope) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubex/ast/expression/binary.rb', line 25

def generate_evaluation_code code, local_scope
  generate_and_dispose_subexprs(code, local_scope) do
    if @has_temp
      code << "#{@c_code} = rb_funcall(#{@left.c_code(local_scope)}," +
        "rb_intern(\"#{@operator}\")," +
        "1, #{@right.c_code(local_scope)});"
      code.nl
    else
      @c_code = "( #{@left.c_code(local_scope)} #{@operator} #{@right.c_code(local_scope)} )"
    end
  end
end