Anonymous user
Execute Brain****/Icon: Difference between revisions
Updated entry to account for nested brackets, reformatted to shorten source line lengths, touched on the ruler, added hello option to test nested brackets
(c&p error) |
(Updated entry to account for nested brackets, reformatted to shorten source line lengths, touched on the ruler, added hello option to test nested brackets) |
||
Line 1:
>++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>▼
>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++.▼
This version of BF takes program and input from the parameter list.
Memory grows as needed from a single cell.
Line 9 ⟶ 4:
<pre>BF programtext inputtext</pre>
<lang Icon>#
# bf interpreter
#
procedure main(arglist)
prog := get(arglist)
# test for nested brackets, previous version was deemed incorrect
if \prog == "hello" then
"]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++" ||
# default program
/prog := "++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++" ||
"++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>" ||
">+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++." ||
"<+++++++.--------.<<<<<+.<+++.---."
input := get(arglist)
/input := ""
Line 34 ⟶ 40:
write("Program is well formed.")
write("Program=",image(prog))
write("Ruler =",image(repl("0123456789",
*image(prog)/10 + 1)[1:*image(prog)-1]))
# 2. execute
Line 40 ⟶ 47:
cell := 1
mem := [0]
nest := 0
prog ? while i := move(1) do {
case i of {
">" :
if ( cell +:= 1 ) > *mem then
put(mem, 0)
"<" :
if ( cell -:= 1 ) < 1 then
runerr(205,cell)
"+" : mem[cell] +:= 1
"-" : mem[cell] -:= 1
"." : writes(char(mem[cell]))
"," : input ?:= ( mem[cell] := move(1), tab(0) )
# if the byte at the pointer is zero.
"[" : if mem[cell] = 0 then {
repeat {
i := move(1)
if i == "
}
}
# if the byte at the pointer is nonzero.
"]" : if mem[cell] ~= 0 then {
move(-1)
repeat {
i := move(-1)
if i == "
if i == "[" then if nest = 0 then break else nest -:= 1
}
move(1)
}
▲ }
}
end</lang>
Line 82 ⟶ 105:
Program is well formed.
Program="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---."
Ruler ="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
Goodbye, World!
#BF hello
Program is well formed.
Program=">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++."
Ruler ="012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
Hello World!
</pre>
|