Execute Brain****/Raku: Difference between revisions

m
Fixed syntax highlighting and added line numbers.
(added perl6)
 
m (Fixed syntax highlighting and added line numbers.)
 
(5 intermediate revisions by 3 users not shown)
Line 1:
{{works with|Rakudo|2018.03}}
<syntaxhighlight lang="raku" perl6line>class BFInterpreter {
has @!.code;
has @!mem;
has @!loop_stack;
Line 8 ⟶ 9:
 
method new (Str $code) {
BFInterpreter.bless(*,code => $code.lines.split("")comb);
}
 
Line 14 ⟶ 15:
$!c = 0;
$!m = 0;
while $!c < @!.code {
given @.code[$!c] {
given @!code[$!c] {
when '>' { $!m++ }
when '<' { $!m-- }
Line 23:
when '.' { @!mem[$!m].chr.print }
when ',' {
@!input_buffer = $*IN.get.split("")comb unless @!input_buffer.elems > 0;
@!mem[$!m] = @!input_buffer.shift;
}
when '[' {
@!mem[$!m] == 0 ?? self!jump() !! @!loop_stack.push($!c);
}
when ']' {
Line 42:
while $depth {
$!c++;
die "unbalanced code" if $!c >= @!.code.elems;
$!depth++ if @!.code[$!c] eq '[';
$!depth-- if @!.code[$!c] eq ']';
}
}
}
}</lang>
 
# Test: "Hello World" program:
 
Test: "Hello World" program:
<lang perl6>
my $code = "++++++++++
[>+++++++>++++++++++>+++>+<<<<-]
Line 57:
 
my $bfi = BFInterpreter.new($code);
$bfi.run;</syntaxhighlight>
</lang>
9,479

edits