ruby2c 1.0.0.9 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,43 +14,43 @@ class TestRubyToAnsiC < R2CTestCase
14
14
  end
15
15
 
16
16
  def test_c_type_bool
17
- assert_equal "bool", @ruby_to_c.class.c_type(Type.bool)
17
+ assert_equal "bool", @ruby_to_c.class.c_type(CType.bool)
18
18
  end
19
19
 
20
20
  def test_c_type_float
21
- assert_equal "double", @ruby_to_c.class.c_type(Type.float)
21
+ assert_equal "double", @ruby_to_c.class.c_type(CType.float)
22
22
  end
23
23
 
24
24
  def test_c_type_long
25
- assert_equal "long", @ruby_to_c.class.c_type(Type.long)
25
+ assert_equal "long", @ruby_to_c.class.c_type(CType.long)
26
26
  end
27
27
 
28
28
  def test_c_type_long_list
29
- assert_equal "long *", @ruby_to_c.class.c_type(Type.long_list)
29
+ assert_equal "long *", @ruby_to_c.class.c_type(CType.long_list)
30
30
  end
31
31
 
32
32
  def test_c_type_str
33
- assert_equal "str", @ruby_to_c.class.c_type(Type.str)
33
+ assert_equal "str", @ruby_to_c.class.c_type(CType.str)
34
34
  end
35
35
 
36
36
  def test_c_type_str_list
37
- assert_equal "str *", @ruby_to_c.class.c_type(Type.str_list)
37
+ assert_equal "str *", @ruby_to_c.class.c_type(CType.str_list)
38
38
  end
39
39
 
40
40
  def test_c_type_symbol
41
- assert_equal "symbol", @ruby_to_c.class.c_type(Type.symbol)
41
+ assert_equal "symbol", @ruby_to_c.class.c_type(CType.symbol)
42
42
  end
43
43
 
44
44
  def test_c_type_unknown
45
- assert_equal "void *", @ruby_to_c.class.c_type(Type.unknown)
45
+ assert_equal "void *", @ruby_to_c.class.c_type(CType.unknown)
46
46
  end
47
47
 
48
48
  def test_c_type_value
49
- assert_equal "void *", @ruby_to_c.class.c_type(Type.value)
49
+ assert_equal "void *", @ruby_to_c.class.c_type(CType.value)
50
50
  end
51
51
 
52
52
  def test_c_type_void
53
- assert_equal "void", @ruby_to_c.class.c_type(Type.void)
53
+ assert_equal "void", @ruby_to_c.class.c_type(CType.void)
54
54
  end
55
55
 
56
56
  def test_env
@@ -59,7 +59,7 @@ class TestRubyToAnsiC < R2CTestCase
59
59
  end
60
60
 
61
61
  def test_process_and
62
- input = t(:and, t(:lit, 1, Type.long), t(:lit, 2, Type.long))
62
+ input = t(:and, t(:lit, 1, CType.long), t(:lit, 2, CType.long))
63
63
  output = "1 && 2"
64
64
 
65
65
  assert_equal output, @ruby_to_c.process(input)
@@ -74,8 +74,8 @@ class TestRubyToAnsiC < R2CTestCase
74
74
 
75
75
  def test_process_args_normal
76
76
  input = t(:args,
77
- t(:foo, Type.long),
78
- t(:bar, Type.long))
77
+ t(:foo, CType.long),
78
+ t(:bar, CType.long))
79
79
  output = "(long foo, long bar)"
80
80
 
81
81
  assert_equal output, @ruby_to_c.process(input)
@@ -90,8 +90,8 @@ class TestRubyToAnsiC < R2CTestCase
90
90
 
91
91
  def test_process_array_multiple
92
92
  input = t(:array,
93
- t(:lvar, :arg1, Type.long),
94
- t(:lvar, :arg2, Type.long))
93
+ t(:lvar, :arg1, CType.long),
94
+ t(:lvar, :arg2, CType.long))
95
95
  output = "arg1, arg2"
96
96
 
97
97
  assert_equal output, @ruby_to_c.process(input)
