Module: Rubex::Helpers::Writers

Included in:
AST::Node::Base, AST::TopStatement::Klass, AST::TopStatement::MethodDef
Defined in:
lib/rubex/helpers/writers.rb

Instance Method Summary collapse

Instance Method Details

#declare_carrays(code, scope) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubex/helpers/writers.rb', line 20

def declare_carrays(code, scope)
  scope.carray_entries.select do |s|
    s.type.dimension.is_a? Rubex::AST::Expression::Literal::Base
  end.each do |arr|
    type = arr.type.type.to_s
    c_name = arr.c_name
    dimension = arr.type.dimension.c_code(@scope)
    value = arr.value.map { |a| a.c_code(@scope) } if arr.value
    code.declare_carray(type: type, c_name: c_name, dimension: dimension, value: value)
  end
end

#declare_ruby_objects(code, scope) ⇒ Object



70
71
72
73
74
# File 'lib/rubex/helpers/writers.rb', line 70

def declare_ruby_objects(code, scope)
  scope.ruby_obj_entries.each do |var|
    code.declare_variable type: var.type.to_s, c_name: var.c_name
  end
end

#declare_temps(code, scope) ⇒ Object



4
5
6
7
8
# File 'lib/rubex/helpers/writers.rb', line 4

def declare_temps(code, scope)
  scope.temp_entries.each do |var|
    code.declare_variable type: var.type.to_s, c_name: var.c_name
  end
end

#declare_types(code, type_entries) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubex/helpers/writers.rb', line 32

def declare_types(code, type_entries)
  type_entries.each do |entry|
    type = entry.type

    if type.alias_type?
      base = type.old_type
      if base.respond_to?(:base_type) && base.base_type.c_function?
        func = base.base_type
        str = "typedef #{func.type} (#{type.old_type.ptr_level} #{type.new_type})"
        str << '(' + func.arg_list.map { |e| e.type.to_s }.join(',') + ')'
        str << ';'
        code << str
      else
        code << "typedef #{type.old_type} #{type.new_type};"
      end
    elsif type.struct_or_union? && !entry.extern?
      code << sue_header(entry)
      code.block(sue_footer(entry)) do
        declare_vars code, type.scope
        declare_carrays code, type.scope
        declare_ruby_objects code, type.scope
      end
    end
    code.nl
  end
end

#declare_vars(code, scope) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/rubex/helpers/writers.rb', line 10

def declare_vars(code, scope)
  scope.var_entries.each do |var|
    if var.type.base_type.c_function?
      code.declare_func_ptr var: var
    else
      code.declare_variable type: var.type.to_s, c_name: var.c_name
    end
  end
end


66
67
68
# File 'lib/rubex/helpers/writers.rb', line 66

def sue_footer(entry)
  entry.extern ? ';' : " #{entry.type.c_name};"
end

#sue_header(entry) ⇒ Object



59
60
61
62
63
64
# File 'lib/rubex/helpers/writers.rb', line 59

def sue_header(entry)
  type = entry.type
  str = "#{type.kind} #{type.name}"
  str.prepend 'typedef ' unless entry.extern
  str
end

#write_char_2_ruby_str_code(code) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/rubex/helpers/writers.rb', line 97

def write_char_2_ruby_str_code(code)
  code << "VALUE #{Rubex::C_FUNC_CHAR2RUBYSTR}(char ch)"
  code.block do
    code << "char s[2];\n"
    code << "s[0] = ch;\n"
    code << "s[1] = '\\0';\n"
    code << "return rb_str_new2(s);\n"
  end
end

#write_char_2_ruby_str_header(header) ⇒ Object



92
93
94
95
# File 'lib/rubex/helpers/writers.rb', line 92

def write_char_2_ruby_str_header(header)
  header.nl
  header << "VALUE #{Rubex::C_FUNC_CHAR2RUBYSTR}(char ch);"
end

#write_usability_functions_code(code) ⇒ Object



87
88
89
90
# File 'lib/rubex/helpers/writers.rb', line 87

def write_usability_functions_code(code)
  code.nl
  write_char_2_ruby_str_code(code)
end

#write_usability_functions_header(header) ⇒ Object



82
83
84
85
# File 'lib/rubex/helpers/writers.rb', line 82

def write_usability_functions_header(header)
  header.nl
  write_char_2_ruby_str_header header
end

#write_usability_macros(code) ⇒ Object



76
77
78
79
80
# File 'lib/rubex/helpers/writers.rb', line 76

def write_usability_macros(code)
  code.nl
  code.c_macro Rubex::RUBEX_PREFIX + 'INT2BOOL(arg) (arg ? Qtrue : Qfalse)'
  code.nl
end