Module: Rubex::Helpers

Defined in:
lib/rubex/helpers.rb,
lib/rubex/helpers/writers.rb,
lib/rubex/helpers/node_type_methods.rb

Defined Under Namespace

Modules: NodeTypeMethods, Writers

Class Method Summary collapse

Class Method Details

.construct_function_argument(data) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rubex/helpers.rb', line 8

def construct_function_argument(data)
  if data[:variables][0][:ident].is_a?(Hash)
    Rubex::AST::Expression::FuncPtrArgDeclaration.new(data)
  else
    Rubex::AST::Expression::ArgDeclaration.new(data)
  end
end

.create_arg_arrays(arg_list) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rubex/helpers.rb', line 62

def create_arg_arrays(arg_list)
  arg_list.each_with_object([]) do |arg, array|
    entry = arg.entry
    c_name = entry.type.base_type.c_function? ? '' : entry.c_name
    array << [entry.type.to_s, c_name]
  end
end

.determine_dtype(data, ptr_level) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubex/helpers.rb', line 34

def determine_dtype(data, ptr_level)
  if ptr_level && ptr_level[-1] == '*'
    ptr_level = ptr_level.dup
    base_type = Rubex::DataType::CPtr.new simple_dtype(data)
    ptr_level.chop!

    ptr_level.each_char do |_star|
      base_type = Rubex::DataType::CPtr.new base_type
    end

    base_type
  else
    simple_dtype(data)
  end
end

.result_type_for(left, right) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rubex/helpers.rb', line 26

def result_type_for(left, right)
  return left.dup if left == right

  (left < right ? right.dup : left.dup)
rescue ArgumentError => e
  raise Rubex::TypeError, e.to_s
end

.simple_dtype(dtype) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubex/helpers.rb', line 50

def simple_dtype(dtype)
  if dtype.is_a?(Rubex::DataType::CFunction)
    dtype
  else
    begin
      Rubex::CUSTOM_TYPES[dtype] || Rubex::TYPE_MAPPINGS[dtype].new
    rescue StandardError
      raise Rubex::TypeError, "Type #{dtype} not previously declared."
    end
  end
end

.to_lhs_type(lhs, rhs) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rubex/helpers.rb', line 16

def to_lhs_type(lhs, rhs)
  if lhs.type.object?
    rhs.to_ruby_object
  elsif !lhs.type.object? && rhs.type.object?
    rhs.from_ruby_object(lhs)
  else
    rhs
  end
end