cast 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- * Consequetive unary operators no longer conflict with other
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
@@ -1,2 +1,9 @@
1
- source :rubygems
1
+ source 'https://rubygems.org/'
2
+
3
+ gem 'minitest'
4
+ gem 'minitest-focus'
5
+ gem 'pry'
6
+ gem 'racc'
7
+ gem 'ritual'
8
+
2
9
  gemspec
@@ -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
- <div class="node_classes">
44
- <style scoped="scoped">
45
- div.node_classes .node_class_abstract {
46
- font-weight: bold;
47
- }
48
- div.node_classes .column {
49
- line-height: 120%;
50
- float: left;
51
- color: #222;
52
- }
53
- div.node_classes .spacer {
54
- display: float;
55
- clear: both;
56
- }
57
- </style>
58
- <ul class="column">
59
- <li class="node_class"><span class="node_class_abstract">Node</span><ul>
60
- <li class="node_class"><span class="node_class_concrete">TranslationUnit</span></li>
61
- <li class="node_class"><span class="node_class_concrete">Comment</span></li>
62
- <li class="node_class"><span class="node_class_concrete">Declaration</span></li>
63
- <li class="node_class"><span class="node_class_concrete">Declarator</span></li>
64
- <li class="node_class"><span class="node_class_concrete">FunctionDef</span></li>
65
- <li class="node_class"><span class="node_class_concrete">Parameter</span></li>
66
- <li class="node_class"><span class="node_class_concrete">Enumerator</span></li>
67
- <li class="node_class"><span class="node_class_concrete">MemberInit</span></li>
68
- <li class="node_class"><span class="node_class_concrete">Member</span></li>
69
- <li class="node_class"><span class="node_class_abstract">Statement</span><ul>
70
- <li class="node_class"><span class="node_class_concrete">Block</span></li>
71
- <li class="node_class"><span class="node_class_concrete">If</span></li>
72
- <li class="node_class"><span class="node_class_concrete">Switch</span></li>
73
- <li class="node_class"><span class="node_class_concrete">While</span></li>
74
- <li class="node_class"><span class="node_class_concrete">For</span></li>
75
- <li class="node_class"><span class="node_class_concrete">Goto</span></li>
76
- <li class="node_class"><span class="node_class_concrete">Continue</span></li>
77
- <li class="node_class"><span class="node_class_concrete">Break</span></li>
78
- <li class="node_class"><span class="node_class_concrete">Return</span></li>
79
- <li class="node_class"><span class="node_class_concrete">ExpressionStatement</span></li>
80
- </ul></li>
81
- <li class="node_class"><span class="node_class_abstract">Label</span><ul>
82
- <li class="node_class"><span class="node_class_concrete">PlainLabel</span></li>
83
- <li class="node_class"><span class="node_class_concrete">Default</span></li>
84
- <li class="node_class"><span class="node_class_concrete">Case</span></li>
85
- </ul></li>
86
- <li class="node_class"><span class="node_class_abstract">Type</span><ul>
87
- <li class="node_class"><span class="node_class_abstract">IndirectType</span><ul>
88
- <li class="node_class"><span class="node_class_concrete">Pointer</span></li>
89
- <li class="node_class"><span class="node_class_concrete">Array</span></li>
90
- <li class="node_class"><span class="node_class_concrete">Function</span></li>
91
- </ul></li>
92
- <li class="node_class"><span class="node_class_abstract">DirectType</span><ul>
93
- <li class="node_class"><span class="node_class_concrete">Struct</span></li>
94
- <li class="node_class"><span class="node_class_concrete">Union</span></li>
95
- <li class="node_class"><span class="node_class_concrete">Enum</span></li>
96
- <li class="node_class"><span class="node_class_concrete">CustomType</span></li>
97
- <li class="node_class"><span class="node_class_abstract">PrimitiveType</span><ul>
98
- <li class="node_class"><span class="node_class_concrete">Void</span></li>
99
- <li class="node_class"><span class="node_class_concrete">Int</span></li>
100
- <li class="node_class"><span class="node_class_concrete">Float</span></li>
101
- <li class="node_class"><span class="node_class_concrete">Char</span></li>
102
- <li class="node_class"><span class="node_class_concrete">Bool</span></li>
103
- <li class="node_class"><span class="node_class_concrete">Complex</span></li>
104
- <li class="node_class"><span class="node_class_concrete">Imaginary</span></li>
105
- </ul></li>
106
- </ul></li>
107
- </ul></li>
108
- </ul></li>
109
- </ul>
110
- <ul class="column">
111
- <li class="node_class"><span class="node_class_abstract">Node</span><ul>
112
- <li class="node_class"><span class="node_class_abstract">Expression</span><ul>
113
- <li class="node_class"><span class="node_class_concrete">Comma</span></li>
114
- <li class="node_class"><span class="node_class_concrete">Conditional</span></li>
115
- <li class="node_class"><span class="node_class_concrete">Variable</span></li>
116
- <li class="node_class"><span class="node_class_abstract">UnaryExpression</span><ul>
117
- <li class="node_class"><span class="node_class_abstract">PostfixExpression</span><ul>
118
- <li class="node_class"><span class="node_class_concrete">Index</span></li>
119
- <li class="node_class"><span class="node_class_concrete">Call</span></li>
120
- <li class="node_class"><span class="node_class_concrete">Dot</span></li>
121
- <li class="node_class"><span class="node_class_concrete">Arrow</span></li>
122
- <li class="node_class"><span class="node_class_concrete">PostInc</span></li>
123
- <li class="node_class"><span class="node_class_concrete">PostDec</span></li>
124
- </ul></li>
125
- <li class="node_class"><span class="node_class_abstract">PrefixExpression</span><ul>
126
- <li class="node_class"><span class="node_class_concrete">Cast</span></li>
127
- <li class="node_class"><span class="node_class_concrete">Address</span></li>
128
- <li class="node_class"><span class="node_class_concrete">Dereference</span></li>
129
- <li class="node_class"><span class="node_class_concrete">Sizeof</span></li>
130
- <li class="node_class"><span class="node_class_concrete">Plus</span></li>
131
- <li class="node_class"><span class="node_class_concrete">Minus</span></li>
132
- <li class="node_class"><span class="node_class_concrete">PreInc</span></li>
133
- <li class="node_class"><span class="node_class_concrete">PreDec</span></li>
134
- <li class="node_class"><span class="node_class_concrete">BitNot</span></li>
135
- <li class="node_class"><span class="node_class_concrete">Not</span></li>
136
- </ul></li>
137
- </ul></li>
138
- <li class="node_class"><span class="node_class_abstract">BinaryExpression</span><ul>
139
- <li class="node_class"><span class="node_class_concrete">Add</span></li>
140
- <li class="node_class"><span class="node_class_concrete">Subtract</span></li>
141
- <li class="node_class"><span class="node_class_concrete">Multiply</span></li>
142
- <li class="node_class"><span class="node_class_concrete">Divide</span></li>
143
- <li class="node_class"><span class="node_class_concrete">Mod</span></li>
144
- <li class="node_class"><span class="node_class_concrete">Equal</span></li>
145
- <li class="node_class"><span class="node_class_concrete">NotEqual</span></li>
146
- <li class="node_class"><span class="node_class_concrete">Less</span></li>
147
- <li class="node_class"><span class="node_class_concrete">More</span></li>
148
- <li class="node_class"><span class="node_class_concrete">LessOrEqual</span></li>
149
- <li class="node_class"><span class="node_class_concrete">MoreOrEqual</span></li>
150
- <li class="node_class"><span class="node_class_concrete">BitAnd</span></li>
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="2"><tt>IntLiteral</tt></td>
1405
- <td class="nd_field"><tt>val</tt></td>
1406
- <td class="nd_values"><tt>Integer</tt></td>
1407
- <td class="nd_default"></td>
1408
- <td class="nd_comments" rowspan="2">
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>format</tt></td>
1419
- <td class="nd_values"><tt>:dec</tt>, <tt>:hex</tt>, <tt>:oct</tt></td>
1420
- <td class="nd_default"><tt>:dec</tt></td>
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="1"><tt>FloatLiteral</tt></td>
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
- <td class="nd_comments" rowspan="1">
1429
- </td>
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.verbose = true
7
+ t.warning = false
8
8
  end
9
9
 
10
10
  extension
@@ -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
@@ -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
- cast_FloatLiteral_set_suffix(value, rb_str_new(cp, cursor - cp));
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? | H+ "." H* P?) ) FS? {
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