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

m
Fixed syntax highlighting and added line numbers.
No edit summary
m (Fixed syntax highlighting and added line numbers.)
 
(3 intermediate revisions by 2 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.comb);
}
 
Line 14 ⟶ 15:
$!c = 0;
$!m = 0;
while $!c < @!.code {
given @!.code[$!c] {
when '>' { $!m++ }
when '<' { $!m-- }
Line 41 ⟶ 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:
 
<lang perl6>my $code = "++++++++++
[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.
Line 55 ⟶ 57:
 
my $bfi = BFInterpreter.new($code);
$bfi.run;</syntaxhighlight>
</lang>
9,476

edits