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

m
Fixed syntax highlighting.
m (Fixed syntax highlighting.)
 
(One intermediate revision by one other user not shown)
Line 6:
The opcodes also use a binary representation, which allows to jump through the loop structures. Note, however, that finding the end of the loop still requires linear time, which could be improved.
 
<langsyntaxhighlight lang="erlang">-module(brainfuck).
-export([run/1]).
 
Line 37:
run(<< $,, Ins/binary>>, DataPtr, Tape) ->
<<Prev:(DataPtr)/binary, _/integer, Next/binary>> = Tape,
[X] = case io:get_chars("",1), of
[C] -> C;
eof -> -1
end,
run(Ins, DataPtr, <<Prev/binary, X/integer, Next/binary>>);
run(<< $[, Ins/binary>>, DataPtr, Tape) ->
Line 65 ⟶ 68:
find_end(Ins, N+1, Pos+1);
find_end(<<_, Ins/binary>>, N, Pos) ->
find_end(Ins, N, Pos+1).</langsyntaxhighlight>
 
Example output, the first one being the usual 'Hello World!' program and the second one testing nested loops to increment count to 64 and print the result (should be '@'):
9,476

edits