ruby2c 1.0.0.9 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,6 @@ require 'crewriter'
6
6
  require 'r2ctestcase'
7
7
 
8
8
  class TestCRewriter < R2CTestCase
9
-
10
9
  def setup
11
10
  @processor = CRewriter.new
12
11
  @rewrite = CRewriter.new
@@ -16,17 +15,17 @@ class TestCRewriter < R2CTestCase
16
15
  def test_process_call_rewritten
17
16
 
18
17
  input = t(:call,
19
- t(:str, "this", Type.str),
18
+ t(:str, "this", CType.str),
20
19
  :+,
21
- t(:array, t(:str, "that", Type.str)),
22
- Type.str)
20
+ t(:array, t(:str, "that", CType.str)),
21
+ CType.str)
23
22
  expected = t(:call,
24
23
  nil,
25
24
  :strcat,
26
25
  t(:array,
27
- t(:str, "this", Type.str),
28
- t(:str, "that", Type.str)),
29
- Type.str)
26
+ t(:str, "this", CType.str),
27
+ t(:str, "that", CType.str)),
28
+ CType.str)
30
29
 
31
30
  assert_equal expected, @rewrite.process(input)
32
31
  end
@@ -34,10 +33,10 @@ class TestCRewriter < R2CTestCase
34
33
  def test_process_call_same
35
34
 
36
35
  input = t(:call,
37
- t(:lit, 1, Type.long),
36
+ t(:lit, 1, CType.long),
38
37
  :+,
39
- t(:array, t(:lit, 2, Type.long)),
40
- Type.long)
38
+ t(:array, t(:lit, 2, CType.long)),
39
+ CType.long)
41
40
  expected = input.deep_clone
42
41
 
43
42
  assert_equal expected, @rewrite.process(input)
@@ -52,113 +51,113 @@ class TestCRewriter < R2CTestCase
52
51
  t(:args),
53
52
  t(:scope,
54
53
  t(:block,
55
- t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
54
+ t(:lasgn, :sum, t(:lit, 0, CType.long), CType.long),
56
55
  t(:iter,
57
56
  t(:call,
58
- t(:lit, 0...10, Type.range),
57
+ t(:lit, 0...10, CType.range),
59
58
  :each,
60
- nil, Type.void),
61
- t(:dasgn_curr, :a, Type.long),
59
+ nil, CType.void),
60
+ t(:dasgn_curr, :a, CType.long),
62
61
  t(:iter,
63
62
  t(:call,
64
- t(:lit, 0...10, Type.range),
63
+ t(:lit, 0...10, CType.range),
65
64
  :each,
66
- nil, Type.void),
67
- t(:dasgn_curr, :b, Type.long),
65
+ nil, CType.void),
66
+ t(:dasgn_curr, :b, CType.long),
68
67
  t(:lasgn,
69
68
  :sum,
70
69
  t(:call,
71
- t(:lvar, :sum, Type.long),
70
+ t(:lvar, :sum, CType.long),
72
71
  :+,
73
72
  t(:arglist,
74
73
  t(:call,
75
- t(:dvar, :a, Type.long),
74
+ t(:dvar, :a, CType.long),
76
75
  :+,
77
- t(:arglist, t(:dvar, :b, Type.long)),
78
- Type.void)),
79
- Type.void),
80
- Type.long))),
81
- t(:return, t(:lvar, :sum, Type.long)))), Type.void))
76
+ t(:arglist, t(:dvar, :b, CType.long)),
77
+ CType.void)),
78
+ CType.void),
79
+ CType.long))),
80
+ t(:return, t(:lvar, :sum, CType.long)))), CType.void))
82
81
 
