about summary refs log tree commit diff stats
path: root/examples/rot13.bf
diff options
context:
space:
mode:
authorCase Duckworth2022-06-29 00:59:59 -0500
committerCase Duckworth2022-06-29 00:59:59 -0500
commit0a78720644e45bd27c4a57cbddf2d32aacb555c3 (patch)
tree29783dc9a534dbbfe172d5c84e66f5efcd9c43dc /examples/rot13.bf
parentRecognize bf comments (diff)
downloadtrainfuck-0a78720644e45bd27c4a57cbddf2d32aacb555c3.tar.gz
trainfuck-0a78720644e45bd27c4a57cbddf2d32aacb555c3.zip
Probably version ... whatever the next higher one is
Diffstat (limited to 'examples/rot13.bf')
-rw-r--r--examples/rot13.bf32
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/rot13.bf b/examples/rot13.bf new file mode 100644 index 0000000..bfcde73 --- /dev/null +++ b/examples/rot13.bf
@@ -0,0 +1,32 @@
1[ ROT13 in Brainfuck
2from https://en.wikipedia.org/wiki/Brainfuck#ROT13
3]
4
5-,+[ Read first character and start outer character reading loop
6-[ Skip forward if character is 0
7>>++++[>++++++++<-] Set up divisor (32) for division loop
8(MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
9<+<-[ Set up dividend (x minus 1) and enter division loop
10>+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward
11<[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient
12<<<<<- Decrement dividend
13] End division loop
14]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag
15>--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag
16++++++++++++<[ If flag then set up divisor (13) for second division loop
17(MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
18>-[>+>>] Reduce divisor; Normal case: increase remainder
19>[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient
20<<<<<- Decrease dividend
21] End division loop
22>>[<+>-] Add remainder back to divisor to get a useful 13
23>[ Skip forward if quotient was 0
24-[ Decrement quotient and skip forward if quotient was 1
25-<<[-]>> Zero quotient and divisor if quotient was 2
26]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1
27]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0
28] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
29<[-] Clear remainder from first division if second division was skipped
30<.[-] Output ROT13ed character from copy and clear it
31<-,+ Read next character
32] End character reading loop