-
Notifications
You must be signed in to change notification settings - Fork 0
/
golf_prelude.c
215 lines (206 loc) · 5.26 KB
/
golf_prelude.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*-c-*-
THIS FILE WAS AUTOGENERATED BY template/prelude.c.tmpl. DO NOT EDIT.
sources: golf_prelude
*/
#include "ruby/ruby.h"
#include "internal.h"
#include "vm_core.h"
#include "iseq.h"
static const char prelude_name0[] = "<internal:golf_prelude>";
static const struct {
char L0[462]; /* 1..18 */
char L18[485]; /* 19..34 */
char L34[505]; /* 35..59 */
char L59[487]; /* 60..96 */
char L96[493]; /* 97..116 */
char L116[222]; /* 117..131 */
} prelude_code0 = {
#line 1 "golf_prelude.rb"
"class Object\n"
" @@golf_hash = {}\n"
"\n"
" verbose, $VERBOSE = $VERBOSE, nil\n"
" def method_missing m, *a, &b\n"
" t = @@golf_hash[ [m, self.class] ] ||= matching_methods(m)[0]\n"
" if t && b\n"
" __send__(t, *a) {|*args|\n"
" b.binding.eval(\"proc{|golf_matchdata| $~ = golf_matchdata }\").call($~) if $~\n"
" b.call(*args)\n"
" }\n"
" else\n"
" t ? __send__(t, *a, &b) : super\n"
" end\n"
" end\n"
" $VERBOSE = verbose\n"
"\n"
" def matching_methods(s = '', m = callable_methods)\n"
,
#line 19 "golf_prelude.rb"
" r = /^#{s.to_s.gsub(/./){\"(.*?)\" + Regexp.escape($&)}}/\n"
" m.grep(r).sort_by do |i|\n"
" i.to_s.match(r).captures.map(&:size) << i\n"
" end\n"
" end\n"
"\n"
" def self.const_missing c\n"
" t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c, constants)[0]\n"
" t and return const_get(t)\n"
" raise NameError, \"uninitialized constant #{c}\", caller(1)\n"
" end\n"
"\n"
" def shortest_abbreviation(s = '', m = callable_methods)\n"
" s = s.to_s\n"
" our_case = (?A..?Z) === s[0]\n"
" if m.index(s.to_sym)\n"
,
#line 35 "golf_prelude.rb"
" 1.upto(s.size){|z| s.scan(/./).combination(z).map{|trial|\n"
" next unless ((?A..?Z) === trial[0]) == our_case\n"
" trial *= ''\n"
" return trial if matching_methods(trial, m)[0].to_s == s\n"
" }}\n"
" else\n"
" nil\n"
" end\n"
" end\n"
"\n"
" def callable_methods\n"
" self.class == Object ? methods + private_methods : methods\n"
" end\n"
"\n"
" private\n"
"\n"
" def h(a = 'H', b = 'w', c = '!')\n"
" puts \"#{a}ello, #{b}orld#{c}\"\n"
" end\n"
"\n"
" def f(m = 100)\n"
" 1.upto(m){|n|puts'FizzBuzz\n"
"'[i=n**4%-15,i+13]||n}\n"
" end\n"
"\n"
,
#line 60 "golf_prelude.rb"
" alias say puts\n"
"\n"
" def do_while\n"
" 0 while yield\n"
" end\n"
"\n"
" def do_until\n"
" 0 until yield\n"
" end\n"
"end\n"
"\n"
"class Array\n"
" alias old_to_s to_s\n"
" alias to_s join\n"
"end\n"
"\n"
"class FalseClass\n"
" alias old_to_s to_s\n"
" def to_s\n"
" \"\"\n"
" end\n"
"end\n"
"\n"
"class Integer\n"
" alias each times\n"
" include Enumerable\n"
"end\n"
"\n"
"class String\n"
" alias / split\n"
"\n"
" def to_a\n"
" split('')\n"
" end\n"
"\n"
" (Array.instance_methods - instance_methods - %i[to_ary transpose flatten flatten! compact compact! assoc rassoc]).each{|meth|\n"
" eval \"\n"
,
#line 97 "golf_prelude.rb"
" def #{meth}(*args, &block)\n"
" a = to_a\n"
" result = a.#{meth}(*args, &block)\n"
" replace(a.join)\n"
" if result.class == Array\n"
" Integer === result[0] ? result.pack('c*') : result.join\n"
" elsif result.class == Enumerator\n"
" result.map(&:join).to_enum\n"
" else\n"
" result\n"
" end\n"
" end\"\n"
" }\n"
"end\n"
"\n"
"class Enumerator\n"
" alias old_to_s to_s\n"
" (Array.instance_methods - instance_methods - [:replace] + [:to_s]).each{|meth|\n"
" eval \"\n"
" def #{meth}(*args, &block)\n"
,
#line 117 "golf_prelude.rb"
" to_a.#{meth}(*args, &block)\n"
" end\"\n"
" }\n"
" alias old_inspect inspect\n"
" alias inspect old_to_s\n"
"end\n"
"\n"
"class Symbol\n"
" def call(*args, &block)\n"
" proc do |recv|\n"
" recv.__send__(self, *args, &block)\n"
" end\n"
" end\n"
"end\n"
#line 163 "golf_prelude.c"
};
#define PRELUDE_NAME(n) rb_usascii_str_new_static(prelude_name##n, sizeof(prelude_name##n)-1)
#define PRELUDE_CODE(n) rb_utf8_str_new_static(prelude_code##n.L0, sizeof(prelude_code##n))
static rb_ast_t *
prelude_ast(VALUE name, VALUE code, int line)
{
rb_ast_t *ast = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
if (!ast->body.root) {
rb_ast_dispose(ast);
rb_exc_raise(rb_errinfo());
}
return ast;
}
COMPILER_WARNING_PUSH
#if GCC_VERSION_SINCE(4, 2, 0)
COMPILER_WARNING_ERROR(-Wmissing-field-initializers)
#endif
static void
prelude_eval(VALUE code, VALUE name, int line)
{
static const rb_compile_option_t optimization = {
TRUE, /* int inline_const_cache; */
TRUE, /* int peephole_optimization; */
FALSE,/* int tailcall_optimization; */
TRUE, /* int specialized_instruction; */
TRUE, /* int operands_unification; */
TRUE, /* int instructions_unification; */
TRUE, /* int stack_caching; */
TRUE, /* int frozen_string_literal; */
FALSE, /* int debug_frozen_string_literal; */
FALSE, /* unsigned int coverage_enabled; */
0, /* int debug_level; */
};
rb_ast_t *ast = prelude_ast(name, code, line);
rb_iseq_eval(rb_iseq_new_with_opt(&ast->body, name, name, Qnil, INT2FIX(line),
NULL, ISEQ_TYPE_TOP, &optimization));
rb_ast_dispose(ast);
}
COMPILER_WARNING_POP
void
Init_golf(void)
{
prelude_eval(PRELUDE_CODE(0), PRELUDE_NAME(0), 1);
#if 0
printf("%.*s", (int)sizeof(prelude_code0), prelude_code0.L0);
#endif
}