@@ -99,7 +99,7 @@ class TestRubyToAnsiC < R2CTestCase
99
99
 
100
100
  def test_process_array_single
101
101
  input = t(:array,
102
- t(:lvar, :arg1, Type.long))
102
+ t(:lvar, :arg1, CType.long))
103
103
  output = "arg1"
104
104
 
105
105
  assert_equal output, @ruby_to_c.process(input)
@@ -129,7 +129,7 @@ class TestRubyToAnsiC < R2CTestCase
129
129
  end
130
130
 
131
131
  def test_process_call_lhs
132
- input = t(:call, t(:lit, 1, Type.long), :name, nil)
132
+ input = t(:call, t(:lit, 1, CType.long), :name, nil)
133
133
  output = "name(1)"
134
134
 
135
135
  assert_equal output, @ruby_to_c.process(input)
@@ -137,7 +137,7 @@ class TestRubyToAnsiC < R2CTestCase
137
137
 
138
138
  def test_process_call_lhs_rhs
139
139
  input = t(:call,
140
- t(:lit, 1, Type.long),
140
+ t(:lit, 1, CType.long),
141
141
  :name,
142
142
  t(:array, t(:str, "foo")))
143
143
  output = "name(1, \"foo\")"
@@ -147,7 +147,7 @@ class TestRubyToAnsiC < R2CTestCase
147
147
 
148
148
  def test_process_call_nil?
149
149
  input = t(:call,
150
- t(:lvar, :arg, Type.long),
150
+ t(:lvar, :arg, CType.long),
151
151
  :nil?,
152
152
  nil)
153
153
  output = "arg"
@@ -160,9 +160,9 @@ class TestRubyToAnsiC < R2CTestCase
160
160
 
161
161
  methods.each do |method|
162
162
  input = t(:call,
163
- t(:lit, 1, Type.long),
163
+ t(:lit, 1, CType.long),
164
164
  method,
165
- t(:array, t(:lit, 2, Type.long)))
165
+ t(:array, t(:lit, 2, CType.long)))
166
166
  output = "1 #{method} 2"
167
167
 
168
168
  assert_equal output, @ruby_to_c.process(input)
@@ -181,12 +181,12 @@ class TestRubyToAnsiC < R2CTestCase
181
181
  end
182
182
 
183
183
  def test_process_dasgn_curr
184
- input = t(:dasgn_curr, :x, Type.long)
184
+ input = t(:dasgn_curr, :x, CType.long)
185
185
  output = "x"
186
186
 
187
187
  assert_equal output, @ruby_to_c.process(input)
188
188
  # HACK - see test_type_checker equivalent test
189
- # assert_equal Type.long, @ruby_to_c.env.lookup("x")
189
+ # assert_equal CType.long, @ruby_to_c.env.lookup("x")
190
190
  end
191
191
 
192
192
  # TODO: fix for 1.8.2
@@ -195,7 +195,7 @@ class TestRubyToAnsiC < R2CTestCase
195
195
  :empty,
196
196
  t(:args),
197
197
  t(:scope),
198
- Type.function([], Type.void))
198
+ CType.function([], CType.void))
199
199
  output = "void\nempty() {\n}"
200
200
  assert_equal output, @ruby_to_c.process(input)
201
201
 
@@ -206,19 +206,19 @@ class TestRubyToAnsiC < R2CTestCase
206
206
  def test_process_defn_with_args_and_body
207
207
  input = t(:defn, :empty,
208
208
  t(:args,
209
- t(:foo, Type.long),
210
- t(:bar, Type.long)),
209
+ t(:foo, CType.long),
210
+ t(:bar, CType.long)),
211
211
  t(:scope,
212
212
  t(:block,
213
- t(:lit, 5, Type.long))),
214
- Type.function([], Type.void))
213
+ t(:lit, 5, CType.long))),
214
+ CType.function([], CType.void))
215
215
  output = "void\nempty(long foo, long bar) {\n5;\n}"
216
216
 
217
217
  assert_equal output, @ruby_to_c.process(input)
218
218
  end
219
219
 
220
220
  def test_process_dvar
