If cell1 == 0:
Get input into cell1
If cell1 == 1 or cell1 == 0:
Return cell11
Else:
Initialise self-interpreter-quine function
Pass cell1-1 into cell1 of the function
Run function
Multiply cell1 by the return value
Return cell1
If cell1 == 0:
Get input into cell1
If cell1 == 1:
Return cell1
Else:
Initialise self-interpreter-quine function
Pass cell1-1 into cell1 of the function
Run function
Multiply cell1 by the return value
Return cell1
If cell1 == 0:
Get input into cell1
If cell1 == 1 or cell1 == 0:
Return 1
Else:
Initialise self-interpreter-quine function
Pass cell1-1 into cell1 of the function
Run function
Multiply cell1 by the return value
Return cell1
brainfuck, 6013 48774877 4376 bytes
Edit: -1136 bytes. Switched to a better way of generating the data for the quine
Edit2: -501 bytes. Revisited my self-interpreter and cut it down a couple of hundred bytes
->++>++++>+>++>+++>>++++>>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>++++>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>+++>>+++>+>+++++>+++++>++++>>++>+>++>++>++>++>++>++>+++>>++>++>>++++>>+++>++>++>++>++>++>++++>+++++>>++>++>++>+>+++>>++++>>++>++>++>>++>++>++>>++>++>++>>++>++>++>++>++>++>>++>++>++>++>++>++>>++>++>++>>++>++>++>++>>++>++>++>++>++++>+>+++>>+++++>+++>>++>>+++++>>>++>>++>++>++>>+++++>+++++>>>++>++>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>++++>++++>++++>>+++>>>++++>>+++>>>++++>>+++>+>++++>+++++>>>++>+>+>+++>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++++>+++>>++++>>+++>>>++++>>+++>>>++++>>+++>>>++++>+++++>+>+>++>++++>+>+++>+>++>>>++++>>+++>>+++>+>+>++++>++++>+++++>>++>+>+++>+>+++>>>++++>>+++>>++++>++++>+++++>+>++>>+++>>+++>+>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++>>>>++++>>+++>>>++++>>+++>+>++++>++++>+++++>+++>+>+++>>>++++++>++++>>++>++>++>++>++>++>++>++++>+>+++++>+++++>+++++>+++++>+++++>+++++>>+++++>+++>+++++>+>+++++>++++>>+++>>++++>+>++>++>>>++>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>++>++>++>++>++>++>>>++>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>>>++>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>++>++>++>++>++>++>++>>>++>>>+++>+>+>+>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>++++>>+++>>++++>+>++>++>++>>>++>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>++>++>++>++>>>++>>>+++>+>++>++>++>++>++>++++>+++++>+>+>+++++>+++++>+++++>+++++>+++++>+++++>>>+++>+>+>++>++++>>+++>>++++>+>++>++>++>++>++>>>++>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>++>++>++>++>++>++>>>++>>>+++>+>+>++++>+++++>+++>+>++++>++>+++>++++++>+++>++>++>++>++>++>++>++>++>++>>++>>>>>++>+>+>+>+>+>++++>+>+++>>++++>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>+++++>+>+>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>++>+>+>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>++++>>>+++>>++++>+++++>>++++>+++++>+>+>+>+>++++>+>+>+++>>++>>++++>>>+++>>>+++>+>+++>+>++>++++>+>++++>+++++>>>++>+>+>+++>+>+++>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>++++>>++++>+++++>+>+>++>>>+++>>+++>+>++>+>+++++>+>+>++++>+>+>+++>>++++>+++++>>++++>>>+++>>>++>+>+>+>+>++++>+>+>+++>>+++>>++++>+>+>+++>>++++>++++>>>+++>++>++++>+>++++>+++++>>>++>+>+>+++>+>+++>>++>>+++++>+>+>+++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>++++>+>+++>+>+>+>++++>+>+++>+>+>++++>++++>+>+++>+>+>++>++>++>++>++>>>>++++>+++++>+++++>+++++>+++++>+++++>++++>+>+>+++>>++++>++++>>+++>>++>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>++++>+++++>+>+>++++>+>+++>+>+>+++>>+++>+>+>++++>>>>++>++>++>++>++>++>++++>+++++>+>+>++>>>+++>+++>>+++>+>+>++++>+>+++>>++++>+>++>+>+>+++>+++>>+++>>+++++>++++>+>+>+++>>++++>+>+++++>>++++>>+++>>>>>++++>+>+++>+>+>+>++++>++++>+>+++>>++>++>++>++>++>++>++>+>+>+>++++>+++++>+++++>+++++>+++++>+++++>+++++>++++>>>+++>+>++++>>>>>++++>>+++>>++>+>+>++++>+>+++>+>+++>+>++>++++>>>+++>+>++++>>>>>++++>>+++>>++++>+++++>>+++>+>+>++++>>>>+++>+>++++>+>+++>+>+++>>>++++>+>+>+>++>++>++>++>++>++++>+++++>>>++>+>+>+++>+++>+>+++>+>+>++++>+>+++>+++++>+++++>++++>>+++>>+++>+>+>++++>+>+++>>++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>++++>+++++>+++>+>+>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>++++>++++>>+++>>++++>>+++>>>>++++>>+++>>>>>++>+>+>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>+++++>+++>>++++>>+++>>++++>>+++>>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>+++++++>+>+>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>+++++>+++>+>++>++>++>++>++>++>++>++>++>>>++++>++>+>+>+++++>>>+++>>+++>>>>++++>++++[<+]>[>[>]>[>]>++++++++++[>++>++++>+>++>+++>>++++>>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>++++>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>+++>>+++>+>+++++>+++++>++++>>++>+>++>++>++>++>++>++>+++>>++>++>>++++>>+++>++>++>++>++>++>++++>+++++>>++>++>++>+>+++>>++++>>++>++>++>>++>++>++>>++>++>++>>++>++>++>++>++>++>>++>++>++>++>++>++>>++>++>++>>++>++>++>++>>++>++>++>++>++++>+>+++>>+++++>+++>>++>>+++++>>>++>>++>++>++>>+++++>+++++>>>++>++>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>++++>++++>++++>>+++>>>++++>>+++>>>++++>>+++>+>++++>+++++>>>++>+>+>+++>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++++>+++>>++++>>+++>>>++++>>+++>>>++++>>+++>>>++++>+++++>+>+>++>++++>+>+++>+>++>>>++++>>+++>>+++>+>+>++++>++++>+++++>>++>+>+++>+>+++>>>++++>>+++>>++++>++++>+++++>+>++>>+++>>+++>+>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++>>>>++++>>+++>>>++++>>+++>+>++++>++++>+++++>+++>+>+++>>>>++++++>++++>>++>++>++>++>++>++>++>++++>+>+++++>+++++>+++++>+++++>+++++>+++++>>+++++>+++>++>+>+++++>++++>>+++>>++++>+>++>+>++>>>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>+>++>>>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>+>++>>>>>+++>+>+>+>+++++>++++>>+++>>++++>+>+++++>+>++>++>++>>>>>+++>+>+>+>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>++++>>+++>>++++>+>++>++>+>++>>>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>+++++>+>++>++>++>++>++>++>++>>>>>+++>+>+>++>++>++>++>++++>+++++>+>+++++>+++++>+++++>+++++>+++++>+++++>>+++>++>+>++>++++>>+++>>++++>+>++>++>+>++>>>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>+++++>+>++>>>>>+++>+>+>+++++>+>++++>++>+++>+>++++>>+++>>++++++>+++>>>>++>+>+>+>+>+++++>++++>+>+++>>++++>+++++>++++>+>+>+++>>++++>+>++>++++>>+++>>>++++>+>+++>+>+>++++>>>++>++++>+>+>++++>+>+++>+>+>+++++>++++>>>+++>+>++++>>>>>++++>>+++>>++>+>+>++++>+>+++>+>+++>+>+++++>++++>>>+++>+>++++>>>>>++++>>+++>>+++++>+>+>++++>+>+++>+>+++>+>++>++>++++>+++++>>>++>+>+>+++>>>++++>>+++>>+++>+>+++>+>++++>+>+++>>+++++>+>+++>>+++++>++++>+>+>+++>>++++>+>++>++>++++>>+++>>>++++>+>+>+++>>++++>+>+>++>++++>+>+>++++>+>+++>>++++>+++++>+>+>++>>>+++>>+++++>+++++>++++>+>+>+++>>++++>++++>>+++>>++>+>+>++++>+>+++>+>+++>>++>++++>+>+>+++>>++++>++++>>+++>>+++++>+>+>++++>+>+++>+>+++>>++>++++>>+++>>+++>+++>+>+>++++>+>+++>>+++++>+++++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>+++++++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>+++++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>++++++>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>++++>++++>>+++>>++++>>+++>>>>++++>>+++>>>++>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>+++++>+++>>++++>>+++>>++++>>+++>>>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>++++>>>+++>>++++>+>+>+>++++>+>+>+++>+>++>>>>++++>>>+++>>+++++>+++>>++++>+++++>+++>+>+>++>++++>+>++++>+++++>>>++>+>+>+++>+>+++>+>++++>+++++>>>++>+>+>+++>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>++>++>++++>+++++>+++++>>++++>+++++>+>+>++>>>+++>>+++>++>+>+>+++++>++++>++>++>+>+>+++>+>++++>+++++>>>>++++>>>+++>>++>+>+>+>++++>+>+>+++>+>+++>+>++++>+>+++>+>+++>+>++>++>++>++>++>++>++>++>>>++++>++>+>+>+++++>>>+++>>+++>>>++++>++++[<+]>[>[>]>[>]>++++++++++[-<++++++>]<++[<]<[<]<+>>-[[>]>[>]>+++++++[-<++++++>]<+[<]<[<]<+>>-]>]<--[>+<++++++]>++>[>]+++++[->+++<]>[>+++>+++>+++>++++++>++++++>+++>++++>++++[<]>-]>+>->>+>+++>-->>++[<]<<[<]<<[<]>[[[>]>>[>]>>[>]<[->>+<<]<[<]<<[<]<<[<]>-]>[>]>>[>]>>[>]>>[-<<+[<]<+>>[>]>]<<[[->+<]<]>>[>]>[[-<+>]>]<<[<]<<[<]<<[<]>]>>>[>]>>[>]<[[-]<]>>]<]>>>,[>+++++++[<------>-]-<-[>]>[<++>>+>>]<<<-[>]>[<+++++++>>+>>]<<<-[>]>[<+>>+>>]<<<-[>]>[<++++++++>>+>>]<<<--]+<-[>]>[<+<+>>>>]<<<-[>]>[<+<+>>>>]<<<-[>]>[<+<+>>>>]<<<-[>]>[<-<+++>>>>]<<<-------[>]>[<+++>>+>>]<<<--[>]>[<++++>>+>>]<+++++[-<<----[>]>[<++<+>>>>]<<<-->>]<<+[>]>[<+++++>>+>>]<<<[>]>[<-<+++++++>>>>]<<++++[-[>]>[<++++++>>+>>]<<[<-]<[+],]+++++++++>+>>>>+<<<<<[<]>[-[<<]>[[>]>>>>-<<<<<[<]<]>-[<<]>[[>]>>>>+<<<<<[<]<]>-[<<]>[[>]>>>[>>]>[->[>]+<+[>]>[<++<+>>>>]<<<-<<<<[<<]>+>[>>]>>]<]<+[<[->>+<<]<]<<[<]<]>[>]>[<-[<<]>[[>]>>>[>[<+>>>>]<<-<<+>>]>]<+<<[+]<[>]>,]>>>+<<<<-<<[<<]>[[<]>[->[>>]>>+<<<<[<<]>]>[<<]>[[>>]+[<[[<<]>[<+[>]>>[<]<<[>>+[<<[<]<<->>+<<]<]>+>[>>]<[>>>>[>]>+<<[<]<]<-<<]<<<[<]<]>[>>]<[>>>>[>]>-[<<]>[[>]>>>>[<]<<<[<]<<[[<]<<+++++>>>[<<[<]<]<++[->>+<<]>>[>]>]<]<[<]>-<]>-[<<]>[<++[>]>>[<<]>[<<+[<<[<]>[-<<+>>]>-[<<]>[[>]>+<<[<]<]>-[<<]>[[>]>[[<<]>[[>]>+<<[<]<]>+[<<]>[[>]>-<<[<]<<]>]<<[>>>++++++[<<[<]<]>+[>]>]]<<[<]>-<<+>>]]>]<<[<]>[<+<<]]>]>-[<<]>[<<]>->[>]>>>>[<]<<<[[<]>+++++++<<<[[<<]>[[>]>>.<<<[<]<]>-[<<]>[[>]>>-<<<[<]<]>-[<<]>[[>]>>,<<<[<]<<<[<]<[<]>[[>]>[>]>>>[>]>>+<<<[<]<<<[<]<[<]>-]>[>]>[>]>>[<]<]>-[<<]>[[>]>>+<<<[<]<]>-[>>]<[>>>>[>]>+<<[<]<]<+[>>]<[>>>>[>]>[[<<]>[[>]>>>[>>]>[<<<[<<]<+>>>[>>]>->]<<[>>>]<[<]<]>>[<<<+++++[]>[->>+<<]]<]<<[<]]<<+[<[->>+<<]<]<[-[>]>]<<[<]>+<]>>>+<<]<[<]<]>-[<<]>[[>]>>>>[[<<]>[[>]>++[-]<<<<<[<]<<<[<]<[<]>[[>]>[>]>>>[>]>>>>+<<<<<[<]<<<[<]<[<]>-]>[>]>[>]>]>>[-[<<]>[[>]>>>>.<<<<<[<]<]><<+>>]>]+<<-[<<]>[[++<<]<[-]<+++++++++>>[+<<>>>[>>]>+<<<[<<]<]<[<]<]<++++++++>>[+<<->>]>]>>>[]>>]>]>>[]
Try it online!. The input here is a simple cat program (,[.,]
) which will print the program itself.
->++++>+>++>+++>>++++>>+++>++>++++> ....... >+++++>>>+++>>+++>>>>++++>+++>+++++>>>+++>>+++>>>++++>+++
This part makes up the data section of the quine, and is by far the majority of the code at 36693270 bytes. The beginning -
is the endinga marker for the beginning of the data. Each >+++
represents a character of the code after this section.
Initialises the 8 characters of brainfuck
>[>]>+++++[>[>]+++++[->+++<]>[>+++>+++>+++>++++++>++++++>+++>++++>++++[<]>-]
>+>->>+>+++>-->>++[<]<<[<]<<<[<]>>>++[<]<<[<]<<[<]>
Tape looks like:
data 0 0 code 0 0 characters
Runs through the data destructively and adds the represented symbol to the code section
[
[
For each plus in this cell
Shift the gap in the characters over one
[>]>>[>]>>[>]<[->>+<<]
<[<]<<[<]<<[<]>-
]
Navigate to character
>[>]>>[>]>>[>]>>
Copy the character to the end of the code section
[-<<+[<]<+>>[>]>]
Shift the symbol section over one
<<[[->+<]<]
>>[>]>[[-<+>]>]
Navigate to next byte of data
<<[<]<<[<]<<[<]>
]
Remove characters
>>[>]>>[>]<[[-]<]
>>>>>,
[
>(7+++++++)[<(6------)>-]-<]+<-
[>]>
[plus <++>>+>>]<<<<+<+>>>>]<<<
-[>]>
[comma <+++++++>>+>>]<<<<+<+>>>>]<<<
-[>]>
[minus <+>>+>>]<<<<+<+>>>>]<<<
-[>]>
[dot <++++++++>>+>>]<<<<-<+++>>>>]<<<
(14--------------)[>]>
[left <+++>>+>>]<<<<++<+>>>>]<<<
--[>]>
[right <++++>>+>>]<<-<+++++++>>>>]<<
(29+++++[29++++[-<<<------>>]<<+>]+<+)
[>]>
[start loop <+++++>>+>>]<<<<++<+>>>>]<<<
--[>]>
[end loop <++++++>>+>>]<<
<-<+>>>>]<<
[-]<[+]<[+]<[>]>,
]
(9+++++++++)>+
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-] | +[ | <. | >- | [, | ]+ | ,> | .< |
Represents the end of the program with 9 1255
.
>>>>+<<<<<Initialise simulated tape
>>>+<<<<-
[<]>
[
-[<<]>
[minus
[>]>>>>-<<<<<[<]<
]
>-[<<]>
[plus
[>]>>>>+<<<<<[<]<
]
>-[<<]>
[left
[>]>>>[>>]>
[
[end ->loop
co 0 0 [-<<<<[<<]>+>[>>]>>]<
0 e: _1 0 0 0 1 ]?
<+
Check if current cell is [<[->>+<<]<]one
<<[<]<<+[>]>>[<]<<
]
>-[<<]>
co [right
0 0 1 e _1: 0 0 !0 [>]>>>1
[>[-<<+>>]>]or
<+<-<<[<<]>
co [->[>>]>>+<<<<[<<]>]
0 0 1 e _1 0: 0 0 >[<<]>1
[
[>>]+
[<[->>+<<]<]
>+>-<<
]
<<<[<]<
]
If current cell is >-[<<]>
one navigate to corresponding [startstart loop
[>]>>>>[<]<<<[<]<<
[
[<]<<
Create (5+++++)counter
>>>>>+
[
co 0 0 de _1 0 c: !0 1
checks if next instruction is an end loop
<<[<]<<-
[>>]<
c !0 0: 0 de _1 0 c !0 1
or
c: 0 0 0 de _1 0 c !0 1
[>>>>[>]>+<<[<]<] Add one to counter if it is
checks if start loop
(5----<-)[>>]<
[<<]>c !0 0: 0 de _1 0 c !0 1
[[>]>+<<[<]<]or
checksc: if0 end0 loop 0 de _1 0 c !0 1
>[>>>>[>]>-[<<]><<[<]<] Subtract one from counter if it is
[c ? 0: 0 de _1 0 c !0 1
Adds two to counteract checks [>]>
and move to the next instruction
[ <++[-<<[<]<<]>>+<<]
>>[>]>
>
c 0 0 ode ]_1 0 c: !0 1
<<
End on the counter
[>>>(6++++++)[-<<+>>]]
If the counter is 0 then we have reached the corresponding >bracket
]
<<[<]>[<+<<]c 0 0 2 de _1 0 0: !0 1 0
<
]
>c 0 0 1?2 de _1 0: 0 0 1 0
Subtract one from current instruction
This executes the start loop code next but that does nothing
<[<]>-<
]
>-[<<]>
[end[start loop
<->[>]>>>>[<]<<<c 0 0 0 de: _1 0 0 ? 1
<++[>]>>[<<]>
c 0 0 2 de _1 0 0 0 1:
or
c 0 0 2 de _1 0 0: !0 1
[ If current cell is 0 navigate to corresponding end loop
[<]>Initialise counter
<<+
c 0 0 ode _1 0 c: 0 1
[ While counter is not 0
Transfer current instruction over (7+++++++first instruction is guaranteed to be start loop)<<<
[ <<[<]>[-<<+>>]>
co 0 0 de _1 0 c: 0 1
Check if start loop
--[<<]>
co 0 0: !0 e _1 0 c 0 1
or
co 0 0 0 e _1 0 c 0 1
[[>]>+<<[<]<] Add one to counter if so
checks if end loop
(6------)>+[<<]>
[>>]<
co 0 0: !0 e _1 0 c 0 [1
add one to markeror
>>>>[>]>+<<[<]<
co 0 0 0 e: _1 0 c 0 ]1
<
[[>]>-<<[<]<] Subtract one from checkscounter if start loopso
+[>>]<
Add one to counteract checks and navigate to [counter
>>>>[>]>>+[>]>
co 0 0 de if_1 marker0 notc: 0
1
End on [->]<<counter
ifIf markercounter is 0
[>>>]<[<]<
]
>>
then we have reached the corresponding end [<<<(5+++++)[->>+<<]]<loop
]
<<[<]--[>]>co 0 1 e _1 0 0: 0 1
]
<<[<]>+<co 0 0 2?1 e _1 0 0: ? 1
Subtract two from the current instruction to bring it back up to the right value
<<[<]>--<
]
3 of these are pretty self explanatory
Navigate to the current cell and execute the instruction on it
>-[<<]>
[output
[>]>>.<<<[<]<
]
>-[<<]>
[minus
[>]>>-<<<[<]<
]
>-[<<]>
[input
[>]>>>>[-]<<<<<[<]<<<[<]<[<]>Reset current cell
[[>]>>, (no more input so this is set to 0)
co 0 0 0 [>]>[>]>>>[>]>>>>+<<<<<[<]<<<[<]<[<]>-e: _1 0 0 0: 1 b 1 a 0 d 1 e 1 f
Navigate to start of code section
<<<[<]<<<[<]<[<]>
d: ata 0 co 0 0 0 e _1 0 0 0 1 b
or
0: co 0 0 0 e _1
Transfer next instruction to current cell
[[>]>[>]>>>[>]>>+<<<[<]<<<[<]<[<]>-]
>[>]>[>]>0: ata 0 co 0 0 0 e _1 0 0 d 1 b
or
0: co 0 0 0 e _1
Navigate back to the normal spot
>[>]>[>]>>[<]<
]
>-[<<]>
[output[plus
[>]>>>>.<<<<<[<]<[>]>>+<<<[<]<
]
>-[<<]>
[end[right
Simulated tape looks like:
a b c: d e f
co 0 0 0 e: _1 0 0 c 1 b 1 a 0 d 1 e 1 f
Navigate to value of cell to the right
[>]>>>[>>]>
co 0 0 0 e _1 0 0 c 1 b 1 a 0 d: 1 e 1 f
Transfer it to temporary cell
[<<<[<<]<+>>>[>>]>-]
<(9+++++++++)>> co 0 0 0 e _1 d 0 c 1 b 1 a 0 0: 1 e 1 f
[+<< Pop extra marker if it exists from the right cells and add one to the left
>[->>]>]<<+
co 0 0 0 e _1 d 0 c 1 b 1 a 1: 0 0 e 1 f
Transfer all left cells over 2 cells
[<[->>+<<]<]<[->>+<<]
co 0 0 0 e _1 0: 0 d 1 c 1 b 1: a 0 e 1 f
Navigate back to normal spot
<[<]<
]
>-[<<]>
[left
Simulated tape looks like:
a b c: d e f
co 0 0 0: e _1 0 0 c 1 b 1 a 0 d 1 e 1 f
Add temporary marker
[>]>++
co 0 0 0 e _1 0 2: c 1 b 1 a 0 d 1 e 1 f
Remove temporary marker and transfer all left cells over two
[-->[-<<+>>]>]
co 0 0 0 e _1 c 0 b _1 a _1 0 0: d 1 e 1 f
Add marker to right cells remove marker from left cells and reset left cell's markers
+<<-[++<<]<
co 0 0 0 e _1 c: 0 b 1 a 0 0 1 d 1 e 1 f
Transfer current cell to to right cells
[->>>[>>]>+<<<[<<]<]
co 0 0 0 e _1 0: 0 b 1 a 0 c 1 d 1 e 1 f
Navigate back to normal spot
<[<]<
]
Add 8 to reverse checks
<(8++++++++)>>
Execute next instruction
[+<<->>]>
]
>>>[]>>[]
Always returns 0 (halts and returns 0)Always returns 0 (halts and returns 0)
Always returns 1 (runs forever)Always returns 1 (runs forever)
Returns all input added together, mod 256 (runs forever)Returns all input added together, mod 256 (returns 211, so it runs forever)
Returns 0 if the last two characters of a code are an infinite loop ([]
) (your program returns 0 when given my program, therefore my program halts)Returns 0 if the last two characters of a code are an infinite loop ([]
) (your program returns 0 when given my program, therefore my program halts)
###Fun Fact for those who are still reading If the input for this program is the source code of this program, then it will start recursing, repeatedly creating self-interpreters that run this program and then give it the same program again. This gives me some interesting ideas on creating actual recursive programs in brainfuck. Instead of checking the return value and starting an infinite loop as in this question, the return value can be saved and acted upon. A simple example would be a factorial program that goes
If cell1 == 0:
Get input into cell1
If cell1 == 1:
Return cell1
Else:
Initialise self-interpreter-quine function
Pass cell1-1 into cell1 of the function
Run function
Multiply cell1 by the return value
Return cell1
Of course, this is a completely insane way of coding brainfuck, given that running recursing self-interpreters will increase the runtime exponentially.
brainfuck, 6013 4877 bytes
Edit: Switched to a better way of generating the data for the quine
->++>++++>+>++>+++>>++++>>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>++++>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>+++>>+++>+>+++++>+++++>++++>>++>+>++>++>++>++>++>++>+++>>++>++>>++++>>+++>++>++>++>++>++>++++>+++++>>++>++>++>+>+++>>++++>>++>++>++>>++>++>++>>++>++>++>>++>++>++>++>++>++>>++>++>++>++>++>++>>++>++>++>>++>++>++>++>>++>++>++>++>++++>+>+++>>+++++>+++>>++>>+++++>>>++>>++>++>++>>+++++>+++++>>>++>++>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>++++>++++>++++>>+++>>>++++>>+++>>>++++>>+++>+>++++>+++++>>>++>+>+>+++>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++++>+++>>++++>>+++>>>++++>>+++>>>++++>>+++>>>++++>+++++>+>+>++>++++>+>+++>+>++>>>++++>>+++>>+++>+>+>++++>++++>+++++>>++>+>+++>+>+++>>>++++>>+++>>++++>++++>+++++>+>++>>+++>>+++>+>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++>>>>++++>>+++>>>++++>>+++>+>++++>++++>+++++>+++>+>+++>>>++++++>++++>>++>++>++>++>++>++>++>++++>+>+++++>+++++>+++++>+++++>+++++>+++++>>+++++>+++>+++++>+>+++++>++++>>+++>>++++>+>++>++>>>++>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>++>++>++>++>++>++>>>++>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>>>++>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>++>++>++>++>++>++>++>>>++>>>+++>+>+>+>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>++++>>+++>>++++>+>++>++>++>>>++>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>++>++>++>++>>>++>>>+++>+>++>++>++>++>++>++++>+++++>+>+>+++++>+++++>+++++>+++++>+++++>+++++>>>+++>+>+>++>++++>>+++>>++++>+>++>++>++>++>++>>>++>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>++>++>++>++>++>++>>>++>>>+++>+>+>++++>+++++>+++>+>++++>++>+++>++++++>+++>++>++>++>++>++>++>++>++>++>>++>>>>>++>+>+>+>+>+>++++>+>+++>>++++>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>+++++>+>+>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>++>+>+>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>++++>>>+++>>++++>+++++>>++++>+++++>+>+>+>+>++++>+>+>+++>>++>>++++>>>+++>>>+++>+>+++>+>++>++++>+>++++>+++++>>>++>+>+>+++>+>+++>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>++++>>++++>+++++>+>+>++>>>+++>>+++>+>++>+>+++++>+>+>++++>+>+>+++>>++++>+++++>>++++>>>+++>>>++>+>+>+>+>++++>+>+>+++>>+++>>++++>+>+>+++>>++++>++++>>>+++>++>++++>+>++++>+++++>>>++>+>+>+++>+>+++>>++>>+++++>+>+>+++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>++++>+>+++>+>+>+>++++>+>+++>+>+>++++>++++>+>+++>+>+>++>++>++>++>++>>>>++++>+++++>+++++>+++++>+++++>+++++>++++>+>+>+++>>++++>++++>>+++>>++>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>++++>+++++>+>+>++++>+>+++>+>+>+++>>+++>+>+>++++>>>>++>++>++>++>++>++>++++>+++++>+>+>++>>>+++>+++>>+++>+>+>++++>+>+++>>++++>+>++>+>+>+++>+++>>+++>>+++++>++++>+>+>+++>>++++>+>+++++>>++++>>+++>>>>>++++>+>+++>+>+>+>++++>++++>+>+++>>++>++>++>++>++>++>++>+>+>+>++++>+++++>+++++>+++++>+++++>+++++>+++++>++++>>>+++>+>++++>>>>>++++>>+++>>++>+>+>++++>+>+++>+>+++>+>++>++++>>>+++>+>++++>>>>>++++>>+++>>++++>+++++>>+++>+>+>++++>>>>+++>+>++++>+>+++>+>+++>>>++++>+>+>+>++>++>++>++>++>++++>+++++>>>++>+>+>+++>+++>+>+++>+>+>++++>+>+++>+++++>+++++>++++>>+++>>+++>+>+>++++>+>+++>>++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>++++>+++++>+++>+>+>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>++++>++++>>+++>>++++>>+++>>>>++++>>+++>>>>>++>+>+>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>+++++>+++>>++++>>+++>>++++>>+++>>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>>+++++++>+>+>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>+++++>+++>+>++>++>++>++>++>++>++>++>++>>>++++>++>+>+>+++++>>>+++>>+++>>>>++++>++++[<+]>[>[>]>[>]>++++++++++[-<++++++>]<++[<]<[<]<+>>-[[>]>[>]>+++++++[-<++++++>]<+[<]<[<]<+>>-]>]<--[>+<++++++]>++>[>]+++++[->+++<]>[>+++>+++>+++>++++++>++++++>+++>++++>++++[<]>-]>+>->>+>+++>-->>++[<]<<[<]<<[<]>[[[>]>>[>]>>[>]<[->>+<<]<[<]<<[<]<<[<]>-]>[>]>>[>]>>[>]>>[-<<+[<]<+>>[>]>]<<[[->+<]<]>>[>]>[[-<+>]>]<<[<]<<[<]<<[<]>]>>>[>]>>[>]<[[-]<]>>,[>+++++++[<------>-]-<-[>]>[<++>>+>>]<<<-[>]>[<+++++++>>+>>]<<<-[>]>[<+>>+>>]<<<-[>]>[<++++++++>>+>>]<<<--------------[>]>[<+++>>+>>]<<<--[>]>[<++++>>+>>]<+++++[-<<------>>]<<+[>]>[<+++++>>+>>]<<<--[>]>[<++++++>>+>>]<<[-]<[+],]+++++++++>+>>>>+<<<<<[<]>[-[<<]>[[>]>>>>-<<<<<[<]<]>-[<<]>[[>]>>>>+<<<<<[<]<]>-[<<]>[[>]>>>[>>]>[->[-<<<<[<<]>+>[>>]>>]<]<+[<[->>+<<]<]<<[<]<]>-[<<]>[[>]>>>[>[-<<+>>]>]<+<-<<[<<]>[->[>>]>>+<<<<[<<]>]>[<<]>[[>>]+[<[->>+<<]<]>+>-<<]<<<[<]<]>-[<<]>[[>]>>>>[<]<<<[<]<<[[<]<<+++++>>>[-----[<<]>[[>]>+<<[<]<]>-[<<]>[[>]>[-<<[<]<<]>]<<[>>>++++++[-<<+>>]]>]<<[<]>[<+<<]]>]>-[<<]>[<->[>]>>>>[<]<<<[[<]>+++++++<<<[------[>>]<[>>>>[>]>+<<[<]<]<+[>>]<[>>>>[>]>[->]<<[>>>]<[<]<]>>[<<<+++++[->>+<<]]<]<<[<]--[>]>]<<[<]>+<]>-[<<]>[[>]>>>>[-]<<<<<[<]<<<[<]<[<]>[[>]>[>]>>>[>]>>>>+<<<<<[<]<<<[<]<[<]>-]>[>]>[>]>]>-[<<]>[[>]>>>>.<<<<<[<]<]>-[<<]>[-]<+++++++++>>[+<<->>]>]>>>[]
Try it online!. The input here is a simple cat program (,[.,]
) which will print the program itself.
->++++>+>++>+++>>++++>>+++ ....... >+++++>>>+++>>+++>>>>++++>+++
This part makes up the data section of the quine, and is by far the majority of the code at 3669 bytes. The beginning -
is the ending marker. Each >+++
represents a character of the code after this section.
Initialises the 8 characters of brainfuck
>[>]>+++++[->+++<]>[>+++>+++>+++>++++++>++++++>+++>++++>++++[<]>-]
>+>->>+>+++>-->>++[<]<<[<]<<<[<]>
Tape looks like:
data 0 0 code 0 0 characters
Runs through the data destructively and adds the represented symbol to the code section
[
[
For each plus in this cell
Shift the gap in the characters over one
[>]>>[>]>>[>]<[->>+<<]
<[<]<<[<]<<[<]>-
]
Navigate to character
>[>]>>[>]>>[>]>>
Copy the character to the end of the code section
[-<<+[<]<+>>[>]>]
Shift the symbol section over one
<<[[->+<]<]
>>[>]>[[-<+>]>]
Navigate to next byte of data
<<[<]<<[<]<<[<]>
]
Remove characters
>>[>]>>[>]<[[-]<]
>>,
[
>(7+++++++)[<(6------)>-]-<-
[>]>
[plus <++>>+>>]<<<
-[>]>
[comma <+++++++>>+>>]<<<
-[>]>
[minus <+>>+>>]<<<
-[>]>
[dot <++++++++>>+>>]<<<
(14--------------)[>]>
[left <+++>>+>>]<<<
--[>]>
[right <++++>>+>>]<
(29+++++[-<<------>>]<<+)
[>]>
[start loop <+++++>>+>>]<<<
--[>]>
[end loop <++++++>>+>>]<<
[-]<[+],
]
(9+++++++++)>+
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
- | + | < | > | [ | ] | , | . |
Represents the end of the program with 9 1
>>>>+<<<<<
[<]>
[
-[<<]>
[minus
[>]>>>>-<<<<<[<]<
]
>-[<<]>
[plus
[>]>>>>+<<<<<[<]<
]
>-[<<]>
[left
[>]>>>[>>]>
[
->
[-<<<<[<<]>+>[>>]>>]<
]
<+
[<[->>+<<]<]
<<[<]<
]
>-[<<]>
[right
[>]>>>
[>[-<<+>>]>]
<+<-<<[<<]>
[->[>>]>>+<<<<[<<]>]
>[<<]>
[
[>>]+
[<[->>+<<]<]
>+>-<<
]
<<<[<]<
]
>-[<<]>
[start loop
[>]>>>>[<]<<<[<]<<
[
[<]<<
(5+++++)
>>>
[
checks if start loop
(5-----)
[<<]>
[[>]>+<<[<]<]
checks if end loop
>-[<<]>
[
[>]>
[-<<[<]<<]
>
]
<<
[>>>(6++++++)[-<<+>>]]
>
]
<<[<]>[<+<<]
]
>
]
>-[<<]>
[end loop
<->[>]>>>>[<]<<<
[
[<]>(7+++++++)<<<
[
checks if end loop
(6------)
[>>]<
[
add one to marker
>>>>[>]>+<<[<]<
]
<
checks if start loop
+[>>]<
[
>>>>[>]>
if marker not 0
[->]<<
if marker is 0
[>>>]<[<]<
]
>>
[<<<(5+++++)[->>+<<]]<
]
<<[<]--[>]>
]
<<[<]>+<
]
>-[<<]>
[input
[>]>>>>[-]<<<<<[<]<<<[<]<[<]>
[
[>]>[>]>>>[>]>>>>+<<<<<[<]<<<[<]<[<]>-
]
>[>]>[>]>
]
>-[<<]>
[output
[>]>>>>.<<<<<[<]<
]
>-[<<]>
[end-]
<(9+++++++++)>>
[+<<->>]>
]
>>>[]
Always returns 0 (halts and returns 0)
Always returns 1 (runs forever)
Returns all input added together, mod 256 (runs forever)
brainfuck, 6013 4877 4376 bytes
Edit: -1136 bytes. Switched to a better way of generating the data for the quine
Edit2: -501 bytes. Revisited my self-interpreter and cut it down a couple of hundred bytes
->++>++++>+>++>+++>>++++>>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>++++>++++>>+++>>++++>>+++>>++>++>++>++>++>++>++>++++>+++++>+>++>++>++>++>++>++>>+++>+>++>++++>+>+++>+>++++>+>+++>+>++>>>+++++>+++>>+++>+>+++++>+++++>++++>>++>+>++>++>++>++>++>++>+++>>++>++>>++++>>+++>++>++>++>++>++>++++>+++++>>++>++>++>+>+++>>++++>>++>++>++>>++>++>++>>++>++>++>>++>++>++>++>++>++>>++>++>++>++>++>++>>++>++>++>>++>++>++>++>>++>++>++>++>++++>+>+++>>+++++>+++>>++>>+++++>>>++>>++>++>++>>+++++>+++++>>>++>++>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>++++>++++>++++>>+++>>>++++>>+++>>>++++>>+++>+>++++>+++++>>>++>+>+>+++>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++++>+++>>++++>>+++>>>++++>>+++>>>++++>>+++>>>++++>+++++>+>+>++>++++>+>+++>+>++>>>++++>>+++>>+++>+>+>++++>++++>+++++>>++>+>+++>+>+++>>>++++>>+++>>++++>++++>+++++>+>++>>+++>>+++>+>+>++++>+>+++>+>+>++++>+>+++>+>+>++++>+>+++>>+++>>>>++++>>+++>>>++++>>+++>+>++++>++++>+++++>+++>+>+++>>>>++++++>++++>>++>++>++>++>++>++>++>++++>+>+++++>+++++>+++++>+++++>+++++>+++++>>+++++>+++>++>+>+++++>++++>>+++>>++++>+>++>+>++>>>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>+>++>>>>>+++>+>+>+>+++++>++++>>+++>>++++>+>++>+>++>>>>>+++>+>+>+>+++++>++++>>+++>>++++>+>+++++>+>++>++>++>>>>>+++>+>+>+>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>+++++>++++>>+++>>++++>+>++>++>+>++>>>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>+++++>+>++>++>++>++>++>++>++>>>>>+++>+>+>++>++>++>++>++++>+++++>+>+++++>+++++>+++++>+++++>+++++>+++++>>+++>++>+>++>++++>>+++>>++++>+>++>++>+>++>>>>>+++>+>+>+>+++++>+++++>++++>>+++>>++++>+>+++++>+>++>>>>>+++>+>+>+++++>+>++++>++>+++>+>++++>>+++>>++++++>+++>>>>++>+>+>+>+>+++++>++++>+>+++>>++++>+++++>++++>+>+>+++>>++++>+>++>++++>>+++>>>++++>+>+++>+>+>++++>>>++>++++>+>+>++++>+>+++>+>+>+++++>++++>>>+++>+>++++>>>>>++++>>+++>>++>+>+>++++>+>+++>+>+++>+>+++++>++++>>>+++>+>++++>>>>>++++>>+++>>+++++>+>+>++++>+>+++>+>+++>+>++>++>++++>+++++>>>++>+>+>+++>>>++++>>+++>>+++>+>+++>+>++++>+>+++>>+++++>+>+++>>+++++>++++>+>+>+++>>++++>+>++>++>++++>>+++>>>++++>+>+>+++>>++++>+>+>++>++++>+>+>++++>+>+++>>++++>+++++>+>+>++>>>+++>>+++++>+++++>++++>+>+>+++>>++++>++++>>+++>>++>+>+>++++>+>+++>+>+++>>++>++++>+>+>+++>>++++>++++>>+++>>+++++>+>+>++++>+>+++>+>+++>>++>++++>>+++>>+++>+++>+>+>++++>+>+++>>+++++>+++++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>+++++++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>+++++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>++++++>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>++++>++++>>+++>>++++>>+++>>>>++++>>+++>>>++>+>+>+>++++>+>+++>+>+>+>++++>+>+++>+>++++>+>+++>>+++++>+++>>++++>>+++>>++++>>+++>>>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>++>+>+>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>>>++++>>>+++>>++++>+>+>+>++++>+>+>+++>+>++>>>>++++>>>+++>>+++++>+++>>++++>+++++>+++>+>+>++>++++>+>++++>+++++>>>++>+>+>+++>+>+++>+>++++>+++++>>>++>+>+>+++>+>++++>+>+++>+>+++>>+++++>++++>+>+>+++>>++++>++++>>+++>>++>++>++++>+++++>+++++>>++++>+++++>+>+>++>>>+++>>+++>++>+>+>+++++>++++>++>++>+>+>+++>+>++++>+++++>>>>++++>>>+++>>++>+>+>+>++++>+>+>+++>+>+++>+>++++>+>+++>+>+++>+>++>++>++>++>++>++>++>++>>>++++>++>+>+>+++++>>>+++>>+++>>>++++>++++[<+]>[>[>]>[>]>++++++++++[-<++++++>]<++[<]<[<]<+>>-[[>]>[>]>+++++++[-<++++++>]<+[<]<[<]<+>>-]>]<--[>+<++++++]>++>[>]+++++[->+++<]>[>+++>+++>+++>++++++>++++++>+++>++++>++++[<]>-]>+>->>+>+++>-->>++[<]<<[<]<<[<]>[[[>]>>[>]>>[>]<[->>+<<]<[<]<<[<]<<[<]>-]>[>]>>[>]>>[>]>>[-<<+[<]<+>>[>]>]<<[[->+<]<]>>[>]>[[-<+>]>]<<[<]<<[<]<<[<]>]>>>[>]>>[>]<[[-]<]>>>,[>+++++++[<------>-]+<-[>]>[<+<+>>>>]<<<-[>]>[<+<+>>>>]<<<-[>]>[<+<+>>>>]<<<-[>]>[<-<+++>>>>]<<<--------------[>]>[<++<+>>>>]<<<--[>]>[<-<+++++++>>>>]<<++++[-<------>]+<+[>]>[<++<+>>>>]<<<--[>]>[<-<+>>>>]<<-<[+]<[>]>,]>>>+<<<<-[<]>[-[<<]>[<+[>]>>[<]<<[>>+[<<[<]<<-[>>]<[>>>>[>]>+<<[<]<]<-[>>]<[>>>>[>]>-<<[<]<]<++[->>+<<]>>[>]>]<]<[<]>-<]>-[<<]>[<++[>]>>[<<]>[<<+[<<[<]>[-<<+>>]>--[<<]>[[>]>+<<[<]<]>+[<<]>[[>]>-<<[<]<]>+[>]>]]<<[<]>--<]>-[<<]>[[>]>>.<<<[<]<]>-[<<]>[[>]>>-<<<[<]<]>-[<<]>[[>]>>,<<<[<]<<<[<]<[<]>[[>]>[>]>>>[>]>>+<<<[<]<<<[<]<[<]>-]>[>]>[>]>>[<]<]>-[<<]>[[>]>>+<<<[<]<]>-[<<]>[[>]>>>[>>]>[<<<[<<]<+>>>[>>]>-]>[-]<<+[<[->>+<<]<]<[->>+<<]<[<]<]>-[<<]>[[>]>++[-->[-<<+>>]>]+<<-[++<<]<[->>>[>>]>+<<<[<<]<]<[<]<]<++++++++>>[+<<->>]>]>>[]
Try it online! The input here is a simple cat program (,[.,]
) which will print the program itself.
->++>++++> ....... >+++++>>>+++>>+++>>>++++>+++
This part makes up the data section of the quine, and is by far the majority of the code at 3270 bytes. The beginning -
is a marker for the beginning of the data. Each >+++
represents a character of the code after this section.
Initialises the 8 characters of brainfuck
>[>]+++++[->+++<]>[>+++>+++>+++>++++++>++++++>+++>++++>++++[<]>-]
>+>->>+>+++>-->>++[<]<<[<]<<[<]>
Tape looks like:
data 0 0 code 0 0 characters
Runs through the data destructively and adds the represented symbol to the code section
[
[
For each plus in this cell
Shift the gap in the characters over one
[>]>>[>]>>[>]<[->>+<<]
<[<]<<[<]<<[<]>-
]
Navigate to character
>[>]>>[>]>>[>]>>
Copy the character to the end of the code section
[-<<+[<]<+>>[>]>]
Shift the symbol section over one
<<[[->+<]<]
>>[>]>[[-<+>]>]
Navigate to next byte of data
<<[<]<<[<]<<[<]>
]
Remove characters
>>[>]>>[>]<[[-]<]
>>>,
[
>(7+++++++)[<(6------)>-]+<-
[>]>
[plus <+<+>>>>]<<<
-[>]>
[comma <+<+>>>>]<<<
-[>]>
[minus <+<+>>>>]<<<
-[>]>
[dot <-<+++>>>>]<<<
(14--------------)[>]>
[left <++<+>>>>]<<<
--[>]>
[right <-<+++++++>>>>]<<
(29++++[-<------>]+<+)
[>]>
[start loop <++<+>>>>]<<<
--[>]>
[end loop <-<+>>>>]<<
-<[+]<[>]>,
]
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
] | [ | . | - | , | + | > | < |
Represents the end of the program with 255
.
Initialise simulated tape
>>>+<<<<-
[<]>
[
-[<<]>
[end loop
co 0 0 0 e: _1 0 0 0 1 ?
Check if current cell is one
<+[>]>>[<]<<
co 0 0 1 e _1: 0 0 !0 1
or
co 0 0 1 e _1 0: 0 0 1
[ If current cell is one navigate to corresponding start loop
Create counter
>>+
[
co 0 0 de _1 0 c: !0 1
checks if next instruction is an end loop
<<[<]<<-
[>>]<
c !0 0: 0 de _1 0 c !0 1
or
c: 0 0 0 de _1 0 c !0 1
[>>>>[>]>+<<[<]<] Add one to counter if it is
checks if start loop
<-[>>]<
c !0 0: 0 de _1 0 c !0 1
or
c: 0 0 0 de _1 0 c !0 1
[>>>>[>]>-<<[<]<] Subtract one from counter if it is
c ? 0: 0 de _1 0 c !0 1
Adds two to counteract checks and move to the next instruction
<++[->>+<<]
>>[>]>
c 0 0 ode _1 0 c: !0 1
End on the counter
If the counter is 0 then we have reached the corresponding bracket
]
c 0 0 2 de _1 0 0: !0 1 0
<
]
c 0 0 1?2 de _1 0: 0 0 1 0
Subtract one from current instruction
This executes the start loop code next but that does nothing
<[<]>-<
]
>-[<<]>
[start loop
c 0 0 0 de: _1 0 0 ? 1
<++[>]>>[<<]>
c 0 0 2 de _1 0 0 0 1:
or
c 0 0 2 de _1 0 0: !0 1
[ If current cell is 0 navigate to corresponding end loop
Initialise counter
<<+
c 0 0 ode _1 0 c: 0 1
[ While counter is not 0
Transfer current instruction over (first instruction is guaranteed to be start loop)
<<[<]>[-<<+>>]>
co 0 0 de _1 0 c: 0 1
Check if start loop
--[<<]>
co 0 0: !0 e _1 0 c 0 1
or
co 0 0 0 e _1 0 c 0 1
[[>]>+<<[<]<] Add one to counter if so
checks if end loop
>+[<<]>
co 0 0: !0 e _1 0 c 0 1
or
co 0 0 0 e: _1 0 c 0 1
[[>]>-<<[<]<] Subtract one from counter if so
Add one to counteract checks and navigate to counter
>+[>]>
co 0 0 de _1 0 c: 0 1
End on counter
If counter is 0 then we have reached the corresponding end loop
]
co 0 1 e _1 0 0: 0 1
]
co 0 0 2?1 e _1 0 0: ? 1
Subtract two from the current instruction to bring it back up to the right value
<<[<]>--<
]
3 of these are pretty self explanatory
Navigate to the current cell and execute the instruction on it
>-[<<]>
[output
[>]>>.<<<[<]<
]
>-[<<]>
[minus
[>]>>-<<<[<]<
]
>-[<<]>
[input
Reset current cell
[>]>>, (no more input so this is set to 0)
co 0 0 0 e: _1 0 0 0: 1 b 1 a 0 d 1 e 1 f
Navigate to start of code section
<<<[<]<<<[<]<[<]>
d: ata 0 co 0 0 0 e _1 0 0 0 1 b
or
0: co 0 0 0 e _1
Transfer next instruction to current cell
[[>]>[>]>>>[>]>>+<<<[<]<<<[<]<[<]>-]
0: ata 0 co 0 0 0 e _1 0 0 d 1 b
or
0: co 0 0 0 e _1
Navigate back to the normal spot
>[>]>[>]>>[<]<
]
>-[<<]>
[plus
[>]>>+<<<[<]<
]
>-[<<]>
[right
Simulated tape looks like:
a b c: d e f
co 0 0 0 e: _1 0 0 c 1 b 1 a 0 d 1 e 1 f
Navigate to value of cell to the right
[>]>>>[>>]>
co 0 0 0 e _1 0 0 c 1 b 1 a 0 d: 1 e 1 f
Transfer it to temporary cell
[<<<[<<]<+>>>[>>]>-]
co 0 0 0 e _1 d 0 c 1 b 1 a 0 0: 1 e 1 f
Pop extra marker if it exists from the right cells and add one to the left
>[-]<<+
co 0 0 0 e _1 d 0 c 1 b 1 a 1: 0 0 e 1 f
Transfer all left cells over 2 cells
[<[->>+<<]<]<[->>+<<]
co 0 0 0 e _1 0: 0 d 1 c 1 b 1: a 0 e 1 f
Navigate back to normal spot
<[<]<
]
>-[<<]>
[left
Simulated tape looks like:
a b c: d e f
co 0 0 0: e _1 0 0 c 1 b 1 a 0 d 1 e 1 f
Add temporary marker
[>]>++
co 0 0 0 e _1 0 2: c 1 b 1 a 0 d 1 e 1 f
Remove temporary marker and transfer all left cells over two
[-->[-<<+>>]>]
co 0 0 0 e _1 c 0 b _1 a _1 0 0: d 1 e 1 f
Add marker to right cells remove marker from left cells and reset left cell's markers
+<<-[++<<]<
co 0 0 0 e _1 c: 0 b 1 a 0 0 1 d 1 e 1 f
Transfer current cell to to right cells
[->>>[>>]>+<<<[<<]<]
co 0 0 0 e _1 0: 0 b 1 a 0 c 1 d 1 e 1 f
Navigate back to normal spot
<[<]<
]
Add 8 to reverse checks
<(8++++++++)>>
Execute next instruction
[+<<->>]>
]
>>[]
Always returns 0 (halts and returns 0)
Always returns 1 (runs forever)
Returns all input added together, mod 256 (returns 211, so it runs forever)
###Fun Fact for those who are still reading If the input for this program is the source code of this program, then it will start recursing, repeatedly creating self-interpreters that run this program and then give it the same program again. This gives me some interesting ideas on creating actual recursive programs in brainfuck. Instead of checking the return value and starting an infinite loop as in this question, the return value can be saved and acted upon. A simple example would be a factorial program that goes
If cell1 == 0:
Get input into cell1
If cell1 == 1:
Return cell1
Else:
Initialise self-interpreter-quine function
Pass cell1-1 into cell1 of the function
Run function
Multiply cell1 by the return value
Return cell1
Of course, this is a completely insane way of coding brainfuck, given that running recursing self-interpreters will increase the runtime exponentially.
##Test Inputs:
Always returns 0 (halts and returns 0)
(empty program)
Always returns 1 (runs forever)
+
Returns all input added together, mod 256 (runs forever)
,[[->+<],]>
,[>,]>(9+++++++++)[-<(10++++++++++)>]<[-<-<->>]+<---[[-]>[-]<]<-[[-]>>[-]<<]>+>[-<->]<
##Test Inputs:
Always returns 0 (halts and returns 0)
(empty program)
Always returns 1 (runs forever)
+
Returns all input added together, mod 256 (runs forever)
,[[->+<],]>
,[>,]>(9+++++++++)[-<(10++++++++++)>]<[-<-<->>]+<---[[-]>[-]<]<-[[-]>>[-]<<]>+>[-<->]<