kanayago 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +15 -0
  3. data/.rubocop_todo.yml +23 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +79 -0
  6. data/Rakefile +182 -0
  7. data/ext/kanayago/ccan/check_type/check_type.h +63 -0
  8. data/ext/kanayago/ccan/container_of/container_of.h +142 -0
  9. data/ext/kanayago/ccan/list/list.h +791 -0
  10. data/ext/kanayago/ccan/str/str.h +17 -0
  11. data/ext/kanayago/constant.h +53 -0
  12. data/ext/kanayago/extconf.rb +21 -0
  13. data/ext/kanayago/id.h +347 -0
  14. data/ext/kanayago/id_table.h +39 -0
  15. data/ext/kanayago/internal/array.h +151 -0
  16. data/ext/kanayago/internal/basic_operators.h +64 -0
  17. data/ext/kanayago/internal/bignum.h +244 -0
  18. data/ext/kanayago/internal/bits.h +568 -0
  19. data/ext/kanayago/internal/compile.h +34 -0
  20. data/ext/kanayago/internal/compilers.h +107 -0
  21. data/ext/kanayago/internal/complex.h +29 -0
  22. data/ext/kanayago/internal/encoding.h +36 -0
  23. data/ext/kanayago/internal/error.h +218 -0
  24. data/ext/kanayago/internal/fixnum.h +184 -0
  25. data/ext/kanayago/internal/gc.h +322 -0
  26. data/ext/kanayago/internal/hash.h +191 -0
  27. data/ext/kanayago/internal/imemo.h +261 -0
  28. data/ext/kanayago/internal/io.h +140 -0
  29. data/ext/kanayago/internal/numeric.h +274 -0
  30. data/ext/kanayago/internal/parse.h +117 -0
  31. data/ext/kanayago/internal/rational.h +71 -0
  32. data/ext/kanayago/internal/re.h +28 -0
  33. data/ext/kanayago/internal/ruby_parser.h +125 -0
  34. data/ext/kanayago/internal/sanitizers.h +297 -0
  35. data/ext/kanayago/internal/serial.h +23 -0
  36. data/ext/kanayago/internal/static_assert.h +16 -0
  37. data/ext/kanayago/internal/string.h +186 -0
  38. data/ext/kanayago/internal/symbol.h +45 -0
  39. data/ext/kanayago/internal/thread.h +79 -0
  40. data/ext/kanayago/internal/variable.h +72 -0
  41. data/ext/kanayago/internal/vm.h +137 -0
  42. data/ext/kanayago/internal/warnings.h +16 -0
  43. data/ext/kanayago/internal.h +108 -0
  44. data/ext/kanayago/kanayago.c +420 -0
  45. data/ext/kanayago/kanayago.h +21 -0
  46. data/ext/kanayago/lex.c +302 -0
  47. data/ext/kanayago/method.h +255 -0
  48. data/ext/kanayago/node.c +440 -0
  49. data/ext/kanayago/node.h +111 -0
  50. data/ext/kanayago/node_name.inc +224 -0
  51. data/ext/kanayago/parse.c +26931 -0
  52. data/ext/kanayago/parse.h +244 -0
  53. data/ext/kanayago/parse.tmp.y +16145 -0
  54. data/ext/kanayago/parser_bits.h +564 -0
  55. data/ext/kanayago/parser_node.h +32 -0
  56. data/ext/kanayago/parser_st.c +164 -0
  57. data/ext/kanayago/parser_st.h +162 -0
  58. data/ext/kanayago/parser_value.h +106 -0
  59. data/ext/kanayago/probes.h +4 -0
  60. data/ext/kanayago/ruby_assert.h +14 -0
  61. data/ext/kanayago/ruby_atomic.h +23 -0
  62. data/ext/kanayago/ruby_parser.c +1165 -0
  63. data/ext/kanayago/rubyparser.h +1391 -0
  64. data/ext/kanayago/shape.h +234 -0
  65. data/ext/kanayago/st.c +2339 -0
  66. data/ext/kanayago/symbol.h +123 -0
  67. data/ext/kanayago/thread_pthread.h +168 -0
  68. data/ext/kanayago/universal_parser.c +230 -0
  69. data/ext/kanayago/vm_core.h +2215 -0
  70. data/ext/kanayago/vm_opts.h +67 -0
  71. data/lib/kanayago/version.rb +5 -0
  72. data/lib/kanayago.rb +11 -0
  73. data/sig/kanayago.rbs +4 -0
  74. metadata +116 -0