221
- input = t(:dvar, :dvar, Type.long)
221
+ input = t(:dvar, :dvar, CType.long)
222
222
  output = "dvar"
223
223
 
224
224
  assert_equal output, @ruby_to_c.process(input)
@@ -232,17 +232,17 @@ class TestRubyToAnsiC < R2CTestCase
232
232
  end
233
233
 
234
234
  def test_process_gvar
235
- input = t(:gvar, :$stderr, Type.long)
235
+ input = t(:gvar, :$stderr, CType.long)
236
236
  output = "stderr"
237
237
 
238
238
  assert_equal output, @ruby_to_c.process(input)
239
239
  assert_raises RuntimeError do
240
- @ruby_to_c.process t(:gvar, :$some_gvar, Type.long)
240
+ @ruby_to_c.process t(:gvar, :$some_gvar, CType.long)
241
241
  end
242
242
  end
243
243
 
244
244
  def test_process_iasgn
245
- input = t(:iasgn, :@blah, t(:lit, 42, Type.long), Type.long)
245
+ input = t(:iasgn, :@blah, t(:lit, 42, CType.long), CType.long)
246
246
  expected = "self->blah = 42"
247
247
 
248
248
  assert_equal expected, @ruby_to_c.process(input)
@@ -251,9 +251,9 @@ class TestRubyToAnsiC < R2CTestCase
251
251
  def test_process_if
252
252
  input = t(:if,
253
253
  t(:call,
254
- t(:lit, 1, Type.long),
254
+ t(:lit, 1, CType.long),
255
255
  :==,
256
- t(:array, t(:lit, 2, Type.long))),
256
+ t(:array, t(:lit, 2, CType.long))),
257
257
  t(:str, "not equal"),
258
258
  nil)
259
259
  output = "if (1 == 2) {\n\"not equal\";\n}"
@@ -264,11 +264,11 @@ class TestRubyToAnsiC < R2CTestCase
264
264
  def test_process_if_block
265
265
  input = t(:if,
266
266
  t(:call,
267
- t(:lit, 1, Type.long),
267
+ t(:lit, 1, CType.long),
268
268
  :==,
269
- t(:array, t(:lit, 2, Type.long))),
269
+ t(:array, t(:lit, 2, CType.long))),
270
270
  t(:block,
271
- t(:lit, 5, Type.long),
271
+ t(:lit, 5, CType.long),
272
272
  t(:str, "not equal")),
273
273
  nil)
274
274
  output = "if (1 == 2) {\n5;\n\"not equal\";\n}"
@@ -279,9 +279,9 @@ class TestRubyToAnsiC < R2CTestCase
279
279
  def test_process_if_else
280
280
  input = t(:if,
281
281
  t(:call,
282
- t(:lit, 1, Type.long),
282
+ t(:lit, 1, CType.long),
283
283
  :==,
284
- t(:array, t(:lit, 2, Type.long))),
284
+ t(:array, t(:lit, 2, CType.long))),
285
285
  t(:str, "not equal"),
286
286
  t(:str, "equal"))
287
287
  output = "if (1 == 2) {\n\"not equal\";\n} else {\n\"equal\";\n}"
@@ -290,43 +290,43 @@ class TestRubyToAnsiC < R2CTestCase
290
290
  end
291
291
 
292
292
  def test_process_ivar
293
- @ruby_to_c.env.add :@blah, Type.long
294
- input = t(:ivar, :@blah, Type.long)
293
+ @ruby_to_c.env.add :@blah, CType.long
294
+ input = t(:ivar, :@blah, CType.long)
295
295
  expected = "self->blah"
296
296
 
297
297
  assert_equal expected, @ruby_to_c.process(input)
298
298
  end
299
299
 
300
300
  def test_process_lasgn
301
- input = t(:lasgn, :var, t(:str, "foo"), Type.str)
301
+ input = t(:lasgn, :var, t(:str, "foo"), CType.str)
302
302
  output = "var = \"foo\""
303
303
 
304
304
  assert_equal output, @ruby_to_c.process(input)
305
305
  end
