Class: CSquare::Generator::BinaryOp

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

Direct Known Subclasses

BooleanOp

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



9
10
11
12
13
14
15
16
# File 'lib/csquare/generator/binary_op.rb', line 9

def decorated args_types, return_type, default_type_symbol
  unless args_types.size == 2
    binding.pry
    raise(ArgumentError, "expected two args, found #{args_types.size}")
  end

  super(args_types, return_type, default_type_symbol)
end

#select_pattern(args_types, return_type) ⇒ Object

Determine which pattern to apply



20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
60
# File 'lib/csquare/generator/binary_op.rb', line 20

def select_pattern args_types, return_type
  args = args_types.keys
  types = args_types.values

  # If the exact arg has a pattern, prefer to use that
  if self.has_key?(args[1]) # look for exact arg
    self[args[1]]
  elsif types[1].is_a?(Symbol) # look for type symbol
    self[types[1]]
  else # look for TYPE or LONG_TYPE or :cast
    types = args_types.values

    reduced_types = (types + [return_type]).uniq


    if reduced_types.size == 1 # homogeneous

      if blueprint.long_key == reduced_types[0]
        self[blueprint.long_key] || self[:cast] || self[blueprint.key]
      else
        self[reduced_types[0]]
      end

    elsif reduced_types.size == 2

      self[:cast] || begin
        if types[0] == types[1] # homogeneous
          self[blueprint.key]
        else                    # heterogeneous
          self[blueprint.long_key]
        end
      end

    else

      # This should not happen
      raise(ArgumentError, "Error: three types were supplied: #{args_types.inspect}, #{return_type.inspect}")

    end
  end
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.



63
64
65
66
67
68
69
70
71
# File 'lib/csquare/generator/binary_op.rb', line 63

def select_type_symbol types, return_type, default_symbol
  reduced_types = (types + [return_type]).uniq

  if reduced_types.include?(blueprint.long_key)
    blueprint.types[default_symbol].long_id
  else
    default_symbol
  end
end