Execute Brain****: Difference between revisions

m
Fixed language placement
(Added XBS language)
m (Fixed language placement)
Line 6,368:
 
[[/x86_Assembly|Implementation in x86]]
 
=={{header|zkl}}==
<lang zkl>fcn bf(pgm,input=""){ pgm=pgm.text; // handle both String and Data
const CELLS=0d30_000;
if(Void==pgm.span("[","]")){ println("Mismatched brackets"); return(); }
fcn(code,z,jmpTable){ // build jump table (for [ & ])
if(span:=code.span("[","]")){
a,b:=span; b+=a-1; jmpTable[a+z]=b+z; jmpTable[b+z]=a+z;
self.fcn(code[a+1,b-a-1],z+a+1,jmpTable);
self.fcn(code[b+1,*],z+b+1,jmpTable);
}
}(pgm,0,jmpTable:=Dictionary());
 
tape:=CELLS.pump(Data(CELLS,Int),0);
ip:=dp:=0; input=input.walker();
try{
while(1){
switch(pgm[ip]){
case(">"){ dp+=1 }
case("<"){ dp-=1 }
case("+"){ tape[dp]=tape[dp]+1 }
case("-"){ tape[dp]=tape[dp]-1 }
case("."){ tape[dp].toChar().print() }
case(","){ c:=input._next(); tape[dp]=(c and input.value or 0); }
case("["){ if(0==tape[dp]){ ip=jmpTable[ip] }}
case("]"){ if(tape[dp]) { ip=jmpTable[ip] }}
}
ip+=1;
} // while
}catch(IndexError){} // read past end of tape == end of program
}</lang>
<lang zkl> // print Hello World!
bf("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++.."
"+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
 
// print @
bf(">>++++[<++++[<++++>-]>-]<<.[-]++++++++++.");
 
// read 3 characters, inc by 1 and print: "abc"-->"bcd"
bf(",>,>,><<<[+.>]","abc"); println();
 
bf(",>++++++[<-------->-],[<+>-]<.","23"); println(); // add two digits
 
// "Enter your name:", prints name backwards
bf(">+++++++++++++++++++++++++++++++++++++++++"
"++++++++++++++++++++++++++++.++++++++++++++"
"+++++++++++++++++++++++++++.++++++.-------------"
"--.+++++++++++++.>++++++++++++++++++++++++++"
"++++++.<+++++++.----------.++++++.---.>.<----.----------"
"---.++++++++++++.--------.-----------------------------------"
"--------.>.<>>>+[>,----------]++++++++++.<[+++++++++"
"+.<][<]","Sam Iam\n");
 
// word count
bf(File("wc.b").read(),"This\n is a test");
 
// rot13
bf(File("rot13.b").read(),"This is a test 123");</lang>
{{out}}
<pre>
Hello World!
@
bcd
5
Enter your name:
maI maS
1 4 15
Guvf vf n grfg 123^CCntl C noted
</pre>
The rot13 program is from the Wikipedia and has an infinite loop as it expects a different EoF than I use.
 
The word count program is:
<pre>
>>>+>>>>>+>>+>>+[<<],[
-[-[-[-[-[-[-[-[<+>-[>+<-[>-<-[-[-[<++[<++++++>-]<
[>>[-<]<[>]<-]>>[<+>-[<->[-]]]]]]]]]]]]]]]]
<[-<<[-]+>]<<[>>>>>>+<<<<<<-]>[>]>>>>>>>+>[
<+[
>+++++++++<-[>-<-]++>[<+++++++>-[<->-]+[+>>>>>>]]
<[>+<-]>[>>>>>++>[-]]+<
]>[-<<<<<<]>>>>
],
]+<++>>>[[+++++>>>>>>]<+>+[[<++++++++>-]<.<<<<<]>>>>>>>>]
[Counts lines, words, bytes. Assumes no-change-on-EOF or EOF->0.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/]
</pre>
 
=={{header|XBS}}==
Line 6,554 ⟶ 6,467:
<pre>
5
</pre>
 
=={{header|zkl}}==
<lang zkl>fcn bf(pgm,input=""){ pgm=pgm.text; // handle both String and Data
const CELLS=0d30_000;
if(Void==pgm.span("[","]")){ println("Mismatched brackets"); return(); }
fcn(code,z,jmpTable){ // build jump table (for [ & ])
if(span:=code.span("[","]")){
a,b:=span; b+=a-1; jmpTable[a+z]=b+z; jmpTable[b+z]=a+z;
self.fcn(code[a+1,b-a-1],z+a+1,jmpTable);
self.fcn(code[b+1,*],z+b+1,jmpTable);
}
}(pgm,0,jmpTable:=Dictionary());
 
tape:=CELLS.pump(Data(CELLS,Int),0);
ip:=dp:=0; input=input.walker();
try{
while(1){
switch(pgm[ip]){
case(">"){ dp+=1 }
case("<"){ dp-=1 }
case("+"){ tape[dp]=tape[dp]+1 }
case("-"){ tape[dp]=tape[dp]-1 }
case("."){ tape[dp].toChar().print() }
case(","){ c:=input._next(); tape[dp]=(c and input.value or 0); }
case("["){ if(0==tape[dp]){ ip=jmpTable[ip] }}
case("]"){ if(tape[dp]) { ip=jmpTable[ip] }}
}
ip+=1;
} // while
}catch(IndexError){} // read past end of tape == end of program
}</lang>
<lang zkl> // print Hello World!
bf("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++.."
"+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
 
// print @
bf(">>++++[<++++[<++++>-]>-]<<.[-]++++++++++.");
 
// read 3 characters, inc by 1 and print: "abc"-->"bcd"
bf(",>,>,><<<[+.>]","abc"); println();
 
bf(",>++++++[<-------->-],[<+>-]<.","23"); println(); // add two digits
 
// "Enter your name:", prints name backwards
bf(">+++++++++++++++++++++++++++++++++++++++++"
"++++++++++++++++++++++++++++.++++++++++++++"
"+++++++++++++++++++++++++++.++++++.-------------"
"--.+++++++++++++.>++++++++++++++++++++++++++"
"++++++.<+++++++.----------.++++++.---.>.<----.----------"
"---.++++++++++++.--------.-----------------------------------"
"--------.>.<>>>+[>,----------]++++++++++.<[+++++++++"
"+.<][<]","Sam Iam\n");
 
// word count
bf(File("wc.b").read(),"This\n is a test");
 
// rot13
bf(File("rot13.b").read(),"This is a test 123");</lang>
{{out}}
<pre>
Hello World!
@
bcd
5
Enter your name:
maI maS
1 4 15
Guvf vf n grfg 123^CCntl C noted
</pre>
The rot13 program is from the Wikipedia and has an infinite loop as it expects a different EoF than I use.
 
The word count program is:
<pre>
>>>+>>>>>+>>+>>+[<<],[
-[-[-[-[-[-[-[-[<+>-[>+<-[>-<-[-[-[<++[<++++++>-]<
[>>[-<]<[>]<-]>>[<+>-[<->[-]]]]]]]]]]]]]]]]
<[-<<[-]+>]<<[>>>>>>+<<<<<<-]>[>]>>>>>>>+>[
<+[
>+++++++++<-[>-<-]++>[<+++++++>-[<->-]+[+>>>>>>]]
<[>+<-]>[>>>>>++>[-]]+<
]>[-<<<<<<]>>>>
],
]+<++>>>[[+++++>>>>>>]<+>+[[<++++++++>-]<.<<<<<]>>>>>>>>]
[Counts lines, words, bytes. Assumes no-change-on-EOF or EOF->0.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/]
</pre>
 
Anonymous user