306
306
 
307
307
  def test_process_lit_float
308
- input = t(:lit, 1.0, Type.float)
308
+ input = t(:lit, 1.0, CType.float)
309
309
  output = "1.0"
310
310
 
311
311
  assert_equal output, @ruby_to_c.process(input)
312
312
  end
313
313
 
314
314
  def test_process_lit_long
315
- input = t(:lit, 1, Type.long)
315
+ input = t(:lit, 1, CType.long)
316
316
  output = "1"
317
317
 
318
318
  assert_equal output, @ruby_to_c.process(input)
319
319
  end
320
320
 
321
321
  def test_process_lit_sym
322
- input = t(:lit, :sym, Type.symbol)
322
+ input = t(:lit, :sym, CType.symbol)
323
323
  output = "\"sym\""
324
324
 
325
325
  assert_equal output, @ruby_to_c.process(input)
326
326
  end
327
327
 
328
328
  def test_process_lvar
329
- input = t(:lvar, :arg, Type.long)
329
+ input = t(:lvar, :arg, CType.long)
330
330
  output = "arg"
331
331
 
332
332
  assert_equal output, @ruby_to_c.process(input)
@@ -340,14 +340,14 @@ class TestRubyToAnsiC < R2CTestCase
340
340
  end
341
341
 
342
342
  def test_process_not
343
- input = t(:not, t(:true, Type.bool), Type.bool)
343
+ input = t(:not, t(:true, CType.bool), CType.bool)
344
344
  output = "!(1)"
345
345
 
346
346
  assert_equal output, @ruby_to_c.process(input)
347
347
  end
348
348
 
349
349
  def test_process_or
350
- input = t(:or, t(:lit, 1, Type.long), t(:lit, 2, Type.long))
350
+ input = t(:or, t(:lit, 1, CType.long), t(:lit, 2, CType.long))
351
351
  output = "1 || 2"
352
352
 
353
353
  assert_equal output, @ruby_to_c.process(input)
@@ -380,7 +380,7 @@ class TestRubyToAnsiC < R2CTestCase
380
380
  input = t(:scope, t(:block,
381
381
  t(:lasgn, :arg,
382
382
  t(:str, "declare me"),
383
- Type.str),
383
+ CType.str),
384
384
  t(:return, t(:nil))))
385
385
  output = "{\nstr arg;\narg = \"declare me\";\nreturn NULL;\n}"
386
386
 
@@ -388,14 +388,14 @@ class TestRubyToAnsiC < R2CTestCase
388
388
  end
389
389
 
390
390
  def test_process_str
391
- input = t(:str, "foo", Type.str)
391
+ input = t(:str, "foo", CType.str)
392
392
  output = "\"foo\""
393
393
 
394
394
  assert_equal output, @ruby_to_c.process(input)
395
395
  end
396
396
 
397
397
  def test_process_str_backslashed
398
- input = t(:str, "foo\nbar", Type.str)
398
+ input = t(:str, "foo\nbar", CType.str)
399
399
  output = "\"foo\\nbar\""
400
400
 
401
401
  assert_equal output, @ruby_to_c.process(input)
@@ -403,7 +403,7 @@ class TestRubyToAnsiC < R2CTestCase
403
403
 
404
404
  def test_process_str_multi
405
405
  input = t(:str, "foo
406
- bar", Type.str)
406
+ bar", CType.str)
407
407
  output = "\"foo\\nbar\""
408
408
 
409
409
  assert_equal output, @ruby_to_c.process(input)
@@ -419,9 +419,9 @@ bar", Type.str)
419
419
  def test_process_unless
420
420
  input = t(:if,
421
421
  t(:call,
422
- t(:lit, 1, Type.long),
422
+ t(:lit, 1, CType.long),
423
423
  :==,
424
- t(:array, t(:lit, 2, Type.long))),
424
+ t(:array, t(:lit, 2, CType.long))),
425
425
  nil,
426
426
  t(:str, "equal"))
427
427
  output = "if (1 == 2) {\n;\n} else {\n\"equal\";\n}"