83
82
  expect = t(:class,
84
83
  :IterExample,
85
84
  :Object,
86
- t(:static, "static VALUE static_temp_7;", Type.fucked),
85
+ t(:static, "static VALUE static_temp_7;", CType.fucked),
87
86
  t(:defx,
88
87
  :temp_4,
89
88
  t(:args,
90
- t(:temp_5, Type.long),
91
- t(:temp_6, Type.value)),
89
+ t(:temp_5, CType.long),
90
+ t(:temp_6, CType.value)),
92
91
  t(:scope,
93
92
  t(:block,
94
93
  t(:lasgn,
95
94
  :sum,
96
- t(:lvar, :static_temp_7, Type.long),
97
- Type.long),
95
+ t(:lvar, :static_temp_7, CType.long),
96
+ CType.long),
98
97
  t(:lasgn,
99
98
  :b,
100
- t(:lvar, :temp_5, Type.long),
101
- Type.long),
99
+ t(:lvar, :temp_5, CType.long),
100
+ CType.long),
102
101
  t(:lasgn,
103
102
  :sum,
104
103
  t(:call,
105
- t(:lvar, :sum, Type.long),
104
+ t(:lvar, :sum, CType.long),
106
105
  :+,
107
106
  t(:arglist,
108
107
  t(:call,
109
- t(:dvar, :a, Type.long),
108
+ t(:dvar, :a, CType.long),
110
109
  :+,
111
- t(:arglist, t(:dvar, :b, Type.long)), Type.void)),
112
- Type.void), Type.long),
110
+ t(:arglist, t(:dvar, :b, CType.long)), CType.void)),
111
+ CType.void), CType.long),
113
112
  t(:lasgn,
114
113
  :static_temp_7,
115
- t(:lvar, :sum, Type.long),
116
- Type.long),
117
- t(:return, t(:nil, Type.value)))), Type.void),
114
+ t(:lvar, :sum, CType.long),
115
+ CType.long),
116
+ t(:return, t(:nil, CType.value)))), CType.void),
118
117
  t(:defx,
119
118
  :temp_1,
120
119
  t(:args,
121
- t(:temp_2, Type.long),
122
- t(:temp_3, Type.value)),
120
+ t(:temp_2, CType.long),
121
+ t(:temp_3, CType.value)),
123
122
  t(:scope,
124
123
  t(:block,
125
- t(:lasgn, :a, t(:lvar, :temp_2, Type.long), Type.long),
124
+ t(:lasgn, :a, t(:lvar, :temp_2, CType.long), CType.long),
126
125
  t(:iter,
127
126
  t(:call,
128
- t(:lit, 0...10, Type.range),
127
+ t(:lit, 0...10, CType.range),
129
128
  :each,
130
- nil, Type.void),
129
+ nil, CType.void),
131
130
  t(:args,
132
- t(:array, t(:lvar, :sum, Type.long), Type.void),
133
- t(:array, t(:lvar, :static_temp_7, Type.long), Type.void),
134
- Type.void),
131
+ t(:array, t(:lvar, :sum, CType.long), CType.void),
132
+ t(:array, t(:lvar, :static_temp_7, CType.long), CType.void),
133
+ CType.void),
135
134
  :temp_4),
136
- t(:return, t(:nil, Type.value)))), Type.void),
135
+ t(:return, t(:nil, CType.value)))), CType.void),
137
136
  t(:defn,
138
137
  :example,
139
138
  t(:args),
140
139
  t(:scope,
141
140
  t(:block,
142
- t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
141
+ t(:lasgn, :sum, t(:lit, 0, CType.long), CType.long),
143
142
  t(:iter,
144
143
  t(:call,
145
- t(:lit, 0...10, Type.range),
144
+ t(:lit, 0...10, CType.range),
146
145
  :each,
147
- nil, Type.void),
146
+ nil, CType.void),
148
147
  t(:args,
149
- t(:array, Type.void),
150
- t(:array, Type.void),
151
- Type.void),
148
+ t(:array, CType.void),
149
+ t(:array, CType.void),
150
+ CType.void),
152
151
  :temp_1),
153
- t(:return, t(:lvar, :sum, Type.long)))), Type.void),
154
- Type.zclass)
152
+ t(:return, t(:lvar, :sum, CType.long)))), CType.void),
153
+ CType.zclass)
155
154
 
156
155
  assert_equal expect, @rewrite.process(input)
157
156
  end
158
157
 
159
158
  def test_process_lasgn
160
- input = t(:lasgn, :variable, t(:nil), Type.long)
161
- expect = t(:lasgn, :variable, t(:nil), Type.long)
159
+ input = t(:lasgn, :variable, t(:nil), CType.long)
160
+ expect = t(:lasgn, :variable, t(:nil), CType.long)
162
161
 
163
162
  assert_equal expect, @rewrite.process(input)
164
163
  assert_equal [], @rewrite.free
@@ -166,129 +165,129 @@ class TestCRewriter < R2CTestCase
166
165
  end
167
166
 
168
167
  def test_process_lvar
