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
## bf interpreter#proceduremain(arglist)prog:=get(arglist)# test for nested brackets, previous version was deemed incorrectif\prog=="hello"thenprog:=">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>"||"]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++"||"++++++.>>.+++.------.--------.>>+.>++++."# default program/prog:="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++"||"++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>"||">+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++."||"<+++++++.--------.<<<<<+.<+++.---."input:=get(arglist)/input:=""# 1. toss obvious errorsb:=0prog?whilei:=move(1)do{caseiof{"[":b+:=1"]":b-:=1}ifb<0thenstop("malformed program, unbalanced []")}ifb>0thenstop("malformed program, unbalanced []")write("Program is well formed.")write("Program=",image(prog))write("Ruler =",image(repl("0123456789",*image(prog)/10+1)[1:*image(prog)-1]))# 2. executecell:=1mem:=[0]nest:=0prog?whilei:=move(1)do{caseiof{# increment the pointer (to point to the next cell to the right)">":if(cell+:=1)>*memthenput(mem,0)# decrement the pointer (to point to the next cell to the left)"<":if(cell-:=1)<1thenrunerr(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."[":ifmem[cell]=0then{repeat{i:=move(1)ifi=="["thennest+:=1&nextifi=="]"thenifnest=0thenbreakelsenest-:=1}}# jump back to the command after the corresponding [# if the byte at the pointer is nonzero."]":ifmem[cell]~=0then{move(-1)repeat{i:=move(-1)ifi=="]"thennest+:=1&nextifi=="["thenifnest=0thenbreakelsenest-:=1}move(1)}}# everything else is ignored/comment}end
Sample output:
#BF
Program is well formed.
Program="++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.<+++++++.--------.<<<<<+.<+++.---."
Ruler ="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
Goodbye, World!
#BF hello
Program is well formed.
Program=">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++."
Ruler ="012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"
Hello World!