Class: Rubex::AST::Expression::Literal::StringLit

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

Instance Attribute Summary

Attributes inherited from Base

#entry, #type, #typecast

Instance Method Summary collapse

Methods inherited from Base

#==, #c_name, #initialize, #literal?

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from Rubex::AST::Expression::Literal::Base

Instance Method Details

#analyse_for_target_type(target_type, local_scope) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/rubex/ast/expression/literal/string_lit.rb', line 6

def analyse_for_target_type(target_type, local_scope)
  if target_type.char_ptr?
    @type = Rubex::DataType::CStr.new
  elsif target_type.object?
    @type = Rubex::DataType::RubyString.new
    analyse_types local_scope
  else
    raise Rubex::TypeError, "Cannot assign #{target_type} to string."
  end
end

#analyse_types(_local_scope) ⇒ Object



17
18
19
20
# File 'lib/rubex/ast/expression/literal/string_lit.rb', line 17

def analyse_types(_local_scope)
  @type ||= Rubex::DataType::RubyString.new
  @has_temp = true
end

#c_code(_local_scope) ⇒ Object



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

def c_code(_local_scope)
  @c_code
end

#generate_disposal_code(code) ⇒ Object



31
32
33
34
35
36
# File 'lib/rubex/ast/expression/literal/string_lit.rb', line 31

def generate_disposal_code(code)
  if @type.object?
    code << "#{@c_code} = 0;"
    code.nl
  end
end

#generate_evaluation_code(code, _local_scope) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rubex/ast/expression/literal/string_lit.rb', line 22

def generate_evaluation_code(code, _local_scope)
  if @type.cstr?
    @c_code = "\"#{@name}\""
  else
    code << "#{@c_code} = rb_str_new2(\"#{@name}\");"
    code.nl
  end
end