169
- input = t(:lvar, :variable, Type.long)
170
- expect = t(:lvar, :variable, Type.long)
168
+ input = t(:lvar, :variable, CType.long)
169
+ expect = t(:lvar, :variable, CType.long)
171
170
 
172
171
  assert_equal expect, @rewrite.process(input)
173
- assert_equal [[:variable, Type.value]], @rewrite.free # HACK
172
+ assert_equal [[:variable, CType.value]], @rewrite.free # HACK
174
173
  assert_equal [:variable], @rewrite.env.all.keys
175
174
  end
176
175
 
177
176
  def test_process_iter_each
178
177
  # sum = 0; arr.each do |value| sum += value; end
179
178
  input = t(:block,
180
- t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long_list),
181
- t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
179
+ t(:lasgn, :arr, t(:array, t(:lit, 1, CType.long)), CType.long_list),
180
+ t(:lasgn, :sum, t(:lit, 0, CType.long), CType.long),
182
181
  t(:iter,
183
- t(:call, t(:lvar, :arr, Type.long), :each, nil, Type.void),
184
- t(:dasgn_curr, :value, Type.long),
182
+ t(:call, t(:lvar, :arr, CType.long), :each, nil, CType.void),
183
+ t(:dasgn_curr, :value, CType.long),
185
184
  t(:lasgn, # block guts
186
185
  :sum,
187
186
  t(:call,
188
- t(:lvar, :sum, Type.long),
187
+ t(:lvar, :sum, CType.long),
189
188
  :+,
190
- t(:arglist, t(:dvar, :value, Type.long)), Type.void),
191
- Type.long), Type.void), Type.void)
189
+ t(:arglist, t(:dvar, :value, CType.long)), CType.void),
190
+ CType.long), CType.void), CType.void)
192
191
 
193
192
  # $sum = sum; arr.each do |value| temp_1(value) end
194
193
  # def temp_1(value) sum = $sum; ...; $sum = sum; end
195
194
 
196
195
  expect = t(:block,
197
- t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long_list),
198
- t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
196
+ t(:lasgn, :arr, t(:array, t(:lit, 1, CType.long)), CType.long_list),
197
+ t(:lasgn, :sum, t(:lit, 0, CType.long), CType.long),
199
198
  t(:iter, # new iter
200
- t(:call, t(:lvar, :arr, Type.long), :each, nil, Type.void),
199
+ t(:call, t(:lvar, :arr, CType.long), :each, nil, CType.void),
201
200
  t(:args,
202
- t(:array, t(:lvar, :sum, Type.long), Type.void),
203
- t(:array, t(:lvar, :static_temp_4, Type.long), Type.void),
204
- Type.void),
205
- :temp_1),
206
- Type.void)
201
+ t(:array, t(:lvar, :sum, CType.long), CType.void),
202
+ t(:array, t(:lvar, :static_temp_4, CType.long), CType.void),
203
+ CType.void),
204
+ :temp_1, CType.void),
205
+ CType.void)
207
206
 
208
207
  assert_equal expect, @rewrite.process(input)
209
208
 
210
- static = t(:static, "static VALUE static_temp_4;", Type.fucked)
209
+ static = t(:static, "static VALUE static_temp_4;", CType.fucked)
211
210
 
212
211
  defx = t(:defx,
213
212
  :temp_1,
214
- t(:args, t(:temp_2, Type.long), t(:temp_3, Type.value)),
213
+ t(:args, t(:temp_2, CType.long), t(:temp_3, CType.value)),
215
214
  t(:scope,
216
215
  t(:block,
217
- t(:lasgn, :sum, t(:lvar, :static_temp_4, Type.long), Type.long),
218
- t(:lasgn, :value, t(:lvar, :temp_2, Type.long), Type.long),
216
+ t(:lasgn, :sum, t(:lvar, :static_temp_4, CType.long), CType.long),
217
+ t(:lasgn, :value, t(:lvar, :temp_2, CType.long), CType.long),
219
218
  t(:lasgn, :sum, # sum =
220
219
  t(:call,
221
- t(:lvar, :sum, Type.long), # sum + value
220
+ t(:lvar, :sum, CType.long), # sum + value
222
221
  :+,
223
- t(:arglist, t(:dvar, :value, Type.long)), Type.void),
224
- Type.long),
225
- t(:lasgn, :static_temp_4, t(:lvar, :sum, Type.long), Type.long),
226
- t(:return, t(:nil, Type.value)))),
227
- Type.void)
222
+ t(:arglist, t(:dvar, :value, CType.long)), CType.void),
223
+ CType.long),
224
+ t(:lasgn, :static_temp_4, t(:lvar, :sum, CType.long), CType.long),
225
+ t(:return, t(:nil, CType.value)))),
226
+ CType.void)
228
227
 
