RubyToC 1.0.0.4 → 1.0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +15 -0
- data/Makefile +7 -2
- data/Manifest.txt +18 -88
- data/README.txt +2 -1
- data/Rakefile +52 -0
- data/bin/ruby_to_c_show +97 -0
- data/{validate.sh → bin/ruby_to_c_validate} +0 -0
- data/{rewriter.rb → lib/rewriter.rb} +19 -5
- data/{ruby_to_c.rb → lib/ruby_to_ansi_c.rb} +71 -80
- data/lib/ruby_to_ruby_c.rb +167 -0
- data/{support.rb → lib/support.rb} +20 -19
- data/{type_checker.rb → lib/type_checker.rb} +17 -11
- data/{typed_sexp_processor.rb → lib/typed_sexp_processor.rb} +30 -30
- data/test/r2ctestcase.rb +1261 -0
- data/{test_all.rb → test/test_all.rb} +0 -0
- data/{test_extras.rb → test/test_extras.rb} +59 -61
- data/{test_rewriter.rb → test/test_rewriter.rb} +38 -38
- data/{test_ruby_to_c.rb → test/test_ruby_to_ansi_c.rb} +128 -173
- data/test/test_ruby_to_ruby_c.rb +27 -0
- data/{test_support.rb → test/test_support.rb} +80 -43
- data/{test_type_checker.rb → test/test_type_checker.rb} +121 -130
- data/{test_typed_sexp_processor.rb → test/test_typed_sexp_processor.rb} +70 -65
- metadata +55 -48
- data/rewrite.rb +0 -32
- data/translate.rb +0 -31
- data/type.rb +0 -33
- data/zcomparable.rb +0 -300
File without changes
|
@@ -34,59 +34,25 @@ class TestExtraTypeChecker < Test::Unit::TestCase # ZenTest SKIP
|
|
34
34
|
end
|
35
35
|
|
36
36
|
# HACK: this shouldn't be in test code. use augment or something
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
result = []
|
41
|
-
sexp.each do | sub_exp|
|
42
|
-
result << @type_checker.process(@rewriter.process(sub_exp))
|
43
|
-
end
|
44
|
-
return result
|
45
|
-
end
|
46
|
-
|
47
|
-
def util_unify_function
|
48
|
-
a = Type.function(Type.unknown, [ Type.unknown ], Type.unknown)
|
49
|
-
b = Type.function(Type.long, [ Type.str ], Type.void)
|
50
|
-
a.unify b
|
51
|
-
act = a.list_type
|
52
|
-
bct = b.list_type
|
53
|
-
return act, bct
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_unify_function_whole
|
57
|
-
act, bct = util_unify_function
|
58
|
-
assert_equal act, bct
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_unify_function_receiver
|
62
|
-
act, bct = util_unify_function
|
63
|
-
assert_equal act.receiver_type.list_type, bct.receiver_type.list_type
|
64
|
-
assert_equal act.receiver_type.list_type.object_id, bct.receiver_type.list_type.object_id
|
65
|
-
assert_equal act, bct
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_unify_function_args
|
69
|
-
act, bct = util_unify_function.map { |x| x.formal_types }
|
70
|
-
assert_equal act.first.list_type, bct.first.list_type
|
71
|
-
assert_equal act.first.list_type.object_id, bct.first.list_type.object_id
|
72
|
-
end
|
37
|
+
def test_process_defn_return_val
|
38
|
+
ignore = util_process(RandomCode, :meth_a)
|
39
|
+
result = util_process(RandomCode, :meth_b).first
|
73
40
|
|
74
|
-
|
75
|
-
|
76
|
-
assert_equal
|
77
|
-
assert_equal act.return_type.list_type.object_id, bct.return_type.list_type.object_id
|
41
|
+
assert_equal(:meth_b, result[1])
|
42
|
+
# FIX: this is the worst API in my codebase - demeter
|
43
|
+
assert_equal(Type.void, result.sexp_type.list_type.return_type)
|
78
44
|
end
|
79
45
|
|
80
46
|
def test_type_inference_across_args_known
|
81
47
|
generic = util_process(RandomCode, :generic_method).first
|
82
|
-
# puts
|
83
|
-
# pp @type_checker.functions
|
48
|
+
# puts
|
49
|
+
# pp @type_checker.functions
|
84
50
|
specific = util_process(RandomCode, :specific_method).first
|
85
|
-
# puts
|
86
|
-
# pp @type_checker.functions
|
51
|
+
# puts
|
52
|
+
# pp @type_checker.functions
|
87
53
|
|
88
|
-
# pp generic
|
89
|
-
# pp specific
|
54
|
+
# pp generic
|
55
|
+
# pp specific
|
90
56
|
|
91
57
|
args_g = generic[2] # FIX FUCK this is horrid
|
92
58
|
args_s = specific[2] # FIX FUCK this is horrid
|
@@ -104,15 +70,15 @@ class TestExtraTypeChecker < Test::Unit::TestCase # ZenTest SKIP
|
|
104
70
|
args_s[1].sexp_type.list_type.object_id,
|
105
71
|
"#specific_method's x and #generic_method's x are unified")
|
106
72
|
|
107
|
-
# assert_equal(args_g[2].sexp_type.list_type.object_id,
|
108
|
-
# args_s[2].sexp_type.list_type.object_id,
|
109
|
-
# "#specific_method's y and #generic_method's y are unified")
|
73
|
+
# assert_equal(args_g[2].sexp_type.list_type.object_id,
|
74
|
+
# args_s[2].sexp_type.list_type.object_id,
|
75
|
+
# "#specific_method's y and #generic_method's y are unified")
|
110
76
|
|
111
|
-
# assert_equal(Type.long, args_s[2].sexp_type,
|
112
|
-
# "#specific_method's y is a Long")
|
113
|
-
# assert_equal(Type.long, args_g[2].sexp_type,
|
114
|
-
# "#generic_method's y is a Long")
|
115
|
-
end
|
77
|
+
# assert_equal(Type.long, args_s[2].sexp_type,
|
78
|
+
# "#specific_method's y is a Long")
|
79
|
+
# assert_equal(Type.long, args_g[2].sexp_type,
|
80
|
+
# "#generic_method's y is a Long")
|
81
|
+
end
|
116
82
|
|
117
83
|
def test_type_inference_across_args_unknown
|
118
84
|
meth_a = util_process(RandomCode, :meth_a).first
|
@@ -130,14 +96,46 @@ end
|
|
130
96
|
"#meth_a and meth_b arguments are unified by object_id")
|
131
97
|
end
|
132
98
|
|
133
|
-
def
|
134
|
-
|
135
|
-
|
99
|
+
def test_unify_function_args
|
100
|
+
act, bct = util_unify_function.map { |x| x.formal_types }
|
101
|
+
assert_equal act.first.list_type, bct.first.list_type
|
102
|
+
assert_equal act.first.list_type.object_id, bct.first.list_type.object_id
|
103
|
+
end
|
136
104
|
|
137
|
-
|
138
|
-
|
139
|
-
assert_equal
|
105
|
+
def test_unify_function_receiver
|
106
|
+
act, bct = util_unify_function
|
107
|
+
assert_equal act.receiver_type.list_type, bct.receiver_type.list_type
|
108
|
+
assert_equal act.receiver_type.list_type.object_id, bct.receiver_type.list_type.object_id
|
109
|
+
assert_equal act, bct
|
140
110
|
end
|
141
111
|
|
142
|
-
|
112
|
+
def test_unify_function_return
|
113
|
+
act, bct = util_unify_function
|
114
|
+
assert_equal act.return_type.list_type, bct.return_type.list_type
|
115
|
+
assert_equal act.return_type.list_type.object_id, bct.return_type.list_type.object_id
|
116
|
+
end
|
143
117
|
|
118
|
+
def test_unify_function_whole
|
119
|
+
act, bct = util_unify_function
|
120
|
+
assert_equal act, bct
|
121
|
+
end
|
122
|
+
|
123
|
+
def util_process(klass, meth)
|
124
|
+
sexp = @parser.parse_tree_for_method klass, meth
|
125
|
+
sexp = [sexp] unless meth.nil?
|
126
|
+
result = []
|
127
|
+
sexp.each do | sub_exp|
|
128
|
+
result << @type_checker.process(@rewriter.process(sub_exp))
|
129
|
+
end
|
130
|
+
return result
|
131
|
+
end
|
132
|
+
|
133
|
+
def util_unify_function
|
134
|
+
a = Type.function(Type.unknown, [ Type.unknown ], Type.unknown)
|
135
|
+
b = Type.function(Type.long, [ Type.str ], Type.void)
|
136
|
+
a.unify b
|
137
|
+
act = a.list_type
|
138
|
+
bct = b.list_type
|
139
|
+
return act, bct
|
140
|
+
end
|
141
|
+
end
|
@@ -22,40 +22,6 @@ class TestRewriter < R2CTestCase
|
|
22
22
|
assert_equal expect, @rewrite.process(input)
|
23
23
|
end
|
24
24
|
|
25
|
-
def test_process_defn_block
|
26
|
-
input = [:defn, :meth, [:scope, [:block, [:args], [:return, [:nil]]]]]
|
27
|
-
output = [:defn, :meth, [:args], [:scope, [:block, [:return, [:nil]]]]]
|
28
|
-
|
29
|
-
assert_equal output, @rewrite.process(input)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_process_defn_ivar
|
33
|
-
input = [:defn, :name, [:ivar, :@name]]
|
34
|
-
output = [:defn, :name, [:args], [:scope, [:block, [:return, [:ivar, :@name]]]]]
|
35
|
-
|
36
|
-
assert_equal output, @rewrite.process(input)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_process_defn_attrset
|
40
|
-
input = [:defn, :meth, [:attrset, :@name]]
|
41
|
-
output = [:defn, :meth, [:args, :arg], [:scope, [:block, [:return, [:iasgn, :@name, [:lvar, :arg]]]]]]
|
42
|
-
|
43
|
-
assert_equal output, @rewrite.process(input)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_process_fcall
|
47
|
-
input = [:fcall, :puts, [:array, [:lit, 1]]]
|
48
|
-
expect = [:call, nil, :puts, [:arglist, [:lit, 1]]]
|
49
|
-
assert_equal expect, @rewrite.process(input)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_process_vcall_2
|
53
|
-
input = [:vcall, :method]
|
54
|
-
output = [:call, nil, :method, nil]
|
55
|
-
|
56
|
-
assert_equal output, @rewrite.process(input)
|
57
|
-
end
|
58
|
-
|
59
25
|
def test_process_case
|
60
26
|
input = [:case,
|
61
27
|
[:lvar, :var],
|
@@ -123,6 +89,33 @@ class TestRewriter < R2CTestCase
|
|
123
89
|
assert_equal expected, @rewrite.process(input)
|
124
90
|
end
|
125
91
|
|
92
|
+
def test_process_defn_attrset
|
93
|
+
input = [:defn, :meth, [:attrset, :@name]]
|
94
|
+
output = [:defn, :meth, [:args, :arg], [:scope, [:block, [:return, [:iasgn, :@name, [:lvar, :arg]]]]]]
|
95
|
+
|
96
|
+
assert_equal output, @rewrite.process(input)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_process_defn_block
|
100
|
+
input = [:defn, :meth, [:scope, [:block, [:args], [:return, [:nil]]]]]
|
101
|
+
output = [:defn, :meth, [:args], [:scope, [:block, [:return, [:nil]]]]]
|
102
|
+
|
103
|
+
assert_equal output, @rewrite.process(input)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_process_defn_ivar
|
107
|
+
input = [:defn, :name, [:ivar, :@name]]
|
108
|
+
output = [:defn, :name, [:args], [:scope, [:block, [:return, [:ivar, :@name]]]]]
|
109
|
+
|
110
|
+
assert_equal output, @rewrite.process(input)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_process_fcall
|
114
|
+
input = [:fcall, :puts, [:array, [:lit, 1]]]
|
115
|
+
expect = [:call, nil, :puts, [:arglist, [:lit, 1]]]
|
116
|
+
assert_equal expect, @rewrite.process(input)
|
117
|
+
end
|
118
|
+
|
126
119
|
def test_process_iter
|
127
120
|
input = [:iter,
|
128
121
|
[:call, [:lit, 3], :downto, [:array, [:lit, 1]]],
|
@@ -152,7 +145,7 @@ class TestRewriter < R2CTestCase
|
|
152
145
|
assert_equal expected, @rewrite.process(input)
|
153
146
|
end
|
154
147
|
|
155
|
-
def
|
148
|
+
def test_process_iter_downto_nested
|
156
149
|
input = [:block,
|
157
150
|
[:iter,
|
158
151
|
[:call, [:lvar, :n], :downto, [:array, [:lit, 0]]],
|
@@ -186,7 +179,7 @@ class TestRewriter < R2CTestCase
|
|
186
179
|
assert_equal expected, @rewrite.process(input)
|
187
180
|
end
|
188
181
|
|
189
|
-
def
|
182
|
+
def test_process_iter_upto_nested
|
190
183
|
input = [:block,
|
191
184
|
[:iter,
|
192
185
|
[:call, [:lvar, :n], :upto, [:array, [:lit, 0]]],
|
@@ -244,6 +237,13 @@ class TestRewriter < R2CTestCase
|
|
244
237
|
assert_equal output, @rewrite.process(input)
|
245
238
|
end
|
246
239
|
|
240
|
+
def test_process_vcall_2
|
241
|
+
input = [:vcall, :method]
|
242
|
+
output = [:call, nil, :method, nil]
|
243
|
+
|
244
|
+
assert_equal output, @rewrite.process(input)
|
245
|
+
end
|
246
|
+
|
247
247
|
def test_process_when
|
248
248
|
input = [:when, [:array, [:lit, 1]], [:str, "1"]]
|
249
249
|
|
@@ -260,7 +260,7 @@ class TestR2CRewriter < R2CTestCase
|
|
260
260
|
@rewrite = R2CRewriter.new
|
261
261
|
end
|
262
262
|
|
263
|
-
def
|
263
|
+
def xtest_process_call_rewritten
|
264
264
|
|
265
265
|
input = t(:call,
|
266
266
|
t(:str, "this", Type.str),
|
@@ -278,7 +278,7 @@ class TestR2CRewriter < R2CTestCase
|
|
278
278
|
assert_equal expected, @rewrite.process(input)
|
279
279
|
end
|
280
280
|
|
281
|
-
def
|
281
|
+
def xtest_process_call_same
|
282
282
|
|
283
283
|
input = t(:call,
|
284
284
|
t(:lit, 1, Type.long),
|
@@ -3,67 +3,55 @@
|
|
3
3
|
$TESTING = true
|
4
4
|
|
5
5
|
require 'test/unit'
|
6
|
-
require '
|
6
|
+
require 'ruby_to_ansi_c'
|
7
7
|
require 'r2ctestcase'
|
8
8
|
|
9
|
-
class
|
9
|
+
class TestRubyToAnsiC < R2CTestCase
|
10
10
|
|
11
|
-
def
|
12
|
-
|
11
|
+
def setup
|
12
|
+
@ruby_to_c = RubyToAnsiC.new
|
13
|
+
@ruby_to_c.env.extend
|
14
|
+
@processor = @ruby_to_c
|
13
15
|
end
|
14
16
|
|
15
|
-
def
|
16
|
-
assert_equal "
|
17
|
+
def test_c_type_bool
|
18
|
+
assert_equal "bool", @ruby_to_c.class.c_type(Type.bool)
|
17
19
|
end
|
18
20
|
|
19
|
-
def
|
20
|
-
assert_equal "
|
21
|
+
def test_c_type_float
|
22
|
+
assert_equal "double", @ruby_to_c.class.c_type(Type.float)
|
21
23
|
end
|
22
24
|
|
23
|
-
def
|
24
|
-
assert_equal "
|
25
|
+
def test_c_type_long
|
26
|
+
assert_equal "long", @ruby_to_c.class.c_type(Type.long)
|
25
27
|
end
|
26
28
|
|
27
|
-
def
|
28
|
-
assert_equal "
|
29
|
+
def test_c_type_long_list
|
30
|
+
assert_equal "long *", @ruby_to_c.class.c_type(Type.long_list)
|
29
31
|
end
|
30
32
|
|
31
|
-
def
|
32
|
-
assert_equal "
|
33
|
+
def test_c_type_str
|
34
|
+
assert_equal "str", @ruby_to_c.class.c_type(Type.str)
|
33
35
|
end
|
34
36
|
|
35
|
-
def
|
36
|
-
assert_equal "
|
37
|
+
def test_c_type_str_list
|
38
|
+
assert_equal "str *", @ruby_to_c.class.c_type(Type.str_list)
|
37
39
|
end
|
38
40
|
|
39
41
|
def test_c_type_symbol
|
40
|
-
assert_equal "symbol",
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_c_type_value
|
44
|
-
assert_equal "VALUE", TypeMap.c_type(Type.value)
|
42
|
+
assert_equal "symbol", @ruby_to_c.class.c_type(Type.symbol)
|
45
43
|
end
|
46
44
|
|
47
45
|
def test_c_type_unknown
|
48
|
-
assert_equal "
|
46
|
+
assert_equal "void *", @ruby_to_c.class.c_type(Type.unknown)
|
49
47
|
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class TestRubyToC < R2CTestCase
|
53
48
|
|
54
|
-
def
|
55
|
-
@ruby_to_c
|
56
|
-
@ruby_to_c.env.extend
|
57
|
-
@processor = @ruby_to_c
|
49
|
+
def test_c_type_value
|
50
|
+
assert_equal "void *", @ruby_to_c.class.c_type(Type.value)
|
58
51
|
end
|
59
52
|
|
60
|
-
def
|
61
|
-
|
62
|
-
input = [:class, :Suck, :Object,
|
63
|
-
[:defn, :something, [:scope, [:block, [:args], [:fcall, :"whaaa\?"]]]],
|
64
|
-
[:defn, :foo, [:scope, [:block, [:args], [:vcall, :something]]]]]
|
65
|
-
expected = "// class Suck\n\n// ERROR: NoMethodError: undefined method `[]=' for nil:NilClass\n\nvoid\nfoo() {\nsomething();\n}"
|
66
|
-
assert_equal expected, RubyToC.translator.process(input)
|
53
|
+
def test_c_type_void
|
54
|
+
assert_equal "void", @ruby_to_c.class.c_type(Type.void)
|
67
55
|
end
|
68
56
|
|
69
57
|
def test_env
|
@@ -71,17 +59,6 @@ class TestRubyToC < R2CTestCase
|
|
71
59
|
assert_kind_of Environment, @ruby_to_c.env
|
72
60
|
end
|
73
61
|
|
74
|
-
def test_prototypes
|
75
|
-
assert_equal [], @ruby_to_c.prototypes
|
76
|
-
@ruby_to_c.process t(:defn,
|
77
|
-
:empty,
|
78
|
-
t(:args),
|
79
|
-
t(:scope),
|
80
|
-
Type.function([], Type.void))
|
81
|
-
|
82
|
-
assert_equal "void empty();\n", @ruby_to_c.prototypes.first
|
83
|
-
end
|
84
|
-
|
85
62
|
def test_process_and
|
86
63
|
input = t(:and, t(:lit, 1, Type.long), t(:lit, 2, Type.long))
|
87
64
|
output = "1 && 2"
|
@@ -89,6 +66,13 @@ class TestRubyToC < R2CTestCase
|
|
89
66
|
assert_equal output, @ruby_to_c.process(input)
|
90
67
|
end
|
91
68
|
|
69
|
+
def test_process_args_empty
|
70
|
+
input = t(:args)
|
71
|
+
output = "()"
|
72
|
+
|
73
|
+
assert_equal output, @ruby_to_c.process(input)
|
74
|
+
end
|
75
|
+
|
92
76
|
def test_process_args_normal
|
93
77
|
input = t(:args,
|
94
78
|
t(:foo, Type.long),
|
@@ -98,16 +82,18 @@ class TestRubyToC < R2CTestCase
|
|
98
82
|
assert_equal output, @ruby_to_c.process(input)
|
99
83
|
end
|
100
84
|
|
101
|
-
def
|
102
|
-
input
|
103
|
-
output = "()"
|
85
|
+
def test_process_array_empty
|
86
|
+
input = t(:array)
|
87
|
+
output = "rb_ary_new()"
|
104
88
|
|
105
89
|
assert_equal output, @ruby_to_c.process(input)
|
106
90
|
end
|
107
91
|
|
108
|
-
def
|
109
|
-
input = t(:array
|
110
|
-
|
92
|
+
def test_process_array_multiple
|
93
|
+
input = t(:array,
|
94
|
+
t(:lvar, :arg1, Type.long),
|
95
|
+
t(:lvar, :arg2, Type.long))
|
96
|
+
output = "arg1, arg2"
|
111
97
|
|
112
98
|
assert_equal output, @ruby_to_c.process(input)
|
113
99
|
end
|
@@ -120,11 +106,18 @@ class TestRubyToC < R2CTestCase
|
|
120
106
|
assert_equal output, @ruby_to_c.process(input)
|
121
107
|
end
|
122
108
|
|
123
|
-
def
|
124
|
-
input = t(:
|
125
|
-
|
126
|
-
|
127
|
-
output
|
109
|
+
def test_process_block
|
110
|
+
input = t(:block, t(:return, t(:nil)))
|
111
|
+
output = "return NULL;\n"
|
112
|
+
|
113
|
+
assert_equal output, @ruby_to_c.process(input)
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_process_block_multiple
|
117
|
+
input = t(:block,
|
118
|
+
t(:str, "foo"),
|
119
|
+
t(:return, t(:nil)))
|
120
|
+
output = "\"foo\";\nreturn NULL;\n"
|
128
121
|
|
129
122
|
assert_equal output, @ruby_to_c.process(input)
|
130
123
|
end
|
@@ -153,23 +146,12 @@ class TestRubyToC < R2CTestCase
|
|
153
146
|
assert_equal output, @ruby_to_c.process(input)
|
154
147
|
end
|
155
148
|
|
156
|
-
def test_process_call_rhs
|
157
|
-
input = t(:call,
|
158
|
-
nil,
|
159
|
-
:name,
|
160
|
-
t(:array,
|
161
|
-
t(:str, "foo")))
|
162
|
-
output = "name(\"foo\")"
|
163
|
-
|
164
|
-
assert_equal output, @ruby_to_c.process(input)
|
165
|
-
end
|
166
|
-
|
167
149
|
def test_process_call_nil?
|
168
150
|
input = t(:call,
|
169
151
|
t(:lvar, :arg, Type.long),
|
170
152
|
:nil?,
|
171
153
|
nil)
|
172
|
-
output = "
|
154
|
+
output = "arg"
|
173
155
|
|
174
156
|
assert_equal output, @ruby_to_c.process(input)
|
175
157
|
end
|
@@ -188,18 +170,13 @@ class TestRubyToC < R2CTestCase
|
|
188
170
|
end
|
189
171
|
end
|
190
172
|
|
191
|
-
def
|
192
|
-
input = t(:
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
def test_process_block_multiple
|
199
|
-
input = t(:block,
|
200
|
-
t(:str, "foo"),
|
201
|
-
t(:return, t(:nil)))
|
202
|
-
output = "\"foo\";\nreturn Qnil;\n"
|
173
|
+
def test_process_call_rhs
|
174
|
+
input = t(:call,
|
175
|
+
nil,
|
176
|
+
:name,
|
177
|
+
t(:array,
|
178
|
+
t(:str, "foo")))
|
179
|
+
output = "name(\"foo\")"
|
203
180
|
|
204
181
|
assert_equal output, @ruby_to_c.process(input)
|
205
182
|
end
|
@@ -241,17 +218,6 @@ class TestRubyToC < R2CTestCase
|
|
241
218
|
assert_equal output, @ruby_to_c.process(input)
|
242
219
|
end
|
243
220
|
|
244
|
-
def disabled_test_dstr
|
245
|
-
input = t(:dstr,
|
246
|
-
"var is ",
|
247
|
-
t(:lvar, :var),
|
248
|
-
t(:str, ". So there."))
|
249
|
-
output = "sprintf stuff goes here"
|
250
|
-
|
251
|
-
flunk "Way too hard right now"
|
252
|
-
assert_equal output, @ruby_to_c.process(input)
|
253
|
-
end
|
254
|
-
|
255
221
|
def test_process_dvar
|
256
222
|
input = t(:dvar, :dvar, Type.long)
|
257
223
|
output = "dvar"
|
@@ -261,7 +227,7 @@ class TestRubyToC < R2CTestCase
|
|
261
227
|
|
262
228
|
def test_process_false
|
263
229
|
input = t(:false)
|
264
|
-
output = "
|
230
|
+
output = "0"
|
265
231
|
|
266
232
|
assert_equal output, @ruby_to_c.process(input)
|
267
233
|
end
|
@@ -296,19 +262,6 @@ class TestRubyToC < R2CTestCase
|
|
296
262
|
assert_equal output, @ruby_to_c.process(input)
|
297
263
|
end
|
298
264
|
|
299
|
-
def test_process_if_else
|
300
|
-
input = t(:if,
|
301
|
-
t(:call,
|
302
|
-
t(:lit, 1, Type.long),
|
303
|
-
:==,
|
304
|
-
t(:array, t(:lit, 2, Type.long))),
|
305
|
-
t(:str, "not equal"),
|
306
|
-
t(:str, "equal"))
|
307
|
-
output = "if (1 == 2) {\n\"not equal\";\n} else {\n\"equal\";\n}"
|
308
|
-
|
309
|
-
assert_equal output, @ruby_to_c.process(input)
|
310
|
-
end
|
311
|
-
|
312
265
|
def test_process_if_block
|
313
266
|
input = t(:if,
|
314
267
|
t(:call,
|
@@ -324,29 +277,15 @@ class TestRubyToC < R2CTestCase
|
|
324
277
|
assert_equal output, @ruby_to_c.process(input)
|
325
278
|
end
|
326
279
|
|
327
|
-
def
|
328
|
-
|
329
|
-
input = t(:iter,
|
330
|
-
t(:call,
|
331
|
-
t(:lvar, :array, var_type),
|
332
|
-
:each,
|
333
|
-
nil),
|
334
|
-
t(:dasgn_curr, :x, Type.long),
|
280
|
+
def test_process_if_else
|
281
|
+
input = t(:if,
|
335
282
|
t(:call,
|
336
|
-
|
337
|
-
|
338
|
-
t(:array,
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
Type.long),
|
343
|
-
:to_s,
|
344
|
-
nil))))
|
345
|
-
output = "unsigned long index_x;
|
346
|
-
for (index_x = 0; index_x < array.length; ++index_x) {
|
347
|
-
long x = array.contents[index_x];
|
348
|
-
puts(to_s(x));
|
349
|
-
}"
|
283
|
+
t(:lit, 1, Type.long),
|
284
|
+
:==,
|
285
|
+
t(:array, t(:lit, 2, Type.long))),
|
286
|
+
t(:str, "not equal"),
|
287
|
+
t(:str, "equal"))
|
288
|
+
output = "if (1 == 2) {\n\"not equal\";\n} else {\n\"equal\";\n}"
|
350
289
|
|
351
290
|
assert_equal output, @ruby_to_c.process(input)
|
352
291
|
end
|
@@ -366,21 +305,6 @@ puts(to_s(x));
|
|
366
305
|
assert_equal output, @ruby_to_c.process(input)
|
367
306
|
end
|
368
307
|
|
369
|
-
def test_process_lasgn_array
|
370
|
-
input = t(:lasgn,
|
371
|
-
:var,
|
372
|
-
t(:array,
|
373
|
-
t(:str, "foo", Type.str),
|
374
|
-
t(:str, "bar", Type.str)),
|
375
|
-
Type.str_list)
|
376
|
-
output = "var.length = 2;
|
377
|
-
var.contents = (str*) malloc(sizeof(str) * var.length);
|
378
|
-
var.contents[0] = \"foo\";
|
379
|
-
var.contents[1] = \"bar\""
|
380
|
-
|
381
|
-
assert_equal output, @ruby_to_c.process(input)
|
382
|
-
end
|
383
|
-
|
384
308
|
def test_process_lit_float
|
385
309
|
input = t(:lit, 1.0, Type.float)
|
386
310
|
output = "1.0"
|
@@ -397,7 +321,7 @@ var.contents[1] = \"bar\""
|
|
397
321
|
|
398
322
|
def test_process_lit_sym
|
399
323
|
input = t(:lit, :sym, Type.symbol)
|
400
|
-
output = "
|
324
|
+
output = "\"sym\""
|
401
325
|
|
402
326
|
assert_equal output, @ruby_to_c.process(input)
|
403
327
|
end
|
@@ -411,14 +335,14 @@ var.contents[1] = \"bar\""
|
|
411
335
|
|
412
336
|
def test_process_nil
|
413
337
|
input = t(:nil)
|
414
|
-
output = "
|
338
|
+
output = "NULL"
|
415
339
|
|
416
340
|
assert_equal output, @ruby_to_c.process(input)
|
417
341
|
end
|
418
342
|
|
419
343
|
def test_process_not
|
420
344
|
input = t(:not, t(:true, Type.bool), Type.bool)
|
421
|
-
output = "!(
|
345
|
+
output = "!(1)"
|
422
346
|
|
423
347
|
assert_equal output, @ruby_to_c.process(input)
|
424
348
|
end
|
@@ -432,29 +356,7 @@ var.contents[1] = \"bar\""
|
|
432
356
|
|
433
357
|
def test_process_return
|
434
358
|
input = t(:return, t(:nil))
|
435
|
-
output = "return
|
436
|
-
|
437
|
-
assert_equal output, @ruby_to_c.process(input)
|
438
|
-
end
|
439
|
-
|
440
|
-
def test_process_str
|
441
|
-
input = t(:str, "foo", Type.str)
|
442
|
-
output = "\"foo\""
|
443
|
-
|
444
|
-
assert_equal output, @ruby_to_c.process(input)
|
445
|
-
end
|
446
|
-
|
447
|
-
def test_process_str_multi
|
448
|
-
input = t(:str, "foo
|
449
|
-
bar", Type.str)
|
450
|
-
output = "\"foo\\nbar\""
|
451
|
-
|
452
|
-
assert_equal output, @ruby_to_c.process(input)
|
453
|
-
end
|
454
|
-
|
455
|
-
def test_process_str_backslashed
|
456
|
-
input = t(:str, "foo\nbar", Type.str)
|
457
|
-
output = "\"foo\\nbar\""
|
359
|
+
output = "return NULL"
|
458
360
|
|
459
361
|
assert_equal output, @ruby_to_c.process(input)
|
460
362
|
end
|
@@ -463,7 +365,7 @@ bar", Type.str)
|
|
463
365
|
input = t(:scope,
|
464
366
|
t(:block,
|
465
367
|
t(:return, t(:nil))))
|
466
|
-
output = "{\nreturn
|
368
|
+
output = "{\nreturn NULL;\n}"
|
467
369
|
|
468
370
|
assert_equal output, @ruby_to_c.process(input)
|
469
371
|
end
|
@@ -481,14 +383,36 @@ bar", Type.str)
|
|
481
383
|
t(:str, "declare me"),
|
482
384
|
Type.str),
|
483
385
|
t(:return, t(:nil))))
|
484
|
-
output = "{\nstr arg;\narg = \"declare me\";\nreturn
|
386
|
+
output = "{\nstr arg;\narg = \"declare me\";\nreturn NULL;\n}"
|
387
|
+
|
388
|
+
assert_equal output, @ruby_to_c.process(input)
|
389
|
+
end
|
390
|
+
|
391
|
+
def test_process_str
|
392
|
+
input = t(:str, "foo", Type.str)
|
393
|
+
output = "\"foo\""
|
394
|
+
|
395
|
+
assert_equal output, @ruby_to_c.process(input)
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_process_str_backslashed
|
399
|
+
input = t(:str, "foo\nbar", Type.str)
|
400
|
+
output = "\"foo\\nbar\""
|
401
|
+
|
402
|
+
assert_equal output, @ruby_to_c.process(input)
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_process_str_multi
|
406
|
+
input = t(:str, "foo
|
407
|
+
bar", Type.str)
|
408
|
+
output = "\"foo\\nbar\""
|
485
409
|
|
486
410
|
assert_equal output, @ruby_to_c.process(input)
|
487
411
|
end
|
488
412
|
|
489
413
|
def test_process_true
|
490
414
|
input = t(:true)
|
491
|
-
output = "
|
415
|
+
output = "1"
|
492
416
|
|
493
417
|
assert_equal output, @ruby_to_c.process(input)
|
494
418
|
end
|
@@ -530,4 +454,35 @@ bar", Type.str)
|
|
530
454
|
|
531
455
|
assert_equal expected, @ruby_to_c.process(input)
|
532
456
|
end
|
457
|
+
|
458
|
+
def test_prototypes
|
459
|
+
assert_equal [], @ruby_to_c.prototypes
|
460
|
+
@ruby_to_c.process t(:defn,
|
461
|
+
:empty,
|
462
|
+
t(:args),
|
463
|
+
t(:scope),
|
464
|
+
Type.function([], Type.void))
|
465
|
+
|
466
|
+
assert_equal "void empty();\n", @ruby_to_c.prototypes.first
|
467
|
+
end
|
468
|
+
|
469
|
+
def test_translator
|
470
|
+
Object.class_eval "class Suck; end"
|
471
|
+
input = [:class, :Suck, :Object,
|
472
|
+
[:defn, :something, [:scope, [:block, [:args], [:fcall, :"whaaa\?"]]]],
|
473
|
+
[:defn, :foo, [:scope, [:block, [:args], [:vcall, :something]]]]]
|
474
|
+
expected = "// class Suck\n\nvoid\nsomething() {\nwhaaa?();\n}\n\nvoid\nfoo() {\nsomething();\n}"
|
475
|
+
assert_equal expected, RubyToAnsiC.translator.process(input)
|
476
|
+
end
|
477
|
+
|
478
|
+
def disabled_test_dstr
|
479
|
+
input = t(:dstr,
|
480
|
+
"var is ",
|
481
|
+
t(:lvar, :var),
|
482
|
+
t(:str, ". So there."))
|
483
|
+
output = "sprintf stuff goes here"
|
484
|
+
|
485
|
+
flunk "Way too hard right now"
|
486
|
+
assert_equal output, @ruby_to_c.process(input)
|
487
|
+
end
|
533
488
|
end
|