Class: Swift::Pyrite::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/swift/pyrite/transformer.rb

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ Transformer

Returns a new instance of Transformer.



6
7
8
# File 'lib/swift/pyrite/transformer.rb', line 6

def initialize(ast)
  @ast = ast
end

Instance Method Details

#arguments(exp) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/swift/pyrite/transformer.rb', line 124

def arguments(exp)
  return if exp.nil?
  output = []
  exp.each do |arg|
    output << "#{arg[:name]}: #{unwind_type(arg)}"
  end
  output.join(", ")
end

#copy_variable(exp) ⇒ Object



70
71
72
73
74
# File 'lib/swift/pyrite/transformer.rb', line 70

def copy_variable(exp)
  name = exp[:name]
  type = unwind_type(exp)
  "  var #{name}: #{type}"
end

#expression(exp) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/swift/pyrite/transformer.rb', line 60

def expression(exp)
  if exp[:type] == 'func'
    stub_function(exp[:func_decl])
  elsif exp[:type] == 'var'
    copy_variable(exp[:var_decl])
  else
    p exp
  end
end

#generateObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/swift/pyrite/transformer.rb', line 31

def generate
  output = []
  output << "class #{struct_name}: #{protocol_name} {"
  init = initializer(@ast)
  output << init unless init.empty?
  (@ast[:expressions] || []).map do |exp|
    output << expression(exp)
  end
  output << "}"
  output.join("\n")
end

#initializer(ast) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/swift/pyrite/transformer.rb', line 43

def initializer(ast)
  output = []
  vars = []
  (@ast[:expressions] || []).map do |exp|
    next if exp[:type] != 'var'
    e = exp[:var_decl]
    name, type = e[:name], unwind_type(e)
    output << "    self.#{name} = #{name}"
    vars << "#{name}: #{type}"
  end
  if output.size > 0
    output.unshift("  init(#{vars.join(', ')}) {")
    output << "  }"
  end
  output.join("\n")
end

#protocol_nameObject



10
11
12
# File 'lib/swift/pyrite/transformer.rb', line 10

def protocol_name
  @ast[:protocol]
end

#returns(returnTypes) ⇒ Object



137
138
139
140
# File 'lib/swift/pyrite/transformer.rb', line 137

def returns(returnTypes)
  return if returnTypes.nil?
  "-> #{tuple(returnTypes)} "
end

#saveArguments(funcname, exp) ⇒ Object



133
134
135
# File 'lib/swift/pyrite/transformer.rb', line 133

def saveArguments(funcname, exp)
  "    #{funcname}CalledWith.append((" + exp.map {|a| a[:name] }.join(", ") + "))"
end

#struct_nameObject



14
15
16
# File 'lib/swift/pyrite/transformer.rb', line 14

def struct_name
  "Fake#{protocol_name}"
end

#stub_function(exp) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/swift/pyrite/transformer.rb', line 76

def stub_function(exp)
  name = exp[:name]
  output = []
  output << variables(exp)
  output << "  func #{name}(#{arguments(exp[:arguments])}) #{returns(exp[:returnTypes])}{"
  output << "    #{name}CallCount += 1"
  unless exp[:arguments].nil?
    output << saveArguments(name, exp[:arguments])
  end
  unless exp[:returnTypes].nil?
    output << "    return #{name}Returns"
  end
  output << "  }"
  output << ""
  output.join("\n")
end

#tuple(exp) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/swift/pyrite/transformer.rb', line 109

def tuple(exp)
  results = exp.map do |item|
    if item[:name]
      "#{item[:name]}: #{unwind_type(item)}"
    else
      unwind_type(item)
    end
  end
  if results.count > 1
    "(#{results.join(', ')})"
  else
    results.join
  end
end

#unwind_type(exp) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/swift/pyrite/transformer.rb', line 18

def unwind_type exp
  output = []
  output << exp[:type_name]
  if exp[:generic]
    output << "<#{unwind_type(exp[:generic])}>"
  end
  if exp[:bracketed]
    output.unshift('[')
    output << ']'
  end
  output.join
end

#variables(exp) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/swift/pyrite/transformer.rb', line 93

def variables(exp)
  output = []
  output << "  var #{exp[:name]}CallCount: Int = 0"
  unless exp[:arguments].nil?
    types = exp[:arguments].map do |a|
      unwind_type(a)
    end
    tupleType = "(" + types.join(", ") + ")"
    output << "  var #{exp[:name]}CalledWith: [#{tupleType}] = [#{tupleType}]()"
  end
  unless exp[:returnTypes].nil?
    output << "  var #{exp[:name]}Returns: #{tuple(exp[:returnTypes])}"
  end
  output.join("\n")
end