229
228
  assert_equal [static, defx], @rewrite.extra_methods
230
229
  end
231
230
 
232
231
  def test_process_iter_each_with_index
233
232
  input = t(:block,
234
- t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long),
235
- t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
233
+ t(:lasgn, :arr, t(:array, t(:lit, 1, CType.long)), CType.long),
234
+ t(:lasgn, :sum, t(:lit, 0, CType.long), CType.long),
236
235
  t(:iter,
237
236
  t(:call,
238
- t(:lvar, :arr, Type.long),
237
+ t(:lvar, :arr, CType.long),
239
238
  :each_with_index,
240
- nil, Type.void),
239
+ nil, CType.void),
241
240
  t(:masgn,
242
241
  t(:array,
243
- t(:dasgn_curr, :value, Type.long),
244
- t(:dasgn_curr, :i, Type.long))),
242
+ t(:dasgn_curr, :value, CType.long),
243
+ t(:dasgn_curr, :i, CType.long))),
245
244
  t(:lasgn, # block guts
246
245
  :sum,
247
246
  t(:call,
248
- t(:lvar, :sum, Type.long),
247
+ t(:lvar, :sum, CType.long),
249
248
  :+,
250
- t(:arglist, t(:dvar, :value, Type.long)), Type.void),
251
- Type.long)))
249
+ t(:arglist, t(:dvar, :value, CType.long)), CType.void),
250
+ CType.long)))
252
251
 
253
252
  expect = t(:block,
254
- t(:lasgn, :arr, t(:array, t(:lit, 1, Type.long)), Type.long),
255
- t(:lasgn, :sum, t(:lit, 0, Type.long), Type.long),
253
+ t(:lasgn, :arr, t(:array, t(:lit, 1, CType.long)), CType.long),
254
+ t(:lasgn, :sum, t(:lit, 0, CType.long), CType.long),
256
255
  t(:iter, # new iter
257
256
  t(:call,
258
- t(:lvar, :arr, Type.long),
257
+ t(:lvar, :arr, CType.long),
259
258
  :each_with_index,
260
- nil, Type.void),
259
+ nil, CType.void),
261
260
  t(:args,
262
- t(:array, t(:lvar, :sum, Type.long), Type.void),
263
- t(:array, t(:lvar, :static_temp_4, Type.long), Type.void),
264
- Type.void),
261
+ t(:array, t(:lvar, :sum, CType.long), CType.void),
262
+ t(:array, t(:lvar, :static_temp_4, CType.long), CType.void),
263
+ CType.void),
265
264
  :temp_1))
266
265
 
267
266
  assert_equal expect, @rewrite.process(input)
268
267
 
269
- static = t(:static, "static VALUE static_temp_4;", Type.fucked)
268
+ static = t(:static, "static VALUE static_temp_4;", CType.fucked)
270
269
 
271
270
  defx = t(:defx,
272
271
  :temp_1,
273
272
  t(:args,
274
- t(:temp_2, Type.value),
275
- t(:temp_3, Type.value)),
273
+ t(:temp_2, CType.value),
274
+ t(:temp_3, CType.value)),
276
275
  t(:scope,
277
276
  t(:block,
278
- t(:lasgn, :sum, t(:lvar, :static_temp_4, Type.long), Type.long),
277
+ t(:lasgn, :sum, t(:lvar, :static_temp_4, CType.long), CType.long),
279
278
  t(:masgn,
280
279
  t(:array,
281
- t(:lasgn, :value, nil, Type.long),
282
- t(:lasgn, :i, nil, Type.long)),
283
- t(:to_ary, t(:lvar, :temp_2, Type.value))),
280
+ t(:lasgn, :value, nil, CType.long),
281
+ t(:lasgn, :i, nil, CType.long)),
282
+ t(:to_ary, t(:lvar, :temp_2, CType.value))),
284
283
  t(:lasgn, :sum, # sum =
285
284
  t(:call,
286
- t(:lvar, :sum, Type.long), # sum + value
285
+ t(:lvar, :sum, CType.long), # sum + value
287
286
  :+,
288
- t(:arglist, t(:dvar, :value, Type.long)), Type.void),
289
- Type.long),
290
- t(:lasgn, :static_temp_4, t(:lvar, :sum, Type.long), Type.long),
291
- t(:return, t(:nil, Type.value)))), Type.void)
287
+ t(:arglist, t(:dvar, :value, CType.long)), CType.void),
288
+ CType.long),
289
+ t(:lasgn, :static_temp_4, t(:lvar, :sum, CType.long), CType.long),
290
+ t(:return, t(:nil, CType.value)))), CType.void)
292
291
 
