Comment detail

BFコンパイラー (Nested Flatten)
処理系間の差を吸収するため,入出力用の関数を受け取る関数を生成。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
function BF(code){
  var s = '', o = {
    '>': 'm[p+=?]|=0;',
    '<': 'm[p-=?]|=0;',
    '+': 'm[p]+=?;',
    '-': 'm[p]-=?;',
    ',': 'm[p]=get().charCodeAt(0);',
    '.': 'put(c(m[p]));',
    '[': 'while(m[p]){',
    ']': '}' };
  code.replace(/>+|<+|\++|-+|([,.[\]])/g, function($, _){
    s += _ ? o[$] : o[$.charAt(0)].replace('?', $.length) });
  return eval('0,function(get,put){var m=[0],p=0,c=String.fromCharCode;'+ s +'}');
}
/// Rhinoで「hello world」のテスト ///
BF('++++++++++[>+++++++>++++++++++>+++>+<<<<-]\
    >++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'
)(function(){ return '?' }, function(c){ java.lang.System.out.print(c) });

/// WSHで「echo」のテスト ///
//BF(',+[-.,+]')(function(){ return WSH.stdIn.read(1) }, function(c){ WSH.stdOut.write(c) });

Index

Feed

Other

Link

Pathtraq

loading...