Class: Rubex::AST::Expression::Literal::HashLit

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

Instance Attribute Summary

Attributes inherited from Base

#entry, #type, #typecast

Instance Method Summary collapse

Methods inherited from Base

#==, #c_name, #literal?

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(key_val_pairs) ⇒ HashLit

Returns a new instance of HashLit.



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

def initialize(key_val_pairs)
  @key_val_pairs = key_val_pairs
end

Instance Method Details

#analyse_types(local_scope) ⇒ Object



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

def analyse_types(local_scope)
  @has_temp = true
  @type = Rubex::DataType::RubyObject.new
  @key_val_pairs.map! do |k, v|
    k.analyse_for_target_type(@type, local_scope)
    v.analyse_for_target_type(@type, local_scope)
    [k.to_ruby_object, v.to_ruby_object]
  end
  @subexprs = @key_val_pairs.to_a.flatten
end

#c_code(_local_scope) ⇒ Object



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

def c_code(_local_scope)
  @c_code
end

#generate_evaluation_code(code, local_scope) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubex/ast/expression/literal/hash_lit.rb', line 21

def generate_evaluation_code(code, local_scope)
  code << "#{@c_code} = rb_hash_new();"
  code.nl
  @key_val_pairs.each do |k, v|
    k.generate_evaluation_code(code, local_scope)
    v.generate_evaluation_code(code, local_scope)

    code << "rb_hash_aset(#{@c_code}, #{k.c_code(local_scope)}, "
    code << "#{v.c_code(local_scope)});"
    code.nl

    k.generate_disposal_code code
    v.generate_disposal_code code
    code.nl
  end
end