293
292
  assert_equal [static, defx], @rewrite.extra_methods
294
293
  end
@@ -296,33 +295,33 @@ class TestCRewriter < R2CTestCase
296
295
  def test_free
297
296
  e = @rewrite.env
298
297
 
299
- e.add :sum, Type.value
298
+ e.add :sum, CType.value
300
299
  e.set_val :sum, true
301
300
 
302
301
  e.extend
303
302
 
304
- e.add :arr, Type.value
303
+ e.add :arr, CType.value
305
304
  e.set_val :arr, true
306
305
 
307
- # HACK this is a real bug, but not a priority for me right now
308
- # expected = {:arr=>[Type.value, true], :sum=>[Type.value, true]}
309
- # assert_equal expected, e.all
310
- #
311
- # expected = [{:arr=>[Type.value, true]}, {:sum=>[Type.value, true]}]
312
- # assert_equal expected, e.env
313
- #
314
- # assert_equal [[:arr, Type.value]], @rewrite.free
306
+ skip "this is a real bug, but not a priority for me right now"
307
+
308
+ expected = {:arr=>[CType.value, true], :sum=>[CType.value, true]}
309
+ assert_equal expected, e.all
310
+
311
+ expected = [{:arr=>[CType.value, true]}, {:sum=>[CType.value, true]}]
312
+ assert_equal expected, e.env
313
+
314
+ assert_equal [[:arr, CType.value]], @rewrite.free
315
315
  end
316
316
 
317
317
  def test_var_names_in
318
- assert_equal [[:value, Type.long]], @rewrite.var_names_in(t(:dasgn_curr, :value, Type.long))
318
+ assert_equal [[:value, CType.long]], @rewrite.var_names_in(t(:dasgn_curr, :value, CType.long))
319
319
 
320
320
  input = t(:masgn, t(:array,
321
- t(:dasgn_curr, :value, Type.long),
322
- t(:dasgn_curr, :i, Type.str)))
323
- expect = [[:value, Type.long], [:i, Type.str]]
321
+ t(:dasgn_curr, :value, CType.long),
322
+ t(:dasgn_curr, :i, CType.str)))
323
+ expect = [[:value, CType.long], [:i, CType.str]]
324
324
 
325
325
  assert_equal expect, @rewrite.var_names_in(input)
326
326
  end
327
327
  end
328
-
@@ -3,7 +3,7 @@
3
3
  $TESTING = true
4
4
 
5
5
  require 'minitest/autorun' if $0 == __FILE__
6
- require 'minitest/unit'
6
+ require 'minitest/test'
7
7
  require 'type_checker'
8
8
 
9
9
  class RandomCode # ZenTest SKIP
@@ -26,7 +26,7 @@ class RandomCode # ZenTest SKIP
26
26
 
27
27
  end
28
28
 
29
- class TestExtraTypeChecker < MiniTest::Unit::TestCase # ZenTest SKIP
29
+ class TestExtraTypeChecker < Minitest::Test # ZenTest SKIP
30
30
 
31
31
  def setup
32
32
  @rewriter = Rewriter.new
@@ -58,8 +58,8 @@ class TestExtraTypeChecker < MiniTest::Unit::TestCase # ZenTest SKIP
58
58
  end
59
59
 
60
60
  def util_unify_function
61
- a = Type.function(Type.unknown, [ Type.unknown ], Type.unknown)
62
- b = Type.function(Type.long, [ Type.str ], Type.void)
61
+ a = CType.function(CType.unknown, [ CType.unknown ], CType.unknown)
62
+ b = CType.function(CType.long, [ CType.str ], CType.void)
63
63
  a.unify b
64
64
  act = a.list_type
65
65
  bct = b.list_type