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

Content added Content deleted
(Added reading eof from input as -1 (some implementations use 0))
m (Fixed syntax highlighting.)
 
Line 6: 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.
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.


<lang erlang>-module(brainfuck).
<syntaxhighlight lang="erlang">-module(brainfuck).
-export([run/1]).
-export([run/1]).


Line 68: Line 68:
find_end(Ins, N+1, Pos+1);
find_end(Ins, N+1, Pos+1);
find_end(<<_, Ins/binary>>, N, Pos) ->
find_end(<<_, Ins/binary>>, N, Pos) ->
find_end(Ins, N, Pos+1).</lang>
find_end(Ins, N, Pos+1).</syntaxhighlight>


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 '@'):
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 '@'):