Execute Brain****/Raku: Difference between revisions
Content added Content deleted
(added perl6) |
m (Fixed syntax highlighting and added line numbers.) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{works with|Rakudo|2018.03}} |
|||
<lang |
<syntaxhighlight lang="raku" line>class BFInterpreter { |
||
has @ |
has @.code; |
||
has @!mem; |
has @!mem; |
||
has @!loop_stack; |
has @!loop_stack; |
||
Line 8: | Line 9: | ||
method new (Str $code) { |
method new (Str $code) { |
||
BFInterpreter.bless( |
BFInterpreter.bless(code => $code.lines.comb); |
||
} |
} |
||
Line 14: | Line 15: | ||
$!c = 0; |
$!c = 0; |
||
$!m = 0; |
$!m = 0; |
||
while $!c < @ |
while $!c < @.code { |
||
{ |
given @.code[$!c] { |
||
given @!code[$!c] { |
|||
when '>' { $!m++ } |
when '>' { $!m++ } |
||
when '<' { $!m-- } |
when '<' { $!m-- } |
||
Line 23: | Line 23: | ||
when '.' { @!mem[$!m].chr.print } |
when '.' { @!mem[$!m].chr.print } |
||
when ',' { |
when ',' { |
||
@!input_buffer = $*IN.get. |
@!input_buffer = $*IN.get.comb unless @!input_buffer.elems > 0; |
||
@!mem[$!m] = @!input_buffer.shift; |
@!mem[$!m] = @!input_buffer.shift; |
||
} |
} |
||
when '[' { |
when '[' { |
||
@!mem[$!m] == 0 ?? jump |
@!mem[$!m] == 0 ?? self!jump !! @!loop_stack.push($!c); |
||
} |
} |
||
when ']' { |
when ']' { |
||
Line 42: | Line 42: | ||
while $depth { |
while $depth { |
||
$!c++; |
$!c++; |
||
die "unbalanced code" if $!c >= @ |
die "unbalanced code" if $!c >= @.code.elems; |
||
$ |
$depth++ if @.code[$!c] eq '['; |
||
$ |
$depth-- if @.code[$!c] eq ']'; |
||
} |
} |
||
} |
} |
||
} |
|||
}</lang> |
|||
⚫ | |||
⚫ | |||
<lang perl6> |
|||
my $code = "++++++++++ |
my $code = "++++++++++ |
||
[>+++++++>++++++++++>+++>+<<<<-] |
[>+++++++>++++++++++>+++>+<<<<-] |
||
Line 57: | Line 57: | ||
my $bfi = BFInterpreter.new($code); |
my $bfi = BFInterpreter.new($code); |
||
$bfi.run; |
$bfi.run;</syntaxhighlight> |
||
</lang> |
Latest revision as of 11:55, 1 September 2022
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;