iv-phonic 0.0.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.
Files changed (50) hide show
  1. data/.autotest +24 -0
  2. data/Manifest.txt +49 -0
  3. data/README.rdoc +32 -0
  4. data/Rakefile +54 -0
  5. data/ext/include/iv/algorithm.h +23 -0
  6. data/ext/include/iv/alloc.h +200 -0
  7. data/ext/include/iv/any.h +71 -0
  8. data/ext/include/iv/ast-factory.h +277 -0
  9. data/ext/include/iv/ast-fwd.h +92 -0
  10. data/ext/include/iv/ast-serializer.h +579 -0
  11. data/ext/include/iv/ast-visitor.h +121 -0
  12. data/ext/include/iv/ast.h +1127 -0
  13. data/ext/include/iv/chars.h +83 -0
  14. data/ext/include/iv/cmdline.h +830 -0
  15. data/ext/include/iv/conversions.h +308 -0
  16. data/ext/include/iv/dtoa.h +20 -0
  17. data/ext/include/iv/enable_if.h +18 -0
  18. data/ext/include/iv/errors.h +15 -0
  19. data/ext/include/iv/fixedcontainer.h +42 -0
  20. data/ext/include/iv/functor.h +29 -0
  21. data/ext/include/iv/lexer.h +1281 -0
  22. data/ext/include/iv/location.h +23 -0
  23. data/ext/include/iv/mt19937.h +175 -0
  24. data/ext/include/iv/noncopyable.h +30 -0
  25. data/ext/include/iv/none.h +10 -0
  26. data/ext/include/iv/parser.h +2150 -0
  27. data/ext/include/iv/source.h +27 -0
  28. data/ext/include/iv/space.h +178 -0
  29. data/ext/include/iv/static_assert.h +30 -0
  30. data/ext/include/iv/stringpiece.h +385 -0
  31. data/ext/include/iv/token.h +311 -0
  32. data/ext/include/iv/ucdata.h +58 -0
  33. data/ext/include/iv/uchar.h +8 -0
  34. data/ext/include/iv/ustring.h +28 -0
  35. data/ext/include/iv/ustringpiece.h +9 -0
  36. data/ext/include/iv/utils.h +83 -0
  37. data/ext/include/iv/xorshift.h +74 -0
  38. data/ext/iv/phonic/ast-fwd.h +21 -0
  39. data/ext/iv/phonic/ast.h +10 -0
  40. data/ext/iv/phonic/creator.h +530 -0
  41. data/ext/iv/phonic/encoding.h +110 -0
  42. data/ext/iv/phonic/extconf.rb +5 -0
  43. data/ext/iv/phonic/factory.h +247 -0
  44. data/ext/iv/phonic/parser.h +12 -0
  45. data/ext/iv/phonic/phonic.cc +69 -0
  46. data/ext/iv/phonic/rnode.h +15 -0
  47. data/ext/iv/phonic/rparser.h +48 -0
  48. data/ext/iv/phonic/source.h +146 -0
  49. data/test/test_iv_phonic.rb +32 -0
  50. metadata +159 -0
