Class: CSquare::Generator::AssignOp

Inherits:
Op
  • Object
show all
Defined in:
lib/csquare/generator/assign_op.rb

Instance Attribute Summary

Attributes inherited from Op

#blueprint, #id, #patterns

Instance Method Summary collapse

Methods inherited from Op

#[], #[]=, #has_key?, #initialize, #inspect

Constructor Details

This class inherits a constructor from CSquare::Generator::Op

Instance Method Details

#decorated(args_types, return_type, default_type_symbol) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
# File 'lib/csquare/generator/assign_op.rb', line 3

def decorated args_types, return_type, default_type_symbol
  raise(ArgumentError, "expected 1-2 args, found #{args_types.size}") unless args_types.size == 1 || args_types.size == 2

  super(args_types, return_type, default_type_symbol)
end

#select_pattern(args_types, return_type) ⇒ Object

Determine which pattern to apply



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/csquare/generator/assign_op.rb', line 10

def select_pattern args_types, return_type

  arg = args_types.keys[1] || args_types.keys[0] # assign || unary
  type = args_types[arg]

  pat = begin
    if self.has_key?(arg)
      self[arg]
    elsif type.is_a?(Symbol)
      self[type]
    elsif type == return_type # homogeneous
      self[blueprint.key]
    else                      # heterogeneous

      if type == blueprint.long_key
        self[type]
      else
        self[:cast]
      end

    end
  end

  # If we have TYPE on the right, we need to change it to LONG_TYPE if the return type is also LONG_TYPE
  if pat =~ /\(struct\s([A-Z]+)\)/
    orig_struct_type = $1
    if return_type == "LONG_#{orig_struct_type}"
      return pat.gsub("(struct #{orig_struct_type})", "(struct LONG_#{orig_struct_type})")
    end
  end

  pat
end

#select_type_symbol(types, return_type, default_symbol) ⇒ Object

Determine a type symbol to apply to function calls based on argument and return types.



45
46
47
48
49
50
51
52
# File 'lib/csquare/generator/assign_op.rb', line 45

def select_type_symbol types, return_type, default_symbol

  if return_type == blueprint.long_key
    blueprint.types[default_symbol].long_id
  else
    default_symbol
  end
end