あいまいなRubyの私
なんとなく面白いかなと思ってfuzzy rubyつくりました。
http://github.com/technohippy/fruby
count = 0 1000.times do if true then count += 1 end end puts count #=> 1000
if ... then はこれまでどおり100%。
count = 0 1000.times do if true probably count += 1 end end puts count #=> 839
if ... probably は80%。
count = 0 1000.times do if true maybe count += 1 end end puts count #=> 471
if ... maybe は50%。
count = 0 1000.times do if true perhaps count += 1 end end puts count #=> 188
if ... perhaps は20%。
できてみるとそんなに面白くなくてテンション上がらないんだけど、if notをunlessと書けることが大ウケしたと噂の英語圏の人たちは喜んでくれると信じてる。
・・・
実装は単純で、parse.yを修正して、thenの代わりにperhaps, maybe, probablyが使われたとき、条件文に && 0.5 <= rand()
とかを追加するだけ。主な変更は以下。
static NODE *cond_gen(struct parser_params*,NODE*, float); #define cond(node) cond_gen(parser, node, 1.0) #define mcond(node,maybe) cond_gen(parser, node, maybe)
| k_if expr_value maybe compstmt if_tail k_end { /*%%%*/ $$ = NEW_IF(mcond($2, 0.5), $4, $5); fixpos($$, $2); /*% $$ = dispatch3(if, $2, $4, escape_Qundef($5)); %*/ }
static NODE* cond_gen(struct parser_params *parser, NODE *node, float maybe) { NODE *mcond; if (node == 0) return 0; if (maybe == 1.0) { // then return cond0(parser, node); } else { // perhaps, maybe, probably mcond = call_bin_op(NEW_LIT(rb_float_new(maybe)), tGEQ, NEW_FCALL(rb_intern("rand"), 0)); return cond_gen(parser, NEW_NODE(NODE_AND, node, mcond, 0), 1.0); } }
なんとなく動いたり動かなかったりするプログラムが必要なときに、ぜひともご利用下さい。