cast 0.2.1 → 0.3.0
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.
- checksums.yaml +7 -0
- data/CHANGELOG +6 -1
- data/Gemfile +8 -1
- data/README.markdown +144 -168
- data/Rakefile +2 -2
- data/cast.gemspec +0 -3
- data/ext/yylex.re +19 -3
- data/lib/cast/c_nodes.rb +1 -0
- data/lib/cast/node_list.rb +9 -9
- data/lib/cast/preprocessor.rb +7 -20
- data/lib/cast/to_s.rb +38 -2
- data/lib/cast/version.rb +1 -1
- data/test/c_nodes_test.rb +2 -2
- data/test/lexer_test.rb +20 -8
- data/test/node_list_test.rb +18 -18
- data/test/node_test.rb +44 -44
- data/test/parse_test.rb +634 -634
- data/test/parser_test.rb +31 -31
- data/test/preprocessor_test.rb +13 -15
- data/test/render_test.rb +34 -4
- data/test/test_helper.rb +8 -5
- metadata +11 -51
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3fa7d3a1b922bb9896e01e72d120241bf45af9e
|
4
|
+
data.tar.gz: 59d62d402059410cc60537316d74fbdb885f0df8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45f872c0b249b174267ebfe998b09fe54979c74fde293d0701ce4006ccaa11a534f996d03452ac8c47c6598a7e4ec2f97c972d381e101d4a4bbaf18eab25ca83
|
7
|
+
data.tar.gz: b28567344263130cd777c5b3d3b3ae323284a59b93537b68f6262107f5f7c00f3f92dae904d6925e6d9fb3d45b6f308a9dc65b65ab2a239e7f8059511c5b07a3
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 0.3.0 2016-03-21
|
2
|
+
|
3
|
+
* Fix popping & shifting from an empty NodeList.
|
4
|
+
* Add FloatLiteral#exponent and require exponent for hex floats.
|
5
|
+
|
1
6
|
== 0.2.1 2012-12-19
|
2
7
|
|
3
8
|
* Parse adjacent string literals correctly.
|
@@ -18,7 +23,7 @@
|
|
18
23
|
* Printing (#to_s):
|
19
24
|
* FunctionDef#no_prototype? is now honored.
|
20
25
|
* ':' is now printed for labels.
|
21
|
-
*
|
26
|
+
* Consecutive unary operators no longer conflict with other
|
22
27
|
operators (e.g. "+ +" instead of "++").
|
23
28
|
* Sizeof is printed with parentheses (not mandated by C99, but
|
24
29
|
conventional).
|
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -40,158 +40,114 @@ Or in irb:
|
|
40
40
|
|
41
41
|
`C.parse` returns a tree of `Node` objects. Here's the class hierarchy:
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
<li class="node_class"><span class="node_class_concrete">BitOr</span></li>
|
152
|
-
<li class="node_class"><span class="node_class_concrete">BitXor</span></li>
|
153
|
-
<li class="node_class"><span class="node_class_concrete">ShiftLeft</span></li>
|
154
|
-
<li class="node_class"><span class="node_class_concrete">ShiftRight</span></li>
|
155
|
-
<li class="node_class"><span class="node_class_concrete">And</span></li>
|
156
|
-
<li class="node_class"><span class="node_class_concrete">Or</span></li>
|
157
|
-
</ul></li>
|
158
|
-
</ul></li>
|
159
|
-
</ul></li>
|
160
|
-
</ul>
|
161
|
-
<ul class="column">
|
162
|
-
<li class="node_class"><span class="node_class_abstract">Node</span><ul>
|
163
|
-
<li class="node_class"><span class="node_class_abstract">Expression</span><ul>
|
164
|
-
<li class="node_class"><span class="node_class_abstract">AssignmentExpression</span><ul>
|
165
|
-
<li class="node_class"><span class="node_class_concrete">Assign</span></li>
|
166
|
-
<li class="node_class"><span class="node_class_concrete">MultiplyAssign</span></li>
|
167
|
-
<li class="node_class"><span class="node_class_concrete">DivideAssign</span></li>
|
168
|
-
<li class="node_class"><span class="node_class_concrete">ModAssign</span></li>
|
169
|
-
<li class="node_class"><span class="node_class_concrete">AddAssign</span></li>
|
170
|
-
<li class="node_class"><span class="node_class_concrete">SubtractAssign</span></li>
|
171
|
-
<li class="node_class"><span class="node_class_concrete">ShiftLeftAssign</span></li>
|
172
|
-
<li class="node_class"><span class="node_class_concrete">ShiftRightAssign</span></li>
|
173
|
-
<li class="node_class"><span class="node_class_concrete">BitAndAssign</span></li>
|
174
|
-
<li class="node_class"><span class="node_class_concrete">BitXorAssign</span></li>
|
175
|
-
<li class="node_class"><span class="node_class_concrete">BitOrAssign</span></li>
|
176
|
-
</ul></li>
|
177
|
-
<li class="node_class"><span class="node_class_abstract">Literal</span><ul>
|
178
|
-
<li class="node_class"><span class="node_class_concrete">StringLiteral</span></li>
|
179
|
-
<li class="node_class"><span class="node_class_concrete">CharLiteral</span></li>
|
180
|
-
<li class="node_class"><span class="node_class_concrete">CompoundLiteral</span></li>
|
181
|
-
<li class="node_class"><span class="node_class_concrete">IntLiteral</span></li>
|
182
|
-
<li class="node_class"><span class="node_class_concrete">FloatLiteral</span></li>
|
183
|
-
</ul></li>
|
184
|
-
</ul></li>
|
185
|
-
<li class="node_class"><span class="node_class_abstract">NodeList</span><ul>
|
186
|
-
<li class="node_class"><span class="node_class_concrete">NodeArray</span></li>
|
187
|
-
<li class="node_class"><span class="node_class_concrete">NodeChain</span></li>
|
188
|
-
</ul></li>
|
189
|
-
</ul></li>
|
190
|
-
</ul>
|
191
|
-
<div class="spacer"></div>
|
192
|
-
</div>
|
193
|
-
|
194
|
-
The <span class="node_class_abstract">highlighted</span> ones are abstract.
|
43
|
+
* **Node**
|
44
|
+
* TranslationUnit
|
45
|
+
* Comment
|
46
|
+
* Declaration
|
47
|
+
* Declarator
|
48
|
+
* FunctionDef
|
49
|
+
* Parameter
|
50
|
+
* Enumerator
|
51
|
+
* MemberInit
|
52
|
+
* Member
|
53
|
+
* **Statement**
|
54
|
+
* Block
|
55
|
+
* If
|
56
|
+
* Switch
|
57
|
+
* While
|
58
|
+
* For
|
59
|
+
* Goto
|
60
|
+
* Continue
|
61
|
+
* Break
|
62
|
+
* Return
|
63
|
+
* ExpressionStatement
|
64
|
+
* **Label**
|
65
|
+
* PlainLabel
|
66
|
+
* Default
|
67
|
+
* Case
|
68
|
+
* **Type**
|
69
|
+
* **IndirectType**
|
70
|
+
* Pointer
|
71
|
+
* Array
|
72
|
+
* Function
|
73
|
+
* **DirectType**
|
74
|
+
* Struct
|
75
|
+
* Union
|
76
|
+
* Enum
|
77
|
+
* CustomType
|
78
|
+
* **PrimitiveType**
|
79
|
+
* Void
|
80
|
+
* Int
|
81
|
+
* Float
|
82
|
+
* Char
|
83
|
+
* Bool
|
84
|
+
* Complex
|
85
|
+
* Imaginary
|
86
|
+
* **Expression**
|
87
|
+
* Comma
|
88
|
+
* Conditional
|
89
|
+
* Variable
|
90
|
+
* **UnaryExpression**
|
91
|
+
* **PostfixExpression**
|
92
|
+
* Index
|
93
|
+
* Call
|
94
|
+
* Dot
|
95
|
+
* Arrow
|
96
|
+
* PostInc
|
97
|
+
* PostDec
|
98
|
+
* **PrefixExpression**
|
99
|
+
* Cast
|
100
|
+
* Address
|
101
|
+
* Dereference
|
102
|
+
* Sizeof
|
103
|
+
* Plus
|
104
|
+
* Minus
|
105
|
+
* PreInc
|
106
|
+
* PreDec
|
107
|
+
* BitNot
|
108
|
+
* Not
|
109
|
+
* **BinaryExpression**
|
110
|
+
* Add
|
111
|
+
* Subtract
|
112
|
+
* Multiply
|
113
|
+
* Divide
|
114
|
+
* Mod
|
115
|
+
* Equal
|
116
|
+
* NotEqual
|
117
|
+
* Less
|
118
|
+
* More
|
119
|
+
* LessOrEqual
|
120
|
+
* MoreOrEqual
|
121
|
+
* BitAnd
|
122
|
+
* BitOr
|
123
|
+
* BitXor
|
124
|
+
* ShiftLeft
|
125
|
+
* ShiftRight
|
126
|
+
* And
|
127
|
+
* Or
|
128
|
+
* **AssignmentExpression**
|
129
|
+
* Assign
|
130
|
+
* MultiplyAssign
|
131
|
+
* DivideAssign
|
132
|
+
* ModAssign
|
133
|
+
* AddAssign
|
134
|
+
* SubtractAssign
|
135
|
+
* ShiftLeftAssign
|
136
|
+
* ShiftRightAssign
|
137
|
+
* BitAndAssign
|
138
|
+
* BitXorAssign
|
139
|
+
* BitOrAssign
|
140
|
+
* **Literal**
|
141
|
+
* StringLiteral
|
142
|
+
* CharLiteral
|
143
|
+
* CompoundLiteral
|
144
|
+
* IntLiteral
|
145
|
+
* FloatLiteral
|
146
|
+
* **NodeList**
|
147
|
+
* NodeArray
|
148
|
+
* NodeChain
|
149
|
+
|
150
|
+
The **bold** ones are abstract.
|
195
151
|
|
196
152
|
The last 2 (`NodeList`s) represent lists of `Node`s. They quack like
|
197
153
|
standard ruby `Arrays`. `NodeChain` is a doubly linked list;
|
@@ -291,7 +247,7 @@ Other notes about the table:
|
|
291
247
|
table.node_desc tr.first_field table td {
|
292
248
|
border: none;
|
293
249
|
}
|
294
|
-
|
250
|
+
|
295
251
|
table.node_desc td {
|
296
252
|
padding: 3px;
|
297
253
|
vertical-align: top;
|
@@ -363,7 +319,7 @@ Other notes about the table:
|
|
363
319
|
<pre>
|
364
320
|
Declaration
|
365
321
|
type: Int
|
366
|
-
declarators:
|
322
|
+
declarators:
|
367
323
|
- Declarator
|
368
324
|
name: "i"
|
369
325
|
- Declarator
|
@@ -1372,18 +1328,18 @@ int main(int argc, char **argv) {
|
|
1372
1328
|
<pre>CompoundLiteral
|
1373
1329
|
type: Struct
|
1374
1330
|
name: "S"
|
1375
|
-
member_inits:
|
1331
|
+
member_inits:
|
1376
1332
|
- MemberInit
|
1377
1333
|
init: IntLiteral
|
1378
1334
|
val: 1
|
1379
1335
|
- MemberInit
|
1380
|
-
member:
|
1336
|
+
member:
|
1381
1337
|
- Member
|
1382
1338
|
name: "x"
|
1383
1339
|
init: IntLiteral
|
1384
1340
|
val: 2
|
1385
1341
|
- MemberInit
|
1386
|
-
member:
|
1342
|
+
member:
|
1387
1343
|
- Member
|
1388
1344
|
name: "y"
|
1389
1345
|
- IntLiteral
|
@@ -1401,11 +1357,11 @@ int main(int argc, char **argv) {
|
|
1401
1357
|
</tr>
|
1402
1358
|
|
1403
1359
|
<tr class="first_field">
|
1404
|
-
<td class="nd_class" rowspan="
|
1405
|
-
<td class="nd_field"><tt>
|
1406
|
-
<td class="nd_values"><tt
|
1407
|
-
<td class="nd_default"></td>
|
1408
|
-
<td class="nd_comments" rowspan="
|
1360
|
+
<td class="nd_class" rowspan="3"><tt>IntLiteral</tt></td>
|
1361
|
+
<td class="nd_field"><tt>format</tt></td>
|
1362
|
+
<td class="nd_values"><tt>:dec</tt>, <tt>:hex</tt>, <tt>:oct</tt></td>
|
1363
|
+
<td class="nd_default"><tt>:dec</tt></td>
|
1364
|
+
<td class="nd_comments" rowspan="3">
|
1409
1365
|
<p>Also:</p>
|
1410
1366
|
<ul>
|
1411
1367
|
<li><tt>#dec?</tt> -- return true iff <tt>format == :dec</tt></li>
|
@@ -1415,18 +1371,38 @@ int main(int argc, char **argv) {
|
|
1415
1371
|
</td>
|
1416
1372
|
</tr>
|
1417
1373
|
<tr>
|
1418
|
-
<td class="nd_field"><tt>
|
1419
|
-
<td class="nd_values"><tt
|
1420
|
-
<td class="nd_default"
|
1374
|
+
<td class="nd_field"><tt>val</tt></td>
|
1375
|
+
<td class="nd_values"><tt>Integer</tt></td>
|
1376
|
+
<td class="nd_default"></td>
|
1377
|
+
</tr>
|
1378
|
+
<tr>
|
1379
|
+
<td class="nd_field"><tt>suffix</tt></td>
|
1380
|
+
<td class="nd_values"><tt>String</tt></td>
|
1381
|
+
<td class="nd_default"></td>
|
1421
1382
|
</tr>
|
1422
1383
|
|
1423
1384
|
<tr class="first_field">
|
1424
|
-
<td class="nd_class" rowspan="
|
1385
|
+
<td class="nd_class" rowspan="4"><tt>FloatLiteral</tt></td>
|
1386
|
+
<td class="nd_field"><tt>format</tt></td>
|
1387
|
+
<td class="nd_values"><tt>:dec</tt>, <tt>:hex</tt></td>
|
1388
|
+
<td class="nd_default"><tt>:dec</tt></td>
|
1389
|
+
<td class="nd_comments" rowspan="4">
|
1390
|
+
</td>
|
1391
|
+
</tr>
|
1392
|
+
<tr>
|
1425
1393
|
<td class="nd_field"><tt>val</tt></td>
|
1426
1394
|
<td class="nd_values"><tt>Float</tt></td>
|
1427
1395
|
<td class="nd_default"></td>
|
1428
|
-
|
1429
|
-
|
1396
|
+
</tr>
|
1397
|
+
<tr>
|
1398
|
+
<td class="nd_field"><tt>exponent</tt></td>
|
1399
|
+
<td class="nd_values"><tt>Integer</tt></td>
|
1400
|
+
<td class="nd_default"></td>
|
1401
|
+
</tr>
|
1402
|
+
<tr>
|
1403
|
+
<td class="nd_field"><tt>suffix</tt></td>
|
1404
|
+
<td class="nd_values"><tt>String</tt></td>
|
1405
|
+
<td class="nd_default"></td>
|
1430
1406
|
</tr>
|
1431
1407
|
|
1432
1408
|
<tr class="first_field">
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'ritual'
|
2
2
|
require 'rake/testtask'
|
3
3
|
|
4
|
-
Rake::TestTask.new do |t|
|
4
|
+
Rake::TestTask.new(test: [:ext, 'lib/cast/c.tab.rb']) do |t|
|
5
5
|
t.libs << "test"
|
6
6
|
t.test_files = FileList['test/*_test.rb']
|
7
|
-
t.
|
7
|
+
t.warning = false
|
8
8
|
end
|
9
9
|
|
10
10
|
extension
|
data/cast.gemspec
CHANGED
@@ -15,7 +15,4 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
16
|
gem.files = `git ls-files`.split("\n") + ['ext/yylex.c', 'lib/cast/c.tab.rb']
|
17
17
|
gem.test_files = `git ls-files -- test/*`.split("\n")
|
18
|
-
|
19
|
-
gem.add_development_dependency 'ritual', '~> 0.4.0'
|
20
|
-
gem.add_development_dependency 'racc', '~> 1.4.8'
|
21
18
|
end
|
data/ext/yylex.re
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
*
|
4
4
|
* Based on c.re in the exmaples distributed with re2c.
|
5
5
|
*/
|
6
|
+
#include <stdio.h>
|
6
7
|
#include <string.h>
|
7
8
|
#include "cast.h"
|
8
9
|
|
@@ -31,6 +32,7 @@ set_func(IntLiteral, suffix);
|
|
31
32
|
new_func(FloatLiteral);
|
32
33
|
set_func(FloatLiteral, format);
|
33
34
|
set_func(FloatLiteral, val);
|
35
|
+
set_func(FloatLiteral, exponent);
|
34
36
|
set_func(FloatLiteral, suffix);
|
35
37
|
|
36
38
|
new_func(CharLiteral);
|
@@ -75,7 +77,7 @@ static void error1(char *s, char *b, char *e) {
|
|
75
77
|
*/
|
76
78
|
void yylex(VALUE self, cast_Parser *p) {
|
77
79
|
char *cursor = p->cur;
|
78
|
-
char *cp;
|
80
|
+
char *cp, *ep;
|
79
81
|
VALUE value;
|
80
82
|
std:
|
81
83
|
p->tok = cursor;
|
@@ -177,15 +179,29 @@ void yylex(VALUE self, cast_Parser *p) {
|
|
177
179
|
cast_FloatLiteral_set_format(value, ID2SYM(rb_intern("dec")));
|
178
180
|
cast_FloatLiteral_set_val(value, rb_float_new(strtod(p->tok, (char **)&cp)));
|
179
181
|
if (cp < cursor)
|
180
|
-
|
182
|
+
cast_FloatLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
|
183
|
+
|
184
|
+
ep = (char *)memchr(p->tok, 'e', cp - p->tok);
|
185
|
+
if (!ep)
|
186
|
+
ep = (char *)memchr(p->tok, 'E', cp - p->tok);
|
187
|
+
if (ep)
|
188
|
+
cast_FloatLiteral_set_exponent(value, LONG2NUM(strtod(ep + 1, NULL)));
|
189
|
+
|
181
190
|
RETVALUE(cast_sym_FCON);
|
182
191
|
}
|
183
|
-
( "0" [Xx] (H+ P | H* "." H+ P
|
192
|
+
( "0" [Xx] (H+ P | H* "." H+ P | H+ "." H* P) ) FS? {
|
184
193
|
value = cast_new_FloatLiteral_at(p->lineno);
|
185
194
|
cast_FloatLiteral_set_format(value, ID2SYM(rb_intern("hex")));
|
186
195
|
cast_FloatLiteral_set_val(value, rb_float_new(strtod(p->tok, (char **)&cp)));
|
187
196
|
if (cp < cursor)
|
188
197
|
cast_FloatLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
|
198
|
+
|
199
|
+
ep = (char *)memchr(p->tok, 'p', cp - p->tok);
|
200
|
+
if (!ep)
|
201
|
+
ep = (char *)memchr(p->tok, 'P', cp - p->tok);
|
202
|
+
if (ep)
|
203
|
+
cast_FloatLiteral_set_exponent(value, LONG2NUM(strtod(ep + 1, NULL)));
|
204
|
+
|
189
205
|
RETVALUE(cast_sym_FCON);
|
190
206
|
}
|
191
207
|
|