Jump to content

Execute Brain****/Raku

From Rosetta Code
Works with: Rakudo version 2018.03
class BFInterpreter {
    has @.code;
    has @!mem;
    has @!loop_stack;
    has @!input_buffer;
    has $!m;
    has $!c;

    method new (Str $code) {
        BFInterpreter.bless(code => $code.lines.comb);
    }

    method run {
        $!c = 0;
        $!m = 0;
        while $!c < @.code {
            given @.code[$!c] {
                when '>' { $!m++ }
                when '<' { $!m-- }
                when '+' { @!mem[$!m]++ }
                when '-' { @!mem[$!m]-- }
                when '.' { @!mem[$!m].chr.print }
                when ',' {
                    @!input_buffer = $*IN.get.comb unless @!input_buffer.elems > 0;
                    @!mem[$!m] = @!input_buffer.shift;
                }
                when '[' {
                    @!mem[$!m] == 0 ?? self!jump !! @!loop_stack.push($!c);
                }
                when ']' {
                    my $b = @!loop_stack.pop - 1;
                    $!c = $b if @!mem[$!m] > 0;
                }
            }
	    $!c++;
        }
    }

    method !jump {
        my $depth = 1;
        while $depth {
            $!c++;
            die "unbalanced code" if $!c >= @.code.elems;
            $depth++ if @.code[$!c] eq '[';
            $depth-- if @.code[$!c] eq ']';
        }
    }
}

# Test: "Hello World" program:

my $code = "++++++++++
           [>+++++++>++++++++++>+++>+<<<<-]
           >++.>+.+++++++..+++.>++.<<+++++++++++++++.>.
           +++.------.--------.>+.>.";

my $bfi = BFInterpreter.new($code);
$bfi.run;
Cookies help us deliver our services. By using our services, you agree to our use of cookies.