@@ -431,7 +431,7 @@ bar", Type.str)
431
431
 
432
432
  def test_process_while
433
433
  input = t(:while,
434
- t(:call, t(:lvar, :n), :<=, t(:array, t(:lit, 3, Type.long))),
434
+ t(:call, t(:lvar, :n), :<=, t(:array, t(:lit, 3, CType.long))),
435
435
  t(:block,
436
436
  t(:call,
437
437
  nil,
@@ -446,8 +446,8 @@ bar", Type.str)
446
446
  t(:lvar, :n),
447
447
  :+,
448
448
  t(:array,
449
- t(:lit, 1, Type.long))),
450
- Type.long)), true) # NOTE Type.long needed but not used
449
+ t(:lit, 1, CType.long))),
450
+ CType.long)), true) # NOTE CType.long needed but not used
451
451
 
452
452
  expected = "while (n <= 3) {\nputs(to_s(n));\nn = n + 1;\n}"
453
453
 
@@ -460,7 +460,7 @@ bar", Type.str)
460
460
  :empty,
461
461
  t(:args),
462
462
  t(:scope),
463
- Type.function([], Type.void))
463
+ CType.function([], CType.void))
464
464
 
465
465
  assert_equal "void empty();\n", @ruby_to_c.prototypes.first
466
466
  end
@@ -19,8 +19,8 @@ class TestRubyToRubyC < R2CTestCase
19
19
  def test_process_dstr
20
20
  input = t(:dstr,
21
21
  "var is ",
22
- t(:lit, 42, Type.long),
23
- t(:str, ". So there.", Type.str), Type.str)
22
+ t(:lit, 42, CType.long),
23
+ t(:str, ". So there.", CType.str), CType.str)
24
24
  output = 'rb_funcall(rb_mKernel, rb_intern("sprintf"), 4, rb_str_new2("%s%s%s"), rb_str_new2("var is "), LONG2NUM(42), rb_str_new2(". So there."))'
25
25
 
26
26
  assert_equal output, @ruby_to_c.process(input)
@@ -29,14 +29,14 @@ class TestRubyToRubyC < R2CTestCase
29
29
  def test_process_dxstr
30
30
  input = t(:dxstr,
31
31
  "touch ",
32
- t(:lvar, :x, Type.str), Type.str)
32
+ t(:lvar, :x, CType.str), CType.str)
33
33
  output = 'rb_funcall(rb_mKernel, rb_intern("`"), 1, rb_funcall(rb_mKernel, rb_intern("sprintf"), 3, rb_str_new2("%s%s"), rb_str_new2("touch "), x))'
34
34
 
35
35
  assert_equal output, @ruby_to_c.process(input)
36
36
  end
37
37
 
38
38
  def test_process_lit_float
39
- input = t(:lit, 1.0, Type.float)
39
+ input = t(:lit, 1.0, CType.float)
40
40
  output = "rb_float_new(1.0)"
41
41
 
42
42
  assert_equal output, @ruby_to_c.process(input)
@@ -46,13 +46,13 @@ class TestRubyToRubyC < R2CTestCase
46
46
  # ruby: arrays.each { ... }
47
47
  input = t(:iter,
48
48
  t(:call,
49
- t(:lvar, :arrays, Type.str_list), # should register static
49
+ t(:lvar, :arrays, CType.str_list), # should register static
50
50
  :each,
51
- nil, Type.unknown),
51
+ nil, CType.unknown),
52
52
  t(:args,
53
- t(:array, t(:lvar, :arrays, Type.value), Type.void),
54
- t(:array, t(:lvar, :static_temp_4, Type.value), Type.void),
55
- Type.void),
53
+ t(:array, t(:lvar, :arrays, CType.value), CType.void),
54
+ t(:array, t(:lvar, :static_temp_4, CType.value), CType.void),
55
+ CType.void),
56
56
  :temp_1)
57
57
  output = "static_temp_4 = arrays;
58
58
  rb_iterate(rb_each, arrays, temp_1, Qnil);
