Brainfuck 解释器
作者:Daniel Carrera
灵感来自 Acme::Brainfuck
。
用法
perl6 brainfuck.p6.pl myprog.bf
以下是 Brainfuck 语言的“Hello world”程序
++++++++++ initializes cell zero to 10 [ >+++++++>++++++++++>+++>+<<<<- ] loop sets the next four cells to 70/100/30/10 >++. print 'H' >+. print 'e' +++++++. 'l' . 'l' +++. 'o' >++. space <<+++++++++++++++. 'W' >. 'o' +++. 'r' ------. 'l' --------. 'd' >+. '!' >. newline
源代码:brainfuck.p6
use v6; use MONKEY-SEE-NO-EVAL; # we need to parse user input my $hello-bf = " ++++++++++ initializes cell zero to 10 [ >+++++++>++++++++++>+++>+<<<<- ] loop sets the next four cells to 70/100/30/10 >++. print 'H' >+. print 'e' +++++++. 'l' . 'l' +++. 'o' >++. space <<+++++++++++++++. 'W' >. 'o' +++. 'r' ------. 'l' --------. 'd' >+. '!' >. newline "; sub MAIN($input = "") { # Read the program. my $program = $input eq "" ?? $hello-bf !! $input.IO.slurp; # Compile to Perl 6. $program .= subst(/ <-[+\-<>,.\[\]]> /, '', :g); $program .= subst(/(\++)/, { 'P += ' ~ $0.chars ~ ";" }, :g); $program .= subst(/(\-+)/, { 'P -= ' ~ $0.chars ~ ";" }, :g); $program .= subst(/(\>+)/, { '$ptr += ' ~ $0.chars ~ ";" }, :g); $program .= subst(/(\<+)/, { '$ptr -= ' ~ $0.chars ~ ";" }, :g); $program .= subst(/\./, "print chr P;", :g); $program .= subst(/\,/, "P = ord getc;", :g); $program .= subst(/\[/, 'while (P) {', :g); $program .= subst(/\]/, '};', :g); $program .= subst(/P/, '@P[$ptr]', :g); $program = 'my @P = (); my $ptr = 0;' ~ $program; # Run EVAL $program; }