@@ -0,0 +1,420 @@
1
+ #include "kanayago.h"
2
+ #include "internal/ruby_parser.h"
3
+ #include "ruby/ruby.h"
4
+ #include "rubyparser.h"
5
+ #include <stdio.h>
6
+
7
+ #define symbol(arg) \
8
+ ID2SYM(rb_intern((arg)))
9
+
10
+ VALUE rb_mKanayago;
11
+
12
+ static VALUE ast_to_hash(const NODE *);
13
+
14
+ static VALUE
15
+ node_opcall_to_hash(const NODE *node)
16
+ {
17
+ VALUE result = rb_hash_new();
18
+ rb_hash_aset(result, symbol("recv"), ast_to_hash(RNODE_OPCALL(node)->nd_recv));
19
+ rb_hash_aset(result, symbol("mid"), ID2SYM(RNODE_OPCALL(node)->nd_mid));
20
+ rb_hash_aset(result, symbol("args"), ast_to_hash(RNODE_OPCALL(node)->nd_args));
21
+ return result;
22
+ }
23
+
24
+ static VALUE
25
+ node_call_to_hash(const NODE *node)
26
+ {
27
+ VALUE result = rb_hash_new();
28
+
29
+ rb_hash_aset(result, symbol("recv"), ast_to_hash(RNODE_OPCALL(node)->nd_recv));
30
+ rb_hash_aset(result, symbol("mid"), ID2SYM(RNODE_CALL(node)->nd_mid));
31
+ rb_hash_aset(result, symbol("args"), ast_to_hash(RNODE_CALL(node)->nd_args));
32
+
33
+ return result;
34
+ }
35
+
36
+ static VALUE
37
+ node_fcall_to_hash(const NODE *node)
38
+ {
39
+ VALUE result = rb_hash_new();
40
+
41
+ rb_hash_aset(result, symbol("mid"), ID2SYM(RNODE_FCALL(node)->nd_mid));
42
+ rb_hash_aset(result, symbol("args"), ast_to_hash(RNODE_FCALL(node)->nd_args));
43
+
44
+ return result;
45
+ }
46
+
47
+ static VALUE
48
+ node_list_to_hash(const NODE *node)
49
+ {
50
+ VALUE result = rb_ary_new();
51
+ NODE *nd_head = RNODE_LIST(node)->nd_head;
52
+ int list_len = RNODE_LIST(node)->as.nd_alen;
53
+
54
+ for (int i = 0; i < list_len; i++ ) {
55
+ rb_ary_push(result, ast_to_hash(nd_head));
56
+ nd_head = RNODE_LIST(node)->nd_next;
57
+ }
58
+
59
+ return result;
60
+ }
61
+
62
+ static VALUE
63
+ node_defn_to_hash(const NODE *node)
64
+ {
65
+ VALUE result = rb_hash_new();
66
+
67
+ rb_hash_aset(result, symbol("mid"), ID2SYM(RNODE_DEFN(node)->nd_mid));
68
+ rb_hash_aset(result, symbol("defn"), ast_to_hash(RNODE_DEFN(node)->nd_defn));
69
+
70
+ return result;
71
+ }
72
+
73
+ static VALUE
74
+ node_block_to_hash(const NODE *node)
75
+ {
76
+ VALUE result = rb_ary_new();
77
+ const NODE *current_node = node;
78
+
79
+ while (current_node) {
80
+ rb_ary_push(result, ast_to_hash(RNODE_BLOCK(current_node)->nd_head));
81
+ current_node = RNODE_BLOCK(current_node)->nd_next;
82
+ }
83
+
84
+ return result;
85
+ }
86
+
87
+ static VALUE
88
+ node_lasgn_to_hash(const NODE *node)
89
+ {
90
+ VALUE result = rb_hash_new();
91
+
92
+ rb_hash_aset(result, symbol("id"), ID2SYM(RNODE_LASGN(node)->nd_vid));
93
+ rb_hash_aset(result, symbol("value"), ast_to_hash(RNODE_LASGN(node)->nd_value));
94
+
95
+ return result;
96
+ }
97
+
98
+ static VALUE
99
+ node_lvar_to_hash(const NODE *node)
100
+ {
101
+ VALUE result = rb_hash_new();
102
+
103
+ rb_hash_aset(result, symbol("vid"), ID2SYM(RNODE_LVAR(node)->nd_vid));
104
+
105
+ return result;
106
+ }
107
+
108
+ static VALUE
109
+ node_if_to_hash(const NODE *node)
110
+ {
111
+ VALUE result = rb_hash_new();
112
+
113
+ rb_hash_aset(result, symbol("cond"), ast_to_hash(RNODE_IF(node)->nd_cond));
114
+ rb_hash_aset(result, symbol("body"), ast_to_hash(RNODE_IF(node)->nd_body));
115
+ rb_hash_aset(result, symbol("else"), ast_to_hash(RNODE_IF(node)->nd_else));
116
+
117
+ return result;
118
+ }
119
+
120
+ static VALUE
121
+ node_unless_to_hash(const NODE *node)
122
+ {
123
+ VALUE result = rb_hash_new();
124
+
125
+ rb_hash_aset(result, symbol("cond"), ast_to_hash(RNODE_UNLESS(node)->nd_cond));
126
+ rb_hash_aset(result, symbol("body"), ast_to_hash(RNODE_UNLESS(node)->nd_body));
127
+ rb_hash_aset(result, symbol("else"), ast_to_hash(RNODE_UNLESS(node)->nd_else));
128
+
129
+ return result;
130
+ }
131
+
132
+ static VALUE
133
+ node_const_to_hash(const NODE *node)
134
+ {
135
+ VALUE result = rb_hash_new();
136
+
137
+ rb_hash_aset(result, symbol("vid"), ID2SYM(RNODE_CONST(node)->nd_vid));
138
+
139
+ return result;
140
+ }
141
+
142
+ static VALUE
143
+ node_cdecl_to_hash(const NODE *node)
144
+ {
145
+ VALUE result = rb_hash_new();
146
+
147
+ rb_hash_aset(result, symbol("vid"), ID2SYM(RNODE_CDECL(node)->nd_vid));
148
+ rb_hash_aset(result, symbol("else"), ast_to_hash(RNODE_CDECL(node)->nd_else));
149
+ rb_hash_aset(result, symbol("value"), ast_to_hash(RNODE_CDECL(node)->nd_value));
150
+
151
+ return result;
152
+ }
153
+
154
+ static VALUE
155
+ node_literal_to_hash(const NODE *node)
156
+ {
157
+ enum node_type type = nd_type(node);
158
+
159
+ switch (type) {
160
+ case NODE_INTEGER: {
161
+ VALUE result = rb_hash_new();
162
+ rb_hash_aset(result, symbol("NODE_INTEGER"), rb_node_integer_literal_val(node));
163
+ return result;
164
+ }
165
+ case NODE_FLOAT: {
166
+ VALUE result = rb_hash_new();
167
+ rb_hash_aset(result, symbol("NODE_FLOAT"), rb_node_float_literal_val(node));
168
+ return result;
169
+ }
170
+ case NODE_RATIONAL: {
171
+ VALUE result = rb_hash_new();
172
+ rb_hash_aset(result, symbol("NODE_RATIONAL"), rb_node_rational_literal_val(node));
173
+ return result;
174
+ }
175
+ case NODE_IMAGINARY: {
176
+ VALUE result = rb_hash_new();
177
+ rb_hash_aset(result, symbol("NODE_IMAGINARY"), rb_node_imaginary_literal_val(node));
178
+ return result;
179
+ }
180
+ case NODE_STR: {
181
+ VALUE result = rb_hash_new();
182
+ rb_hash_aset(result, symbol("NODE_STR"), rb_node_str_string_val(node));
183
+ return result;
184
+ }
185
+ case NODE_SYM: {
186
+ VALUE result = rb_hash_new();
187
+ rb_hash_aset(result, symbol("NODE_SYM"), rb_node_sym_string_val(node));
188
+ return result;
189
+ }
190
+ default:
191
+ return Qnil;
192
+ }
193
+ }
194
+
195
+ static VALUE
196
+ node_class_to_hash(const NODE *node)
197
+ {
198
+ VALUE result = rb_hash_new();
199
+
200
+ rb_hash_aset(result, symbol("cpath"), ast_to_hash(RNODE_CLASS(node)->nd_cpath));
201
+ rb_hash_aset(result, symbol("super"), ast_to_hash(RNODE_CLASS(node)->nd_super));
202
+ rb_hash_aset(result, symbol("body"), ast_to_hash(RNODE_CLASS(node)->nd_body));
203
+
204
+ return result;
205
+ }
206
+
207
+ static VALUE
208
+ node_colon2_to_hash(const NODE *node)
209
+ {
210
+ VALUE result = rb_hash_new();
211
+
212
+ rb_hash_aset(result, symbol("mid"), ID2SYM(RNODE_COLON2(node)->nd_mid));
213
+ rb_hash_aset(result, symbol("head"), ast_to_hash(RNODE_COLON2(node)->nd_head));
214
+
215
+ return result;
216
+ }
217
+
218
+ static VALUE
219
+ node_begin_to_hash(const NODE *node)
220
+ {
221
+ VALUE result = rb_hash_new();
222
+
223
+ rb_hash_aset(result, symbol("body"), ast_to_hash(RNODE_BEGIN(node)->nd_body));
224
+
225
+ return result;
226
+ }
227
+
228
+ static VALUE
229
+ node_scope_to_hash(const NODE *node)
230
+ {
231
+ VALUE result = rb_hash_new();
232
+
233
+ rb_hash_aset(result, symbol("args"), ast_to_hash((const NODE *)(RNODE_SCOPE(node)->nd_args)));
234
+ rb_hash_aset(result, symbol("body"), ast_to_hash(RNODE_SCOPE(node)->nd_body));
235
+
236
+ return result;
237
+ }
238
+
239
+ static VALUE
240
+ args_ainfo_to_hash(const struct rb_args_info ainfo)
241
+ {
242
+ VALUE result = rb_hash_new();
243
+
244
+ rb_hash_aset(result, symbol("forwarding"), INT2NUM(ainfo.forwarding));
245
+ rb_hash_aset(result, symbol("pre_args_num"), INT2NUM(ainfo.pre_args_num));
246
+ rb_hash_aset(result, symbol("pre_init"), ast_to_hash(ainfo.pre_init));
247
+ rb_hash_aset(result, symbol("post_args_num"), INT2NUM(ainfo.post_args_num));
248
+ rb_hash_aset(result, symbol("post_init"), ast_to_hash(ainfo.post_init));
249
+ rb_hash_aset(result, symbol("first_post_arg"), Qnil);
250
+ rb_hash_aset(result, symbol("rest_arg"), Qnil);
251
+ rb_hash_aset(result, symbol("block_arg"), Qnil);
252
+ rb_hash_aset(result, symbol("opt_args"), ast_to_hash((const NODE *)(ainfo.opt_args)));
253
+ rb_hash_aset(result, symbol("kw_args"), ast_to_hash((const NODE *)(ainfo.kw_args)));
254
+ rb_hash_aset(result, symbol("kw_rest_arg"), ast_to_hash(ainfo.kw_rest_arg));
255
+
256
+ return result;
257
+ }
258
+
259
+ static VALUE
260
+ node_args_to_hash(const NODE *node)
261
+ {
262
+ VALUE result = rb_hash_new();
263
+ VALUE ainfo_hash = args_ainfo_to_hash(RNODE_ARGS(node)->nd_ainfo);
264
+
265
+ rb_hash_aset(result, symbol("ainfo"), ainfo_hash);
266
+
267
+ return result;
268
+ }
269
+
270
+ static VALUE
271
+ node_ivar_to_hash(const NODE *node)
272
+ {
273
+ VALUE result = rb_hash_new();
274
+
275
+ rb_hash_aset(result, symbol("vid"), ID2SYM(RNODE_IVAR(node)->nd_vid));
276
+
277
+ return result;
278
+ }
279
+
280
+ static VALUE
281
+ ast_to_hash(const NODE *node)
282
+ {
283
+ enum node_type type;
284
+
285
+ if (!node) {
286
+ return Qnil;
287
+ }
288
+
289
+ type = nd_type(node);
290
+
291
+ switch (type) {
292
+ case NODE_SCOPE: {
293
+ VALUE result = rb_hash_new();
294
+ rb_hash_aset(result, symbol("NODE_SCOPE"), node_scope_to_hash(node));
295
+ return result;
296
+ }
297
+ case NODE_CLASS: {
298
+ VALUE result = rb_hash_new();
299
+ rb_hash_aset(result, symbol("NODE_CLASS"), node_class_to_hash(node));
300
+ return result;
301
+ }
302
+ case NODE_DEFN: {
303
+ VALUE result = rb_hash_new();
304
+ rb_hash_aset(result, symbol("NODE_DEFN"), node_defn_to_hash(node));
305
+ return result;
306
+ }
307
+ case NODE_OPCALL: {
308
+ VALUE result = rb_hash_new();
309
+ rb_hash_aset(result, symbol("NODE_OPCALL"), node_opcall_to_hash(node));
310
+ return result;
311
+ }
312
+ case NODE_FCALL: {
313
+ VALUE result = rb_hash_new();
314
+ rb_hash_aset(result, symbol("NODE_FCALL"), node_fcall_to_hash(node));
315
+ return result;
316
+ }
317
+ case NODE_CALL: {
318
+ VALUE result = rb_hash_new();
319
+ rb_hash_aset(result, symbol("NODE_CALL"), node_call_to_hash(node));
320
+ return result;
321
+ }
322
+ case NODE_ARGS: {
323
+ VALUE result = rb_hash_new();
324
+ rb_hash_aset(result, symbol("NODE_ARGS"), node_args_to_hash(node));
325
+ return result;
326
+ }
327
+ case NODE_BLOCK: {
328
+ VALUE result = rb_hash_new();
329
+ rb_hash_aset(result, symbol("NODE_BLOCK"), node_block_to_hash(node));
330
+ return result;
331
+ }
332
+ case NODE_LASGN: {
333
+ VALUE result = rb_hash_new();
334
+ rb_hash_aset(result, symbol("NODE_LASGN"), node_lasgn_to_hash(node));
335
+ return result;
336
+ }
337
+ case NODE_LVAR: {
338
+ VALUE result = rb_hash_new();
339
+ rb_hash_aset(result, symbol("NODE_LVAR"), node_lvar_to_hash(node));
340
+ return result;
341
+ }
342
+ case NODE_IF: {
343
+ VALUE result = rb_hash_new();
344
+ rb_hash_aset(result, symbol("NODE_IF"), node_if_to_hash(node));
345
+ return result;
346
+ }
347
+ case NODE_UNLESS: {
348
+ VALUE result = rb_hash_new();
349
+ rb_hash_aset(result, symbol("NODE_UNLESS"), node_unless_to_hash(node));
350
+ return result;
351
+ }
352
+ case NODE_LIST: {
353
+ VALUE result = rb_hash_new();
354
+ rb_hash_aset(result, symbol("NODE_LIST"), node_list_to_hash(node));
355
+ return result;
356
+ }
357
+ case NODE_CONST: {
358
+ VALUE result = rb_hash_new();
359
+ rb_hash_aset(result, symbol("NODE_CONST"), node_const_to_hash(node));
360
+ return result;
361
+ }
362
+ case NODE_CDECL: {
363
+ VALUE result = rb_hash_new();
364
+ rb_hash_aset(result, symbol("NODE_CDECL"), node_cdecl_to_hash(node));
365
+ return result;
366
+ }
367
+ case NODE_COLON2: {
368
+ VALUE result = rb_hash_new();
369
+ rb_hash_aset(result, symbol("NODE_COLON2"), node_colon2_to_hash(node));
370
+ return result;
371
+ }
372
+ case NODE_BEGIN: {
373
+ VALUE result = rb_hash_new();
374
+ rb_hash_aset(result, symbol("NODE_BEGIN"), node_begin_to_hash(node));
375
+ return result;
376
+ }
377
+ case NODE_IVAR: {
378
+ VALUE result = rb_hash_new();
379
+ rb_hash_aset(result, symbol("NODE_IVAR"), node_ivar_to_hash(node));
380
+ return result;
381
+ }
382
+ case NODE_INTEGER:
383
+ case NODE_FLOAT:
384
+ case NODE_RATIONAL:
385
+ case NODE_IMAGINARY:
386
+ case NODE_STR:
387
+ case NODE_SYM:
388
+ return node_literal_to_hash(node);
389
+ default:
390
+ return Qfalse;
391
+ }
392
+ }
393
+
394
+ static VALUE
395
+ kanayago_parse(VALUE self, VALUE source)
396
+ {
397
+ struct ruby_parser *parser;
398
+ rb_parser_t *parser_params;
399
+
400
+ parser_params = rb_parser_params_new();
401
+ VALUE vparser = TypedData_Make_Struct(0, struct ruby_parser,
402
+ &ruby_parser_data_type, parser);
403
+ parser->parser_params = parser_params;
404
+
405
+
406
+ VALUE vast = rb_parser_compile_string(vparser, "main", source, 0);
407
+
408
+ rb_ast_t *ast;
409
+
410
+ TypedData_Get_Struct(vast, rb_ast_t, &ast_data_type, ast);
411
+
412
+ return ast_to_hash(ast->body.root);
413
+ }
414
+
415
+ RUBY_FUNC_EXPORTED void
416
+ Init_kanayago(void)
417
+ {
418
+ rb_mKanayago = rb_define_module("Kanayago");
419
+ rb_define_module_function(rb_mKanayago, "kanayago_parse", kanayago_parse, 1);
420
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef KANAYAGO_H
2
+ #define KANAYAGO_H 1
3
+
4
+ #include "node.h"
5
+ #include "internal/ruby_parser.h"
6
+
7
+ rb_parser_t *rb_parser_params_new(void);
8
+ VALUE rb_parser_compile_string(VALUE, const char *, VALUE, int);
9
+ VALUE rb_node_integer_literal_val(const NODE *);
10
+ VALUE rb_node_float_literal_val(const NODE *);
11
+ VALUE rb_node_rational_literal_val(const NODE *);
12
+ VALUE rb_node_imaginary_literal_val(const NODE *);
13
+ VALUE rb_node_str_string_val(const NODE *);
14
+ VALUE rb_node_sym_string_val(const NODE *);
15
+
16
+ // Add extern for Kanayago
17
+ extern const rb_data_type_t ruby_parser_data_type;
18
+ extern const rb_data_type_t ast_data_type;
19
+ // End for Kanayago
20
+
21
+ #endif /* KANAYAGO_H */