@@ -66,25 +66,25 @@ arrays = static_temp_4;"
66
66
  input = t(:defx,
67
67
  :temp_1,
68
68
  t(:args,
69
- t(:temp_2, Type.str),
70
- t(:temp_3, Type.value)),
69
+ t(:temp_2, CType.str),
70
+ t(:temp_3, CType.value)),
71
71
  t(:scope,
72
72
  t(:block,
73
73
  t(:lasgn,
74
74
  :arrays,
75
- t(:lvar, :static_arrays, Type.value),
76
- Type.value),
77
- t(:lasgn, :x, t(:lvar, :temp_2, Type.str),
78
- Type.str),
75
+ t(:lvar, :static_arrays, CType.value),
76
+ CType.value),
77
+ t(:lasgn, :x, t(:lvar, :temp_2, CType.str),
78
+ CType.str),
79
79
  t(:call,
80
80
  nil,
81
81
  :puts,
82
- t(:arglist, t(:dvar, :x, Type.str)), Type.void),
82
+ t(:arglist, t(:dvar, :x, CType.str)), CType.void),
83
83
  t(:lasgn,
84
84
  :static_arrays,
85
- t(:lvar, :arrays, Type.value),
86
- Type.value),
87
- t(:return, t(:nil, Type.value)))), Type.void)
85
+ t(:lvar, :arrays, CType.value),
86
+ CType.value),
87
+ t(:return, t(:nil, CType.value)))), CType.void)
88
88
 
89
89
  output = "static VALUE
90
90
  rrc_c_temp_1(VALUE temp_2, VALUE temp_3) {
@@ -101,49 +101,49 @@ return Qnil;
101
101
  end
102
102
 
103
103
  def test_process_lit_long
104
- input = t(:lit, 1, Type.long)
104
+ input = t(:lit, 1, CType.long)
105
105
  output = "LONG2NUM(1)"
106
106
 
107
107
  assert_equal output, @ruby_to_c.process(input)
108
108
  end
109
109
 
110
110
  def test_process_lit_range
111
- input = t(:lit, 1..42, Type.range)
111
+ input = t(:lit, 1..42, CType.range)
112
112
  output = "rb_range_new(LONG2NUM(1), LONG2NUM(42), 0)"
113
113
 
114
114
  assert_equal output, @ruby_to_c.process(input)
115
115
  end
116
116
 
117
117
  def test_process_lit_range_exc
118
- input = t(:lit, 1...42, Type.range)
118
+ input = t(:lit, 1...42, CType.range)
119
119
  output = "rb_range_new(LONG2NUM(1), LONG2NUM(42), 1)"
120
120
 
121
121
  assert_equal output, @ruby_to_c.process(input)
122
122
  end
123
123
 
124
124
  def test_process_lit_regexp
125
- input = t(:lit, /x/, Type.regexp)
125
+ input = t(:lit, /x/, CType.regexp)
126
126
  output = "rb_reg_new(\"x\", 1, 0)"
127
127
 
128
128
  assert_equal output, @ruby_to_c.process(input)
129
129
  end
130
130
 
131
131
  def test_process_lit_regexp_i
132
- input = t(:lit, /x|y/i, Type.regexp)
132
+ input = t(:lit, /x|y/i, CType.regexp)
133
133
  output = "rb_reg_new(\"x|y\", 3, 1)"
134
134
 
135
135
  assert_equal output, @ruby_to_c.process(input)
136
136
  end
137
137
 
138
138
  def test_process_lit_sym
139
- input = t(:lit, :sym, Type.symbol)
139
+ input = t(:lit, :sym, CType.symbol)
140
140
  output = "ID2SYM(rb_intern(\"sym\"))"
141
141
 
142
142
  assert_equal output, @ruby_to_c.process(input)
143
143
  end
144
144
 
145
145
  def test_process_xstr
146
- input = t(:xstr, 'touch 5', Type.str)
146
+ input = t(:xstr, 'touch 5', CType.str)
147
147
  output = 'rb_funcall(rb_mKernel, rb_intern("`"), 1, rb_str_new2("touch 5"))'
148
148
 
149
149
  assert_equal output, @ruby_to_c.process(input)