Execute Brain****/Icon: Difference between revisions
Content deleted Content added
mNo edit summary |
m Fixed syntax highlighting. |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{implementation|Brainf***}}{{collection|RCBF}} |
|||
This version of BF takes program and input from the parameter list. |
This version of BF takes program and input from the parameter list. |
||
Memory grows as needed from a single cell. |
Memory grows as needed from a single cell. |
||
Line 4: | Line 5: | ||
<pre>BF programtext inputtext</pre> |
<pre>BF programtext inputtext</pre> |
||
<syntaxhighlight lang="icon"># |
|||
<lang Icon> |
|||
# bf interpreter |
|||
# |
|||
procedure main(arglist) |
procedure main(arglist) |
||
prog := get(arglist) |
prog := get(arglist) |
||
# test for nested brackets, previous version was deemed incorrect |
|||
if \prog == "hello" then |
|||
prog := ">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>" || |
|||
"]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++" || |
|||
"++++++.>>.+++.------.--------.>>+.>++++." |
|||
# default program |
|||
/prog := "++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++" || |
/prog := "++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++" || |
||
"++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>" || |
"++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>" || |
||
">+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++." || |
">+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++." || |
||
"<+++++++.--------.<<<<<+.<+++.---." |
"<+++++++.--------.<<<<<+.<+++.---." |
||
input := get(arglist) |
input := get(arglist) |
||
/input := "" |
/input := "" |
||
Line 29: | Line 41: | ||
write("Program is well formed.") |
write("Program is well formed.") |
||
write("Program=",image(prog)) |
write("Program=",image(prog)) |
||
write("Ruler =",image(repl("0123456789", |
write("Ruler =",image(repl("0123456789", |
||
*image(prog)/10 + 1)[1:*image(prog)-1])) |
|||
# 2. execute |
# 2. execute |
||
Line 35: | Line 48: | ||
cell := 1 |
cell := 1 |
||
mem := [0] |
mem := [0] |
||
nest := 0 |
|||
prog ? while i := move(1) do { |
prog ? while i := move(1) do { |
||
case i of { |
case i of { |
||
# increment the pointer (to point to the next cell to the right) |
|||
">" : |
|||
if ( cell +:= 1 ) > *mem then |
if ( cell +:= 1 ) > *mem then |
||
put(mem, 0) |
put(mem, 0) |
||
⚫ | |||
# decrement the pointer (to point to the next cell to the left) |
|||
"<" : |
|||
if ( cell -:= 1 ) < 1 then |
if ( cell -:= 1 ) < 1 then |
||
runerr(205,cell) |
runerr(205,cell) |
||
# increment (increase by one) the byte at the pointer. |
|||
"+" : mem[cell] +:= 1 |
|||
# decrement (decrease by one) the byte at the pointer. |
|||
"-" : mem[cell] -:= 1 |
|||
# output the value of the byte at the pointer. |
|||
"." : writes(char(mem[cell])) |
|||
# accept one byte of input, storing its value in the byte at the pointer. |
|||
"," : input ?:= ( mem[cell] := move(1), tab(0) ) |
|||
# jump forward to the command after the corresponding ] |
|||
# if the byte at the pointer is zero. |
|||
"[" : if mem[cell] = 0 then { |
|||
repeat { |
repeat { |
||
i := move(1) |
i := move(1) |
||
if i == " |
if i == "[" then nest +:= 1 & next |
||
if i == "]" then if nest = 0 then break else nest -:= 1 |
|||
} |
|||
} |
} |
||
# jump back to the command after the corresponding [ |
|||
# if the byte at the pointer is nonzero. |
|||
"]" : if mem[cell] ~= 0 then { |
|||
move(-1) |
|||
repeat { |
repeat { |
||
i := move(-1) |
i := move(-1) |
||
if i == " |
if i == "]" then nest +:= 1 & next |
||
if i == "[" then if nest = 0 then break else nest -:= 1 |
|||
} |
} |
||
move(1) |
|||
} |
} |
||
⚫ | |||
# everything else is ignored/comment |
|||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
Line 77: | Line 106: | ||
Program is well formed. |
Program is well formed. |
||
Program="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---." |
Program="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---." |
||
Ruler ="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" |
|||
Ruler ="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" |
|||
Goodbye, World! |
Goodbye, World! |
||
#BF hello |
|||
Program is well formed. |
|||
Program=">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++." |
|||
Ruler ="012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123" |
|||
Hello World! |
|||
</pre> |
</pre> |
Latest revision as of 11:25, 1 September 2022
Execute Brain****/Icon is an implementation of Brainf***.
Other implementations of Brainf***.
Execute Brain****/Icon is part of RCBF. You may find other members of RCBF at Category:RCBF.
This version of BF takes program and input from the parameter list. Memory grows as needed from a single cell. If no program is specified, the hello world program is run.
BF programtext inputtext
Sample output:
#BF Program is well formed. Program="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---." Ruler ="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" Goodbye, World! #BF hello Program is well formed. Program=">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++." Ruler ="012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123" Hello World!