cast 0.0.1 → 0.3.1

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.
@@ -0,0 +1,11 @@
1
+ module C
2
+ VERSION = [0, 3, 1]
3
+
4
+ class << VERSION
5
+ include Comparable
6
+
7
+ def to_s
8
+ join('.')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,224 @@
1
+ ######################################################################
2
+ #
3
+ # Tests for miscellaneous methods specific to individual Node classes.
4
+ #
5
+ ######################################################################
6
+
7
+ require 'test_helper'
8
+
9
+ class MiscTests < Minitest::Test
10
+
11
+ # ------------------------------------------------------------------
12
+ # Declarator#declaration type
13
+ # ------------------------------------------------------------------
14
+
15
+ def test_declarator_declaration
16
+ tor = C::Declarator.new(nil, 'x')
17
+ assert_nil(tor.declaration)
18
+
19
+ list = C::NodeArray[tor]
20
+ assert_nil(tor.declaration)
21
+
22
+ tion = C::Declaration.new(C::Int.new, list)
23
+ assert_same(tion, tor.declaration)
24
+
25
+ list.detach
26
+ assert_nil(tor.declaration)
27
+ end
28
+
29
+ def test_declarator_type
30
+ # int i, *j, k[], l(), *m[10];
31
+ decl = C::Declaration.new(C::Int.new)
32
+ decl.declarators << C::Declarator.new(nil, 'i')
33
+ decl.declarators << C::Declarator.new(C::Pointer.new, 'j')
34
+ decl.declarators << C::Declarator.new(C::Array.new, 'k')
35
+ decl.declarators << C::Declarator.new(C::Function.new, 'l')
36
+ arr = C::Array.new(C::Pointer.new, C::IntLiteral.new(10))
37
+ decl.declarators << C::Declarator.new(arr, 'm')
38
+
39
+ assert_equal_inspect_strs(decl.declarators[0].type.inspect, <<EOS)
40
+ Int
41
+ EOS
42
+ assert_equal_inspect_strs(decl.declarators[1].type.inspect, <<EOS)
43
+ Pointer
44
+ type: Int
45
+ EOS
46
+ assert_equal_inspect_strs(decl.declarators[2].type.inspect, <<EOS)
47
+ Array
48
+ type: Int
49
+ EOS
50
+ assert_equal_inspect_strs(decl.declarators[3].type.inspect, <<EOS)
51
+ Function
52
+ type: Int
53
+ EOS
54
+ assert_equal_inspect_strs(decl.declarators[4].type.inspect, <<EOS)
55
+ Array
56
+ type: Pointer
57
+ type: Int
58
+ length: IntLiteral
59
+ val: 10
60
+ EOS
61
+ end
62
+
63
+ # ------------------------------------------------------------------
64
+ # FunctionDef
65
+ # ------------------------------------------------------------------
66
+
67
+ def test_function_def_initialize
68
+ fd = C::FunctionDef.new
69
+ assert fd.def.Block?
70
+ assert fd.def.stmts.empty?
71
+ end
72
+
73
+ # ------------------------------------------------------------------
74
+ # BlockExpression
75
+ # ------------------------------------------------------------------
76
+
77
+ def test_function_def_initialize
78
+ expr = C::BlockExpression.new
79
+ assert expr.block.Block?
80
+ assert expr.block.stmts.empty?
81
+ end
82
+
83
+ # ------------------------------------------------------------------
84
+ # DirectType, IndirectType
85
+ # ------------------------------------------------------------------
86
+
87
+ def test_type_direct_type
88
+ d = C::Int.new
89
+ t = C::Pointer.new(d)
90
+ assert_same(d, t.direct_type)
91
+
92
+ d = C::Float.new
93
+ t = C::Pointer.new(C::Pointer.new(d))
94
+ assert_same(d, t.direct_type)
95
+
96
+ d = C::Struct.new('S')
97
+ t = C::Array.new(d)
98
+ assert_same(d, t.direct_type)
99
+
100
+ d = C::CustomType.new('T')
101
+ t = C::Function.new(d)
102
+ assert_same(d, t.direct_type)
103
+
104
+ t = C::Pointer.new(nil)
105
+ assert_nil(t.direct_type)
106
+
107
+ t = C::Int.new
108
+ assert_same(t, t.direct_type)
109
+ end
110
+
111
+ def test_type_indirect_type
112
+ d = C::Int.new
113
+ t = C::Pointer.new(d)
114
+ assert_equal(C::Pointer.new, t.indirect_type)
115
+
116
+ d = C::Float.new
117
+ t = C::Pointer.new(C::Pointer.new(d))
118
+ assert_equal(C::Pointer.new(C::Pointer.new), t.indirect_type)
119
+
120
+ d = C::Struct.new('S')
121
+ t = C::Array.new(d, C::IntLiteral.new(10))
122
+ assert_equal(C::Array.new(nil, C::IntLiteral.new(10)), t.indirect_type)
123
+
124
+ d = C::CustomType.new('T')
125
+ t = C::Function.new(d)
126
+ assert_equal(C::Function.new, t.indirect_type)
127
+
128
+ t = C::Pointer.new(nil)
129
+ assert_copy(t, t.indirect_type)
130
+
131
+ t = C::Int.new
132
+ assert_nil(t.indirect_type)
133
+ end
134
+
135
+ def test_type_set_direct_type
136
+ d = C::Int.new
137
+ t = C::Pointer.new(d)
138
+ x = C::Int.new
139
+ t.direct_type = x
140
+ assert_same(x, t.type)
141
+
142
+ d = C::Float.new
143
+ t = C::Pointer.new(C::Pointer.new(d))
144
+ x = C::Float.new
145
+ t.direct_type = x
146
+ assert_same(x, t.type.type)
147
+
148
+ d = C::Struct.new('S')
149
+ t = C::Array.new(d)
150
+ x = C::Struct.new('T')
151
+ t.direct_type = x
152
+ assert_same(x, t.type)
153
+
154
+ d = C::CustomType.new('T')
155
+ t = C::Function.new(d)
156
+ x = C::Void.new
157
+ t.direct_type = x
158
+ assert_same(x, t.type)
159
+
160
+ t = C::Pointer.new(nil)
161
+ x = C::Imaginary.new
162
+ t.direct_type = x
163
+ assert_same(x, t.type)
164
+
165
+ t = C::Int.new
166
+ x = C::Void.new
167
+ assert_raises(NoMethodError){t.direct_type = x}
168
+ end
169
+
170
+ # ------------------------------------------------------------------
171
+ # CharLiteral StringLiteral
172
+ # ------------------------------------------------------------------
173
+
174
+ def test_char_literal_wide
175
+ c = C::CharLiteral.new('abc', 'L')
176
+ assert(c.wide?)
177
+ assert_equal('L', c.prefix)
178
+
179
+ c.prefix = nil
180
+ assert(!c.wide?)
181
+ assert_nil(c.prefix)
182
+
183
+ c.prefix = 'x'
184
+ assert(!c.wide?)
185
+ assert_equal('x', c.prefix)
186
+
187
+ c.wide = false
188
+ assert(!c.wide?)
189
+ assert_equal('x', c.prefix)
190
+
191
+ c.wide = true
192
+ assert(c.wide?)
193
+ assert_equal('L', c.prefix)
194
+
195
+ c.wide = false
196
+ assert(!c.wide?)
197
+ assert_nil(c.prefix)
198
+ end
199
+ def test_string_literal_wide
200
+ s = C::StringLiteral.new('abc', 'L')
201
+ assert(s.wide?)
202
+ assert_equal('L', s.prefix)
203
+
204
+ s.prefix = nil
205
+ assert(!s.wide?)
206
+ assert_nil(s.prefix)
207
+
208
+ s.prefix = 'x'
209
+ assert(!s.wide?)
210
+ assert_equal('x', s.prefix)
211
+
212
+ s.wide = false
213
+ assert(!s.wide?)
214
+ assert_equal('x', s.prefix)
215
+
216
+ s.wide = true
217
+ assert(s.wide?)
218
+ assert_equal('L', s.prefix)
219
+
220
+ s.wide = false
221
+ assert(!s.wide?)
222
+ assert_nil(s.prefix)
223
+ end
224
+ end
@@ -0,0 +1,335 @@
1
+ ######################################################################
2
+ #
3
+ # Lexer routine tests. We only test nontrivial tokens, as things like
4
+ # operators are exercised sufficiently in the parser tests
5
+ # (test_parser.rb).
6
+ #
7
+ ######################################################################
8
+
9
+ require 'test_helper'
10
+
11
+ class LexerTest < Minitest::Test
12
+ def check(s)
13
+ check_ast(s){|inp| C::Parser.new.parse(inp)}
14
+ end
15
+
16
+ def test_id_and_typename
17
+ check <<EOS
18
+ typedef int Mytype;
19
+ void f() {
20
+ mytype * a;
21
+ Mytype * a;
22
+ _1234, _a1234, _1234L;
23
+ }
24
+ ----
25
+ TranslationUnit
26
+ entities:
27
+ - Declaration
28
+ storage: typedef
29
+ type: Int
30
+ declarators:
31
+ - Declarator
32
+ name: "Mytype"
33
+ - FunctionDef
34
+ type: Function
35
+ type: Void
36
+ name: "f"
37
+ def: Block
38
+ stmts:
39
+ - ExpressionStatement
40
+ expr: Multiply
41
+ expr1: Variable
42
+ name: "mytype"
43
+ expr2: Variable
44
+ name: "a"
45
+ - Declaration
46
+ type: CustomType
47
+ name: "Mytype"
48
+ declarators:
49
+ - Declarator
50
+ indirect_type: Pointer
51
+ name: "a"
52
+ - ExpressionStatement
53
+ expr: Comma
54
+ exprs:
55
+ - Variable
56
+ name: "_1234"
57
+ - Variable
58
+ name: "_a1234"
59
+ - Variable
60
+ name: "_1234L"
61
+ EOS
62
+ end
63
+ def test_int_literal
64
+ check <<EOS
65
+ void f() {
66
+ 0, 00, 000, 0x0, 0x00;
67
+ 1234, 1234l, 1234ll;
68
+ 01234u, 01234ul, 01234ull;
69
+ 0x1234U, 0x1234L, 0x1234ULL;
70
+ }
71
+ ----
72
+ TranslationUnit
73
+ entities:
74
+ - FunctionDef
75
+ type: Function
76
+ type: Void
77
+ name: "f"
78
+ def: Block
79
+ stmts:
80
+ - ExpressionStatement
81
+ expr: Comma
82
+ exprs:
83
+ - IntLiteral
84
+ val: 0
85
+ - IntLiteral
86
+ format: oct
87
+ val: 0
88
+ - IntLiteral
89
+ format: oct
90
+ val: 0
91
+ - IntLiteral
92
+ format: hex
93
+ val: 0
94
+ - IntLiteral
95
+ format: hex
96
+ val: 0
97
+ - ExpressionStatement
98
+ expr: Comma
99
+ exprs:
100
+ - IntLiteral
101
+ val: 1234
102
+ - IntLiteral
103
+ val: 1234
104
+ suffix: "l"
105
+ - IntLiteral
106
+ val: 1234
107
+ suffix: "ll"
108
+ - ExpressionStatement
109
+ expr: Comma
110
+ exprs:
111
+ - IntLiteral
112
+ format: oct
113
+ val: 668
114
+ suffix: "u"
115
+ - IntLiteral
116
+ format: oct
117
+ val: 668
118
+ suffix: "ul"
119
+ - IntLiteral
120
+ format: oct
121
+ val: 668
122
+ suffix: "ull"
123
+ - ExpressionStatement
124
+ expr: Comma
125
+ exprs:
126
+ - IntLiteral
127
+ format: hex
128
+ val: 4660
129
+ suffix: "U"
130
+ - IntLiteral
131
+ format: hex
132
+ val: 4660
133
+ suffix: "L"
134
+ - IntLiteral
135
+ format: hex
136
+ val: 4660
137
+ suffix: "ULL"
138
+ EOS
139
+ assert_raises(C::ParseError){C::Parser.new.parse('void f() {12lll;}')}
140
+ assert_raises(C::ParseError){C::Parser.new.parse('void f() {12ulL;}')}
141
+ assert_raises(C::ParseError){C::Parser.new.parse('void f() {12lul;}')}
142
+ assert_raises(C::ParseError){C::Parser.new.parse('void f() {123_4;}')}
143
+ end
144
+ def test_float_literal
145
+ check <<EOS
146
+ void f() {
147
+ 123e4, 123E-4;
148
+ 123.4e10, .123E-4;
149
+ 123.4e5, 123.E-10;
150
+
151
+ 0xabp2, 0xabcP-10;
152
+ 0xabc.dp3, 0xabcP-11;
153
+ 0xabc.dp4, 0xabc.P-12;
154
+ }
155
+ ----
156
+ TranslationUnit
157
+ entities:
158
+ - FunctionDef
159
+ type: Function
160
+ type: Void
161
+ name: "f"
162
+ def: Block
163
+ stmts:
164
+ - ExpressionStatement
165
+ expr: Comma
166
+ exprs:
167
+ - FloatLiteral
168
+ val: 1230000.0
169
+ exponent: 4
170
+ - FloatLiteral
171
+ val: 0.0123
172
+ exponent: -4
173
+ - ExpressionStatement
174
+ expr: Comma
175
+ exprs:
176
+ - FloatLiteral
177
+ val: 1234000000000.0
178
+ exponent: 10
179
+ - FloatLiteral
180
+ val: 1.23e-05
181
+ exponent: -4
182
+ - ExpressionStatement
183
+ expr: Comma
184
+ exprs:
185
+ - FloatLiteral
186
+ val: 12340000.0
187
+ exponent: 5
188
+ - FloatLiteral
189
+ val: 1.23e-08
190
+ exponent: -10
191
+ - ExpressionStatement
192
+ expr: Comma
193
+ exprs:
194
+ - FloatLiteral
195
+ format: hex
196
+ val: 684.0
197
+ exponent: 2
198
+ - FloatLiteral
199
+ format: hex
200
+ val: 2.68359375
201
+ exponent: -10
202
+ - ExpressionStatement
203
+ expr: Comma
204
+ exprs:
205
+ - FloatLiteral
206
+ format: hex
207
+ val: 21990.5
208
+ exponent: 3
209
+ - FloatLiteral
210
+ format: hex
211
+ val: 1.341796875
212
+ exponent: -11
213
+ - ExpressionStatement
214
+ expr: Comma
215
+ exprs:
216
+ - FloatLiteral
217
+ format: hex
218
+ val: 43981.0
219
+ exponent: 4
220
+ - FloatLiteral
221
+ format: hex
222
+ val: 0.6708984375
223
+ exponent: -12
224
+ EOS
225
+ assert_raises(C::ParseError){C::Parser.new.parse('void f() {0x123.4pa;}')}
226
+ end
227
+ def test_string_literal
228
+ check <<EOS
229
+ void f() {
230
+ "ab", L"ab", x"ab";
231
+ "a\\0b", L"a\\x0fb", "a\\vb";
232
+ "a b", L"a b";
233
+ "a
234
+ b", L"a
235
+ b";
236
+ }
237
+ ----
238
+ TranslationUnit
239
+ entities:
240
+ - FunctionDef
241
+ type: Function
242
+ type: Void
243
+ name: "f"
244
+ def: Block
245
+ stmts:
246
+ - ExpressionStatement
247
+ expr: Comma
248
+ exprs:
249
+ - StringLiteral
250
+ val: "ab"
251
+ - StringLiteral
252
+ prefix: "L"
253
+ val: "ab"
254
+ - StringLiteral
255
+ prefix: "x"
256
+ val: "ab"
257
+ - ExpressionStatement
258
+ expr: Comma
259
+ exprs:
260
+ - StringLiteral
261
+ val: "a\\\\0b"
262
+ - StringLiteral
263
+ prefix: "L"
264
+ val: "a\\\\x0fb"
265
+ - StringLiteral
266
+ val: "a\\\\vb"
267
+ - ExpressionStatement
268
+ expr: Comma
269
+ exprs:
270
+ - StringLiteral
271
+ val: "a\\tb"
272
+ - StringLiteral
273
+ prefix: "L"
274
+ val: "a\\tb"
275
+ - ExpressionStatement
276
+ expr: Comma
277
+ exprs:
278
+ - StringLiteral
279
+ val: "a\\nb"
280
+ - StringLiteral
281
+ prefix: "L"
282
+ val: "a\\nb"
283
+ EOS
284
+ assert_raises(C::ParseError){C::Parser.new.parse('void f() {xy"ab";}')}
285
+ end
286
+ def test_char_literal
287
+ check <<EOS
288
+ void f() {
289
+ 'a', L'a', x'a';
290
+ '\\0', L'\\xf', '\\v';
291
+ ' ', L'
292
+ ';
293
+ }
294
+ ----
295
+ TranslationUnit
296
+ entities:
297
+ - FunctionDef
298
+ type: Function
299
+ type: Void
300
+ name: "f"
301
+ def: Block
302
+ stmts:
303
+ - ExpressionStatement
304
+ expr: Comma
305
+ exprs:
306
+ - CharLiteral
307
+ val: "a"
308
+ - CharLiteral
309
+ prefix: "L"
310
+ val: "a"
311
+ - CharLiteral
312
+ prefix: "x"
313
+ val: "a"
314
+ - ExpressionStatement
315
+ expr: Comma
316
+ exprs:
317
+ - CharLiteral
318
+ val: "\\\\0"
319
+ - CharLiteral
320
+ prefix: "L"
321
+ val: "\\\\xf"
322
+ - CharLiteral
323
+ val: "\\\\v"
324
+ - ExpressionStatement
325
+ expr: Comma
326
+ exprs:
327
+ - CharLiteral
328
+ val: "\\t"
329
+ - CharLiteral
330
+ prefix: "L"
331
+ val: "\\n"
332
+ EOS
333
+ assert_raises(C::ParseError){C::Parser.new.parse("void f() {xy'ab';}")}
334
+ end
335
+ end