Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include "ruby/ruby.h"
00007 #include "vm_core.h"
00008
00009
00010 static const char prelude_name0[] = "<internal:golf_prelude>";
00011 static const char prelude_code0[] =
00012 "class Object\n"
00013 " @@golf_hash = {}\n"
00014 " def method_missing m, *a, &b\n"
00015 " t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0]\n"
00016 " if t && b\n"
00017 " __send__(t, *a) {|*args|\n"
00018 " b.binding.eval(\"proc{|golf_matchdata| $~ = golf_matchdata }\").call($~) if $~\n"
00019 " b.call(*args)\n"
00020 " }\n"
00021 " else\n"
00022 " t ? __send__(t, *a, &b) : super\n"
00023 " end\n"
00024 " end\n"
00025 "\n"
00026 " def matching_methods(s='', m=callable_methods)\n"
00027 " r=/^#{s.to_s.gsub(/./){\"(.*?)\"+Regexp.escape($&)}}/\n"
00028 " m.grep(r).sort_by do |i|\n"
00029 " i.to_s.match(r).captures.map(&:size) << i\n"
00030 " end\n"
00031 " end\n"
00032 "\n"
00033 " def self.const_missing c\n"
00034 " t = @@golf_hash[ [c,self.class] ] ||= matching_methods(c,constants)[0]\n"
00035 " t and return const_get(t)\n"
00036 " raise NameError, \"uninitialized constant #{c}\", caller(1)\n"
00037 " end\n"
00038 "\n"
00039 " def shortest_abbreviation(s='', m=callable_methods)\n"
00040 " s=s.to_s\n"
00041 " our_case = (?A..?Z)===s[0]\n"
00042 " if m.index(s.to_sym)\n"
00043 " 1.upto(s.size){|z|s.scan(/./).combination(z).map{|trial|\n"
00044 " next unless ((?A..?Z)===trial[0]) == our_case\n"
00045 " trial*=''\n"
00046 " return trial if matching_methods(trial,m)[0].to_s==s\n"
00047 " }}\n"
00048 " else\n"
00049 " nil\n"
00050 " end\n"
00051 " end\n"
00052 "\n"
00053 " def callable_methods\n"
00054 " self.class == Object ? methods + private_methods : methods\n"
00055 " end\n"
00056 "\n"
00057 " private\n"
00058 "\n"
00059 " def h(a='H', b='w', c='!')\n"
00060 " puts \"#{a}ello, #{b}orld#{c}\"\n"
00061 " end\n"
00062 "\n"
00063 " alias say puts\n"
00064 "\n"
00065 " def do_while\n"
00066 " 0 while yield\n"
00067 " end\n"
00068 "\n"
00069 " def do_until\n"
00070 " 0 until yield\n"
00071 " end\n"
00072 "end\n"
00073 "\n"
00074 "class Array\n"
00075 " alias old_to_s to_s\n"
00076 " alias to_s join\n"
00077 "end\n"
00078 "\n"
00079 "class FalseClass\n"
00080 " alias old_to_s to_s\n"
00081 " def to_s\n"
00082 " \"\"\n"
00083 " end\n"
00084 "end\n"
00085 "\n"
00086 "class Integer\n"
00087 " alias each times\n"
00088 " include Enumerable\n"
00089 "end\n"
00090 "\n"
00091 "class String\n"
00092 " alias / split\n"
00093 "\n"
00094 " def to_a\n"
00095 " split('')\n"
00096 " end\n"
00097 "\n"
00098 " (Array.instance_methods-instance_methods-[:to_ary,:transpose,:flatten,:flatten!,:compact,:compact!,:assoc,:rassoc]).each{|meth|\n"
00099 " eval\"\n"
00100 " def #{meth}(*args, &block)\n"
00101 " a=to_a\n"
00102 " result = a.#{meth}(*args, &block)\n"
00103 " replace(a.join)\n"
00104 " if result.class == Array\n"
00105 " Integer===result[0] ? result.pack('c*') : result.join\n"
00106 " elsif result.class == Enumerator\n"
00107 " result.map(&:join).to_enum\n"
00108 " else\n"
00109 " result\n"
00110 " end\n"
00111 " end\"\n"
00112 " }\n"
00113 "end\n"
00114 "\n"
00115 "class Enumerator\n"
00116 " alias old_to_s to_s\n"
00117 " (Array.instance_methods-instance_methods-[:replace]+[:to_s]).each{|meth|\n"
00118 " eval\"\n"
00119 " def #{meth}(*args, &block)\n"
00120 " to_a.#{meth}(*args, &block)\n"
00121 " end\"\n"
00122 " }\n"
00123 " alias old_inspect inspect\n"
00124 " alias inspect old_to_s\n"
00125 "end\n"
00126 ;
00127
00128 #define PRELUDE_COUNT 0
00129
00130
00131 VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE filepath, VALUE line, VALUE opt);
00132
00133 static void
00134 prelude_eval(VALUE code, VALUE name, VALUE line)
00135 {
00136 rb_iseq_eval(rb_iseq_compile_with_option(code, name, Qnil, line, Qtrue));
00137 }
00138
00139 void
00140 Init_golf(void)
00141 {
00142 prelude_eval(
00143 rb_usascii_str_new(prelude_code0, sizeof(prelude_code0) - 1),
00144 rb_usascii_str_new(prelude_name0, sizeof(prelude_name0) - 1),
00145 INT2FIX(1));
00146
00147 #if 0
00148 puts(prelude_code0);
00149 #endif
00150 }
00151