@@ -0,0 +1,530 @@
1
+ #ifndef _IV_PHONIC_CREATOR_H_
2
+ #define _IV_PHONIC_CREATOR_H_
3
+ #include <ruby.h>
4
+ #include <iv/ast-visitor.h>
5
+ #include <iv/utils.h>
6
+ #include "factory.h"
7
+ #include "encoding.h"
8
+ #define SYM(str) ID2SYM(rb_intern(str))
9
+ // create RubyObject of AST from iv AST
10
+ namespace iv {
11
+ namespace phonic {
12
+
13
+ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
14
+ public:
15
+ Creator() { }
16
+
17
+ VALUE Result(const FunctionLiteral* global) {
18
+ VALUE array = rb_ary_new();
19
+ for (Statements::const_iterator it = global->body().begin(),
20
+ last = global->body().end(); it != last; ++it) {
21
+ (*it)->Accept(this);
22
+ rb_ary_push(array, ret_);
23
+ }
24
+ return array;
25
+ }
26
+
27
+ void Visit(const Block* stmt) {
28
+ VALUE hash = rb_hash_new();
29
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Block"));
30
+ VALUE array = rb_ary_new();
31
+ for (Statements::const_iterator it = stmt->body().begin(),
32
+ last = stmt->body().end(); it != last; ++it) {
33
+ (*it)->Accept(this);
34
+ rb_ary_push(array, ret_);
35
+ }
36
+ rb_hash_aset(hash, SYM("body"), array);
37
+ ret_ = hash;
38
+ }
39
+
40
+ void Visit(const FunctionStatement* stmt) {
41
+ VALUE hash = rb_hash_new();
42
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FunctionStatement"));
43
+ Visit(stmt->function());
44
+ rb_hash_aset(hash, SYM("body"), ret_);
45
+ ret_ = hash;
46
+ }
47
+
48
+ void Visit(const VariableStatement* stmt) {
49
+ VALUE hash = rb_hash_new();
50
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("VariableStatement"));
51
+ rb_hash_aset(hash, SYM("const"), stmt->IsConst() ? Qtrue : Qfalse);
52
+ VALUE array = rb_ary_new();
53
+ for (Declarations::const_iterator it = stmt->decls().begin(),
54
+ last = stmt->decls().end(); it != last; ++it) {
55
+ VALUE decl = rb_hash_new();
56
+ rb_hash_aset(decl, SYM("type"), rb_str_new_cstr("Declaration"));
57
+ Visit((*it)->name());
58
+ rb_hash_aset(decl, SYM("name"), ret_);
59
+ if ((*it)->expr()) {
60
+ (*it)->expr()->Accept(this);
61
+ rb_hash_aset(decl, SYM("expr"), ret_);
62
+ }
63
+ rb_ary_push(array, decl);
64
+ }
65
+ rb_hash_aset(hash, SYM("body"), array);
66
+ ret_ = hash;
67
+ }
68
+
69
+ void Visit(const EmptyStatement* stmt) {
70
+ VALUE hash = rb_hash_new();
71
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("EmptyStatement"));
72
+ ret_ = hash;
73
+ }
74
+
75
+ void Visit(const IfStatement* stmt) {
76
+ VALUE hash = rb_hash_new();
77
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("IfStatement"));
78
+ stmt->cond()->Accept(this);
79
+ rb_hash_aset(hash, SYM("cond"), ret_);
80
+ stmt->then_statement()->Accept(this);
81
+ rb_hash_aset(hash, SYM("then"), ret_);
82
+ if (stmt->else_statement()) {
83
+ stmt->else_statement()->Accept(this);
84
+ rb_hash_aset(hash, SYM("else"), ret_);
85
+ }
86
+ ret_ = hash;
87
+ }
88
+
89
+ void Visit(const DoWhileStatement* stmt) {
90
+ VALUE hash = rb_hash_new();
91
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("DoWhileStatement"));
92
+ stmt->cond()->Accept(this);
93
+ rb_hash_aset(hash, SYM("cond"), ret_);
94
+ stmt->body()->Accept(this);
95
+ rb_hash_aset(hash, SYM("body"), ret_);
96
+ ret_ = hash;
97
+ }
98
+
99
+ void Visit(const WhileStatement* stmt) {
100
+ VALUE hash = rb_hash_new();
101
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("WhileStatement"));
102
+ stmt->cond()->Accept(this);
103
+ rb_hash_aset(hash, SYM("cond"), ret_);
104
+ stmt->body()->Accept(this);
105
+ rb_hash_aset(hash, SYM("body"), ret_);
106
+ ret_ = hash;
107
+ }
108
+
109
+ void Visit(const ForStatement* stmt) {
110
+ VALUE hash = rb_hash_new();
111
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ForStatement"));
112
+ if (stmt->init()) {
113
+ stmt->init()->Accept(this);
114
+ rb_hash_aset(hash, SYM("init"), ret_);
115
+ }
116
+ if (stmt->cond()) {
117
+ stmt->cond()->Accept(this);
118
+ rb_hash_aset(hash, SYM("cond"), ret_);
119
+ }
120
+ if (stmt->next()) {
121
+ stmt->next()->Accept(this);
122
+ rb_hash_aset(hash, SYM("next"), ret_);
123
+ }
124
+ stmt->body()->Accept(this);
125
+ rb_hash_aset(hash, SYM("body"), ret_);
126
+ ret_ = hash;
127
+ }
128
+
129
+ void Visit(const ForInStatement* stmt) {
130
+ VALUE hash = rb_hash_new();
131
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ForInStatement"));
132
+ stmt->each()->Accept(this);
133
+ rb_hash_aset(hash, SYM("each"), ret_);
134
+ stmt->enumerable()->Accept(this);
135
+ rb_hash_aset(hash, SYM("enum"), ret_);
136
+ stmt->body()->Accept(this);
137
+ rb_hash_aset(hash, SYM("body"), ret_);
138
+ ret_ = hash;
139
+ }
140
+
141
+ void Visit(const ContinueStatement* stmt) {
142
+ VALUE hash = rb_hash_new();
143
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ContinueStatement"));
144
+ if (stmt->label()) {
145
+ stmt->label()->Accept(this);
146
+ rb_hash_aset(hash, SYM("label"), ret_);
147
+ }
148
+ ret_ = hash;
149
+ }
150
+
151
+ void Visit(const BreakStatement* stmt) {
152
+ VALUE hash = rb_hash_new();
153
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("BreakStatement"));
154
+ if (stmt->label()) {
155
+ stmt->label()->Accept(this);
156
+ rb_hash_aset(hash, SYM("label"), ret_);
157
+ }
158
+ ret_ = hash;
159
+ }
160
+
161
+ void Visit(const ReturnStatement* stmt) {
162
+ VALUE hash = rb_hash_new();
163
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ReturnStatement"));
164
+ stmt->expr()->Accept(this);
165
+ rb_hash_aset(hash, SYM("expr"), ret_);
166
+ ret_ = hash;
167
+ }
168
+
169
+ void Visit(const WithStatement* stmt) {
170
+ VALUE hash = rb_hash_new();
171
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("WithStatement"));
172
+ stmt->context()->Accept(this);
173
+ rb_hash_aset(hash, SYM("context"), ret_);
174
+ stmt->body()->Accept(this);
175
+ rb_hash_aset(hash, SYM("body"), ret_);
176
+ ret_ = hash;
177
+ }
178
+
179
+ void Visit(const LabelledStatement* stmt) {
180
+ VALUE hash = rb_hash_new();
181
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("LabelledStatement"));
182
+ stmt->label()->Accept(this);
183
+ rb_hash_aset(hash, SYM("label"), ret_);
184
+ stmt->body()->Accept(this);
185
+ rb_hash_aset(hash, SYM("body"), ret_);
186
+ ret_ = hash;
187
+ }
188
+
189
+ void Visit(const SwitchStatement* stmt) {
190
+ VALUE hash = rb_hash_new();
191
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("SwitchStatement"));
192
+ stmt->expr()->Accept(this);
193
+ rb_hash_aset(hash, SYM("cond"), ret_);
194
+ VALUE array = rb_ary_new();
195
+ for (CaseClauses::const_iterator it = stmt->clauses().begin(),
196
+ last = stmt->clauses().end(); it != last; ++it) {
197
+ VALUE clause = rb_hash_new();
198
+ if ((*it)->IsDefault()) {
199
+ rb_hash_aset(clause, SYM("type"), rb_str_new_cstr("Default"));
200
+ } else {
201
+ rb_hash_aset(clause, SYM("type"), rb_str_new_cstr("Case"));
202
+ (*it)->expr()->Accept(this);
203
+ rb_hash_aset(clause, SYM("expr"), ret_);
204
+ }
205
+ VALUE stmts = rb_ary_new();
206
+ for (Statements::const_iterator st = (*it)->body().begin(),
207
+ stlast = (*it)->body().end(); st != stlast; ++st) {
208
+ (*st)->Accept(this);
209
+ rb_ary_push(stmts, ret_);
210
+ }
211
+ rb_hash_aset(clause, SYM("body"), stmts);
212
+ rb_ary_push(array, ret_);
213
+ }
214
+ rb_hash_aset(hash, SYM("clauses"), array);
215
+ ret_ = hash;
216
+ }
217
+
218
+ void Visit(const ThrowStatement* stmt) {
219
+ VALUE hash = rb_hash_new();
220
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ThrowStatement"));
221
+ ret_ = hash;
222
+ }
223
+
224
+ void Visit(const TryStatement* stmt) {
225
+ VALUE hash = rb_hash_new();
226
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("TryStatement"));
227
+ ret_ = hash;
228
+ }
229
+
230
+ void Visit(const DebuggerStatement* stmt) {
231
+ VALUE hash = rb_hash_new();
232
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("DebuggerStatement"));
233
+ ret_ = hash;
234
+ }
235
+
236
+ void Visit(const ExpressionStatement* stmt) {
237
+ VALUE hash = rb_hash_new();
238
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ExpressionStatement"));
239
+ stmt->expr()->Accept(this);
240
+ rb_hash_aset(hash, SYM("body"), ret_);
241
+ ret_ = hash;
242
+ }
243
+
244
+ void Visit(const Assignment* expr) {
245
+ VALUE hash = rb_hash_new();
246
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Assignment"));
247
+ rb_hash_aset(hash, SYM("op"),
248
+ rb_str_new_cstr(core::Token::ToString(expr->op())));
249
+ expr->left()->Accept(this);
250
+ rb_hash_aset(hash, SYM("left"), ret_);
251
+ expr->right()->Accept(this);
252
+ rb_hash_aset(hash, SYM("right"), ret_);
253
+ ret_ = hash;
254
+ }
255
+
256
+ void Visit(const BinaryOperation* expr) {
257
+ VALUE hash = rb_hash_new();
258
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("BinaryOperation"));
259
+ rb_hash_aset(hash, SYM("op"),
260
+ rb_str_new_cstr(core::Token::ToString(expr->op())));
261
+ expr->left()->Accept(this);
262
+ rb_hash_aset(hash, SYM("left"), ret_);
263
+ expr->right()->Accept(this);
264
+ rb_hash_aset(hash, SYM("right"), ret_);
265
+ ret_ = hash;
266
+ }
267
+
268
+ void Visit(const ConditionalExpression* expr) {
269
+ VALUE hash = rb_hash_new();
270
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ConditionalExpression"));
271
+ expr->cond()->Accept(this);
272
+ rb_hash_aset(hash, SYM("cond"), ret_);
273
+ expr->left()->Accept(this);
274
+ rb_hash_aset(hash, SYM("left"), ret_);
275
+ expr->right()->Accept(this);
276
+ rb_hash_aset(hash, SYM("right"), ret_);
277
+ ret_ = hash;
278
+ }
279
+
280
+ void Visit(const UnaryOperation* expr) {
281
+ VALUE hash = rb_hash_new();
282
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("UnaryOperation"));
283
+ rb_hash_aset(hash, SYM("op"),
284
+ rb_str_new_cstr(core::Token::ToString(expr->op())));
285
+ expr->expr()->Accept(this);
286
+ rb_hash_aset(hash, SYM("expr"), ret_);
287
+ ret_ = hash;
288
+ }
289
+
290
+ void Visit(const PostfixExpression* expr) {
291
+ VALUE hash = rb_hash_new();
292
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("PostfixExpression"));
293
+ rb_hash_aset(hash, SYM("op"),
294
+ rb_str_new_cstr(core::Token::ToString(expr->op())));
295
+ expr->expr()->Accept(this);
296
+ rb_hash_aset(hash, SYM("expr"), ret_);
297
+ ret_ = hash;
298
+ }
299
+
300
+ void Visit(const StringLiteral* literal) {
301
+ VALUE hash = rb_hash_new();
302
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("StringLiteral"));
303
+ const StringLiteral::value_type& str = literal->value();
304
+ rb_hash_aset(hash, SYM("value"),
305
+ Encoding::ConvertToDefaultInternal(
306
+ rb_enc_str_new(
307
+ reinterpret_cast<const char*>(str.data()),
308
+ str.size()*2,
309
+ rb_to_encoding(Encoding::UTF16Encoding()))));
310
+ ret_ = hash;
311
+ }
312
+
313
+ void Visit(const NumberLiteral* literal) {
314
+ VALUE hash = rb_hash_new();
315
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("NumberLiteral"));
316
+ rb_hash_aset(hash, SYM("value"), rb_float_new(literal->value()));
317
+ ret_ = hash;
318
+ }
319
+
320
+ void Visit(const Identifier* literal) {
321
+ VALUE hash = rb_hash_new();
322
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Identifier"));
323
+ const Identifier::value_type& str = literal->value();
324
+ rb_hash_aset(hash, SYM("value"),
325
+ Encoding::ConvertToDefaultInternal(
326
+ rb_enc_str_new(
327
+ reinterpret_cast<const char*>(str.data()),
328
+ str.size()*2,
329
+ rb_to_encoding(Encoding::UTF16Encoding()))));
330
+ ret_ = hash;
331
+ }
332
+
333
+ void Visit(const ThisLiteral* literal) {
334
+ VALUE hash = rb_hash_new();
335
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ThisLiteral"));
336
+ ret_ = hash;
337
+ }
338
+
339
+ void Visit(const NullLiteral* literal) {
340
+ VALUE hash = rb_hash_new();
341
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("NullLiteral"));
342
+ ret_ = hash;
343
+ }
344
+
345
+ void Visit(const TrueLiteral* literal) {
346
+ VALUE hash = rb_hash_new();
347
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("TrueLiteral"));
348
+ ret_ = hash;
349
+ }
350
+
351
+ void Visit(const FalseLiteral* literal) {
352
+ VALUE hash = rb_hash_new();
353
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FalseLiteral"));
354
+ ret_ = hash;
355
+ }
356
+
357
+ void Visit(const Undefined* literal) {
358
+ VALUE hash = rb_hash_new();
359
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Undefined"));
360
+ ret_ = hash;
361
+ }
362
+
363
+ void Visit(const RegExpLiteral* literal) {
364
+ VALUE hash = rb_hash_new();
365
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("RegExpLiteral"));
366
+ const RegExpLiteral::value_type& content = literal->value();
367
+ rb_hash_aset(hash, SYM("value"),
368
+ Encoding::ConvertToDefaultInternal(
369
+ rb_enc_str_new(
370
+ reinterpret_cast<const char*>(content.data()),
371
+ content.size()*2,
372
+ rb_to_encoding(Encoding::UTF16Encoding()))));
373
+ const RegExpLiteral::value_type& flags = literal->flags();
374
+ rb_hash_aset(hash, SYM("flags"),
375
+ Encoding::ConvertToDefaultInternal(
376
+ rb_enc_str_new(
377
+ reinterpret_cast<const char*>(flags.data()),
378
+ flags.size()*2,
379
+ rb_to_encoding(Encoding::UTF16Encoding()))));
380
+ ret_ = hash;
381
+ }
382
+
383
+ void Visit(const ArrayLiteral* literal) {
384
+ VALUE hash = rb_hash_new();
385
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ArrayLiteral"));
386
+ VALUE array = rb_ary_new();
387
+ for (Expressions::const_iterator it = literal->items().begin(),
388
+ last = literal->items().end(); it != last; ++it) {
389
+ (*it)->Accept(this);
390
+ rb_ary_push(array, ret_);
391
+ }
392
+ rb_hash_aset(hash, SYM("value"), array);
393
+ ret_ = hash;
394
+ }
395
+
396
+ void Visit(const ObjectLiteral* literal) {
397
+ using std::tr1::get;
398
+ VALUE hash = rb_hash_new();
399
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ObjectLiteral"));
400
+ VALUE array = rb_ary_new();
401
+ for (ObjectLiteral::Properties::const_iterator it = literal->properties().begin(),
402
+ last = literal->properties().end(); it != last; ++it) {
403
+ VALUE item = rb_hash_new();
404
+ switch (get<0>(*it)) {
405
+ case ObjectLiteral::DATA:
406
+ rb_hash_aset(item,
407
+ SYM("type"),
408
+ rb_str_new_cstr("DataProperty"));
409
+ break;
410
+
411
+ case ObjectLiteral::SET:
412
+ rb_hash_aset(item,
413
+ SYM("type"),
414
+ rb_str_new_cstr("SetterProperty"));
415
+ break;
416
+
417
+ case ObjectLiteral::GET:
418
+ rb_hash_aset(item,
419
+ SYM("type"),
420
+ rb_str_new_cstr("GetterProperty"));
421
+ break;
422
+ }
423
+ get<1>(*it)->Accept(this);
424
+ rb_hash_aset(item,
425
+ SYM("key"),
426
+ ret_);
427
+ get<2>(*it)->Accept(this);
428
+ rb_hash_aset(item,
429
+ SYM("value"),
430
+ ret_);
431
+ rb_ary_push(array, item);
432
+ }
433
+ rb_hash_aset(hash, SYM("value"), array);
434
+ ret_ = hash;
435
+ }
436
+
437
+ void Visit(const FunctionLiteral* literal) {
438
+ VALUE hash = rb_hash_new();
439
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FunctionLiteral"));
440
+ if (literal->name()) {
441
+ Visit(literal->name());
442
+ rb_hash_aset(hash, SYM("name"), ret_);
443
+ }
444
+ {
445
+ VALUE array = rb_ary_new();
446
+ for (Identifiers::const_iterator it = literal->params().begin(),
447
+ last = literal->params().end(); it != last; ++it) {
448
+ (*it)->Accept(this);
449
+ rb_ary_push(array, ret_);
450
+ }
451
+ rb_hash_aset(hash, SYM("params"), array);
452
+ }
453
+ {
454
+ VALUE array = rb_ary_new();
455
+ for (Statements::const_iterator it = literal->body().begin(),
456
+ last = literal->body().end(); it != last; ++it) {
457
+ (*it)->Accept(this);
458
+ rb_ary_push(array, ret_);
459
+ }
460
+ rb_hash_aset(hash, SYM("body"), array);
461
+ }
462
+ ret_ = hash;
463
+ }
464
+
465
+ void Visit(const IndexAccess* expr) {
466
+ VALUE hash = rb_hash_new();
467
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("IndexAccess"));
468
+ expr->target()->Accept(this);
469
+ rb_hash_aset(hash, SYM("target"), ret_);
470
+ expr->key()->Accept(this);
471
+ rb_hash_aset(hash, SYM("key"), ret_);
472
+ ret_ = hash;
473
+ }
474
+
475
+ void Visit(const IdentifierAccess* expr) {
476
+ VALUE hash = rb_hash_new();
477
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("IdentifierAccess"));
478
+ expr->target()->Accept(this);
479
+ rb_hash_aset(hash, SYM("target"), ret_);
480
+ Visit(expr->key());
481
+ rb_hash_aset(hash, SYM("key"), ret_);
482
+ ret_ = hash;
483
+ }
484
+
485
+ void Visit(const FunctionCall* call) {
486
+ VALUE hash = rb_hash_new();
487
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FunctionCall"));
488
+ call->target()->Accept(this);
489
+ rb_hash_aset(hash, SYM("target"), ret_);
490
+ VALUE args = rb_ary_new();
491
+ for (Expressions::const_iterator it = call->args().begin(),
492
+ last = call->args().end(); it != last; ++it) {
493
+ (*it)->Accept(this);
494
+ rb_ary_push(args, ret_);
495
+ }
496
+ rb_hash_aset(hash, SYM("args"), args);
497
+ ret_ = hash;
498
+ }
499
+
500
+ void Visit(const ConstructorCall* call) {
501
+ VALUE hash = rb_hash_new();
502
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ConstructorCall"));
503
+ call->target()->Accept(this);
504
+ rb_hash_aset(hash, SYM("target"), ret_);
505
+ VALUE args = rb_ary_new();
506
+ for (Expressions::const_iterator it = call->args().begin(),
507
+ last = call->args().end(); it != last; ++it) {
508
+ (*it)->Accept(this);
509
+ rb_ary_push(args, ret_);
510
+ }
511
+ rb_hash_aset(hash, SYM("args"), args);
512
+ ret_ = hash;
513
+ }
514
+
515
+ void Visit(const Declaration* decl) {
516
+ UNREACHABLE();
517
+ }
518
+
519
+ void Visit(const CaseClause* clause) {
520
+ UNREACHABLE();
521
+ }
522
+
523
+ private:
524
+ VALUE ret_;
525
+ };
526
+
527
+ } } // namespace iv::phonic
528
+
529
+ #undef SYM
530
+ #endif // _IV_PHONIC_CREATOR_H_
@@ -0,0 +1,110 @@
1
+ #ifndef _IV_PHONIC_ENCODING_H_
2
+ #define _IV_PHONIC_ENCODING_H_
3
+ #include <ruby.h>
4
+ #include <ruby/encoding.h>
5
+ #include <ruby/intern.h>
6
+ namespace iv {
7
+ namespace phonic {
8
+
9
+ static VALUE CEncoding_UTF_8;
10
+ static VALUE CEncoding_UTF_16BE;
11
+ static VALUE CEncoding_UTF_16LE;
12
+ static VALUE CEncoding_UTF_16;
13
+ static VALUE CEncoding_UTF_32BE;
14
+ static VALUE CEncoding_UTF_32LE;
15
+ static VALUE CEncoding_UTF_32;
16
+ static VALUE CEncoding_ASCII_8BIT;
17
+ static ID kForceEncoding;
18
+ static ID kEncodeBang;
19
+
20
+ class Encoding {
21
+ public:
22
+ static void Init() {
23
+ CEncoding_UTF_8 = rb_funcall(
24
+ rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
25
+ CEncoding_UTF_16BE = rb_funcall(
26
+ rb_path2class("Encoding"), rb_intern("find"),
27
+ 1, rb_str_new2("utf-16be"));
28
+ CEncoding_UTF_16LE = rb_funcall(
29
+ rb_path2class("Encoding"), rb_intern("find"),
30
+ 1, rb_str_new2("utf-16le"));
31
+ CEncoding_UTF_32BE = rb_funcall(
32
+ rb_path2class("Encoding"), rb_intern("find"),
33
+ 1, rb_str_new2("utf-32be"));
34
+ CEncoding_UTF_32LE = rb_funcall(
35
+ rb_path2class("Encoding"), rb_intern("find"),
36
+ 1, rb_str_new2("utf-32le"));
37
+ if (IsLittle()) {
38
+ CEncoding_UTF_16 = CEncoding_UTF_16LE;
39
+ CEncoding_UTF_32 = CEncoding_UTF_32LE;
40
+ } else {
41
+ CEncoding_UTF_16 = CEncoding_UTF_16BE;
42
+ CEncoding_UTF_32 = CEncoding_UTF_32BE;
43
+ }
44
+ CEncoding_ASCII_8BIT = rb_funcall(
45
+ rb_path2class("Encoding"), rb_intern("find"),
46
+ 1, rb_str_new2("ascii-8bit"));
47
+ kForceEncoding = rb_intern("force_encoding");
48
+ kEncodeBang = rb_intern("encode!");
49
+ }
50
+
51
+ static inline VALUE UTF8Encoding() {
52
+ return CEncoding_UTF_8;
53
+ }
54
+
55
+ static inline VALUE ConvertToDefaultInternal(VALUE str) {
56
+ VALUE source = rb_str_dup(str);
57
+ rb_funcall(source, kEncodeBang, 1, rb_enc_default_external());
58
+ return source;
59
+ }
60
+
61
+ static inline VALUE UTF16BEEncoding() {
62
+ return CEncoding_UTF_16BE;
63
+ }
64
+
65
+ static inline VALUE UTF16LEEncoding() {
66
+ return CEncoding_UTF_16LE;
67
+ }
68
+
69
+ static inline VALUE UTF32BEEncoding() {
70
+ return CEncoding_UTF_32BE;
71
+ }
72
+
73
+ static inline VALUE UTF32LEEncoding() {
74
+ return CEncoding_UTF_32LE;
75
+ }
76
+
77
+ static inline VALUE AsciiEncoding() {
78
+ return CEncoding_ASCII_8BIT;
79
+ }
80
+
81
+ static inline VALUE UTF16Encoding() {
82
+ return CEncoding_UTF_16;
83
+ }
84
+ static inline VALUE UTF32Encoding() {
85
+ return CEncoding_UTF_32;
86
+ }
87
+
88
+ static inline bool IsLittle() {
89
+ return Which() == LITTLE;
90
+ }
91
+ static inline bool IsBig() {
92
+ return Which() == BIG;
93
+ }
94
+ private:
95
+ enum {
96
+ BIG = 0,
97
+ LITTLE = 1
98
+ };
99
+ static inline int Which() {
100
+ int x = 0x00000001;
101
+ if (*reinterpret_cast<char*>(&x)) {
102
+ return LITTLE;
103
+ } else {
104
+ return BIG;
105
+ }
106
+ }
107
+ };
108
+
109
+ } } // namespace iv::phonic
110
+ #endif // _IV_PHONIC_ENCODING_H_
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+ $CFLAGS += " -Wall -Werror -Wno-unused-parameter "
3
+ dir_config('iv', 'ext')
4
+ $CFLAGS += " -I../../include "
5
+ create_makefile('iv/phonic')