Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,29 @@
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
}

View file

@ -0,0 +1,27 @@
// 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");