Skip to content

Instantly share code, notes, and snippets.

@Sinthorion
Created April 23, 2020 20:56
Show Gist options
  • Save Sinthorion/f1410f87e03ff51ba2d5144a9edfb14f to your computer and use it in GitHub Desktop.
Save Sinthorion/f1410f87e03ff51ba2d5144a9edfb14f to your computer and use it in GitHub Desktop.
[
Encodes the input string, until newline, to a Brainfuck program that prints this string
The BF code will use the current cell only, which is the fastest possible way to print
in Brainfuck, despite producing longer code than other methods.
registers:
- '+'/'-'/'.'
- prev
- input
- prevdup1
- newdup1
- cmp
- prevdup2
- newdup2
]
++++ ++++ ++++ ++++ ++++ ++++ ++++ ++++ ++++ ++++ +++
>>,
[<+<.>>-]
<<+++.--->
#
---- ---- --[++++ ++++ ++
[>>+>>>+<<<<<-]
>,[<+>>>+>>>+<<<<<-]
#
>>> go to cmp
>[>[-<->>>-<]>+[-<<<+>[-]>>>]<<<]>[-]<< cmp (consumes prev2 and new2)
[- if prev gt new
>-< set else
<[<->-] prev sub new
<<<<++>>>> switch to minus
<[<<<.>>>-]
<<<+.--->>> print dot and reset to plus
>>
]>+[- else (prev le new)
<<<[>-<-] new sub prev
>[<<<<.>>>>-]
<<<<+++.--->>>> print dot
>>
]
<<[-] clear new
<[-] clear prev
<<
---- ---- --]
+++++++++++++++++++++++++++++++++++++++++++>>,[<+<.>>-]<<+++.--->----------[++++++++++[>>+>>>+<<<<<-]>,[<+>>>+>>>+<<<<<-]>>>>[>[-<->>>-<]>+[-<<<+>[-]>>>]<<<]>[-]<<[->-<<[<->-]<<<<++>>>><[<<<.>>>-]<<<+.--->>>>>]>+[-<<<[>-<-]>[<<<<.>>>>-]<<<<+++.--->>>>>>]<<[-]<[-]<<----------]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment