Class: CSquare::Generator::Op

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

Direct Known Subclasses

AssignOp, BinaryOp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blueprint, id) ⇒ Op

Returns a new instance of Op.



2
3
4
5
6
# File 'lib/csquare/generator/op.rb', line 2

def initialize blueprint, id
  @blueprint = blueprint
  @id        = id
  @patterns  = {}
end

Instance Attribute Details

#blueprintObject (readonly)

Returns the value of attribute blueprint.



8
9
10
# File 'lib/csquare/generator/op.rb', line 8

def blueprint
  @blueprint
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/csquare/generator/op.rb', line 8

def id
  @id
end

#patternsObject (readonly)

Returns the value of attribute patterns.



8
9
10
# File 'lib/csquare/generator/op.rb', line 8

def patterns
  @patterns
end

Instance Method Details

#[](t) ⇒ Object



78
79
80
# File 'lib/csquare/generator/op.rb', line 78

def [] t
  @patterns[t]
end

#[]=(t_or_ary, pattern) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/csquare/generator/op.rb', line 82

def []= t_or_ary, pattern
  if t_or_ary.is_a?(Array)
    t_or_ary.each do |t|
      @patterns[t] = pattern
    end
  else
    @patterns[t_or_ary] = pattern
  end
end

#decorated(args_and_types, return_type, default_type_symbol) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/csquare/generator/op.rb', line 15

def decorated args_and_types, return_type, default_type_symbol

  # If we're dealing with a blueprint containing only one type, convert all LONG typenames to regular typenames
  args_types =
  if blueprint.types.size == 1
    h = {}
    args_and_types.each_pair do |arg, type|
      h[arg] = type == blueprint.long_key ? blueprint.key : type
    end

    # Also update the return_type if applicable
    return_type = blueprint.key if return_type == blueprint.long_key
    h
  else
    args_and_types
  end

  # Determine pattern and type symbol
  pattern     = select_pattern_clone(args_types, return_type)

  return nil if pattern.nil? # no match, no need to deal with this expression

  type_symbol = select_type_symbol(args_types.values, return_type, default_type_symbol)

  # Replace $0, $1, ...
  args_types.keys.each_with_index do |arg, i|
    if arg =~ /^\*/ && pattern =~ /\$#{i}\./ # Flip *x.y to x->y
      deref_arg = "#{arg[1...arg.size]}->"
      pattern.gsub!(/\$#{i}\./, deref_arg)
    else
      pattern.gsub!(/\$#{i}/, arg.to_s)
    end
  end


  #if self.is_a?(CSquare::Generator::AssignOp)
  #  STDERR.puts args_types
  #  if args_types.has_key?(0) && args_types[0] == :integer && return_type == "LONG_TYPE"
  #    require 'pry'
  #    binding.pry
  #  end
  #end


  begin
    # Build an AST for the completed expression
    ast = C::Expression.parse(pattern)
  rescue C::ParseError => e
    STDERR.puts "\nParseError:"
    STDERR.puts pattern.inspect
    raise(e)
  end

  ast.decorate_calls!(type_symbol, blueprint)

  # Return type and AST get returned
  if default_type_symbol == type_symbol
    [ast, blueprint.key]
  else
    [ast, blueprint.long_key]
  end
end

#has_key?(t) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/csquare/generator/op.rb', line 92

def has_key? t
  @patterns.has_key?(t)
end

#inspectObject



10
11
12
13
# File 'lib/csquare/generator/op.rb', line 10

def inspect
  obj_id = "0x#{(self.object_id << 1).to_s(16)}"
  "#<#{self.class.to_s}:#{obj_id} id=#{@id.inspect} blueprint=#{@blueprint.id.inspect} keys=#{patterns.keys.inspect}>"
end