Class: Rubex::AST::Expression::Name

Inherits:
Base
  • Object
show all
Defined in:
lib/rubex/ast/expression/name.rb

Overview

Singular name node with no sub expressions.

Instance Attribute Summary

Attributes inherited from Base

#entry, #type, #typecast

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(name) ⇒ Name

Returns a new instance of Name.



6
7
8
# File 'lib/rubex/ast/expression/name.rb', line 6

def initialize(name)
  @name = name
end

Instance Method Details

#analyse_as_c_function(local_scope) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rubex/ast/expression/name.rb', line 60

def analyse_as_c_function(local_scope)
  @name = Rubex::AST::Expression::CFunctionCall.new(
    Expression::Self.new, @name,
    Expression::ActualArgList.new([])
  )
  @name.analyse_types(local_scope)
end

#analyse_declaration(_rhs, local_scope) ⇒ Object

Used when the node is a LHS of an assign statement.



11
12
13
14
15
16
17
18
# File 'lib/rubex/ast/expression/name.rb', line 11

def analyse_declaration(_rhs, local_scope)
  @entry = local_scope.find @name
  unless @entry
    local_scope.add_ruby_obj(name: @name, c_name: Rubex::VAR_PREFIX + @name, value: @rhs)
    @entry = local_scope[@name]
  end
  @type = @entry.type
end

#analyse_for_target_type(target_type, local_scope) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rubex/ast/expression/name.rb', line 20

def analyse_for_target_type(target_type, local_scope)
  @entry = local_scope.find @name

  if @entry&.type&.c_function? && target_type.c_function_ptr?
    @type = @entry.type
  else
    analyse_types local_scope
  end
end

#analyse_types(local_scope) ⇒ Object

Analyse a Name node. This can either be a variable name or a method call

without parenthesis. Code in this method that creates a RubyMethodCall
node primarily exists because in Ruby methods without arguments can
be called without parentheses. These names can potentially be Ruby
methods that are not visible to Rubex, but are present in the Ruby
run time. For example, a program like this:

  def foo
    bar
   #^^^ this is a name node
  end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubex/ast/expression/name.rb', line 41

def analyse_types(local_scope)
  @entry = local_scope.find @name
  unless @entry
    if ruby_constant?
      analyse_as_ruby_constant local_scope
    else
      add_as_ruby_method_to_scope local_scope
    end
  end
  assign_type_based_on_whether_wrapped_type
  analyse_as_ruby_method(local_scope) if @entry.type.ruby_method?
  analyse_as_c_function(local_scope) if @entry.type.c_function?
  if @name.is_a?(Expression::Base)
    @name.allocate_temps local_scope
    @name.release_temps local_scope
  end
  super
end

#c_code(local_scope) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/rubex/ast/expression/name.rb', line 86

def c_code(local_scope)          
  code = super
  code <<
    if @name.is_a?(Rubex::AST::Expression::Base)
      @name.c_code(local_scope)
    else
      @entry.c_name
    end
  code
end

#generate_assignment_code(rhs, code, local_scope) ⇒ Object



80
81
82
83
84
# File 'lib/rubex/ast/expression/name.rb', line 80

def generate_assignment_code(rhs, code, local_scope)
  code << "#{c_code(local_scope)} = #{rhs.c_code(local_scope)};"
  code.nl
  rhs.generate_disposal_code code
end

#generate_disposal_code(code) ⇒ Object



74
75
76
77
78
# File 'lib/rubex/ast/expression/name.rb', line 74

def generate_disposal_code(code)
  if @name.respond_to? :generate_disposal_code
    @name.generate_disposal_code code
  end
end

#generate_evaluation_code(code, local_scope) ⇒ Object



68
69
70
71
72
# File 'lib/rubex/ast/expression/name.rb', line 68

def generate_evaluation_code(code, local_scope)
  if @name.respond_to? :generate_evaluation_code
    @name.generate_evaluation_code code, local_scope
  end
end