Execute Brain****/Icon: Difference between revisions
Content added Content deleted
mNo edit summary |
(Another buggy interpreter) |
||
Line 1: | Line 1: | ||
{{incorrect|C|Nested brackets are not interpreted correctly.<pre> |
|||
>++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.> |
|||
>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++. |
|||
</pre>}} |
|||
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. |
Revision as of 08:59, 24 January 2016
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
<lang Icon> procedure main(arglist)
prog := get(arglist) /prog := "++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++" ||
"++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>" || ">+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++." || "<+++++++.--------.<<<<<+.<+++.---."
input := get(arglist) /input := ""
- 1. toss obvious errors
b := 0 prog ? while i := move(1) do {
case i of { "[" : b +:= 1 "]" : b -:= 1 } if b < 0 then stop("malformed program, unbalanced []") }
if b > 0 then stop("malformed program, unbalanced []")
write("Program is well formed.") write("Program=",image(prog)) write("Ruler =",image(repl("0123456789",*prog/10)))
- 2. execute
cell := 1 mem := [0]
prog ? while i := move(1) do {
case i of {
">" : # increment the pointer (to point to the next cell to the right) if ( cell +:= 1 ) > *mem then put(mem, 0) "<" : # decrement the pointer (to point to the next cell to the left) if ( cell -:= 1 ) < 1 then runerr(205,cell)
"+" : mem[cell] +:= 1 # increment (increase by one) the byte at the pointer.
"-" : mem[cell] -:= 1 # decrement (decrease by one) the byte at the pointer.
"." : writes(char(mem[cell])) # output the value of the byte at the pointer.
"," : input ?:= ( mem[cell] := move(1), tab(0) ) # accept one byte of input, storing its value in the byte at the pointer.
"[" : if mem[cell] = 0 then { # jump forward to the command after the corresponding ] if the byte at the pointer is zero. repeat { i := move(1) if i == "]" then break } }
"]" : if mem[cell] ~= 0 then { # jump back to the command after the corresponding [ if the byte at the pointer is nonzero. repeat { i := move(-1) if i == "[" then break } } } # everything else is ignored/comment }
end</lang>
Sample output:
#BF Program is well formed. Program="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---." Ruler ="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" Goodbye, World!