Compose two Brainfuck programs
brainfuck, 526 bytes
-<+++++<+<+++++<-[+++++++[<++++++++>-]<--->>]<<--<--<+..<<<<,[[<+>>+<-]----[>-<-
---]>[+[++[++++++++++++++[++[[-]<]<<<[>-->>>-.>>>.<<<.>.>.<<.+>>--..>.<..<.>.>..
<++.<.>..>.<..<.>--.>>.<<.>.<..<.>.>.<.<.>++.<<.>.>.>.<.<.>..>.<..<.>>>.<<--.++<
<<]>>>>[<]<]>[<<<<[>]>[+>>>>>.>.<<<.>.<.>>>.<.[.<.>>>]<++[<<.<.>>.<<--.<<.>.>.++
>>>-]<<<<.>.>>.<.<<<.>>..>>.<<<.>--.>.<++...<<.>>--..>>.<.<..<.>.>>.<<++...>.<..
<.>>>.<<--...++<<[.>]<<<--.<<.>>++.<.>>>.<<--.++<<<]]<]>[<<<.>>]<]>[>>.>.<<<.>.>
>>.[<]]<]>[>>.>.<..<.>>>.[<]<<<+>->>]<<.[-]>,]
Formatted:
-<+++++<+<+++++<-[+++++++[<++++++++>-]<--->>]
<<--<--<+..<<<<
,
[
[<+> >+<-]
----[>-<----]>
[
not question mark
+
[
not greater than
++
[
not less than
++++++++++++++
[
not period
++
[
not comma
[-]<
]
<<<
[
comma B
>-->>> -.>>>.<<<.>.>.<<.+>>--..>.<..<.>.>..<++.<.>..>.<..<.>--.>>.<<
.>.<..<.>.>.<.<.>++.<<.>.>.>.<.<.>..>.<..<.>>>.<<--.++<<<
]
>>>>[<]<
]
>
[
period
<<<<
[
B
>
]
>
[
A
+>>> >>.>.<<<.>.<.>>>.<.[.<.>>>]<++[<<.<.>>.<<--.<<.>.>.++>>>-]<<<<.
>.>>.<.<<<.>>..>>.<<<.>--.>.<++...<<.>>--..>>.<.<..<.>.>>.<<++...>.<
..<.>>>.<<--...++<<[.>]<<<--.<<.>>++.<.>>>.<<--.++<<<
]
]
<
]
>
[
less than
<<<.>>
]
<
]
>
[
greater than
>>.>.<<<.>.>>>.[<]
]
<
]
>
[
question mark
>>.>.<..<.>>>.[<]
<<<+>->>
]
<<.[-]>,
]
With respect to A, B, and C: EOF = 0, cells left of start disallowed, 8-bit wrapping cells.
Expects A followed by ?
followed by B.
Try it online
(This answer can be made compliant with a brainfuck interpreter that doesn't allow going left of the start at the cost of one byte by transliterating y/<>/></
and prepending a >
.)
The basic idea is to use a series of string replacements in order to simulate the tapes of A and B using 2-cell nodes, with special attention given to replacing .
in A and ,
in B so that the intermediate data stream is kept in a block of cells to the left of the simulated tape. The string replacement scheme is:
Insert
>>
before AIn both A and B, replace
>
with>[-]+>
and<
with<<
In A, replace
.
with>[-]-[>>]+[[>+<-]<[>+<-]<]>+[->>+]<[>>>-<<+[<<]<+>>>[>>]+<<<-]>[+<->]+</
Insert
>[>>]+>
after A and before BIn B, replace
,
with,[,]>,<<[<<]<[[>]>>[>>]<+<[<<]<[<]>-]>[>]>>[>>]+<*
brainfuck, 1287 bytes
++++++++++++++++[->+++>+++>++++>++++>++++++>++++++<<<<<<]>----->--->---->-->----->--->+<<<.....<<.>>....<<.>>.<<..>...>>>>[>+>,->[-]++++[-<-------->]+<[>>]>[<<-<-<<<..>.<.....>>.<<<.....>>.<<<<.>>..>>.<<<.>>>>.<<<...>>>.<<.<<<..>>>>.<<<..>>.....<<<..>>>>>.<<<<<.>>>>.<<<.>.....<<.>>>>>.<<<<.>>..>>>>>>>>]<<<-------------[>>]>[<<-<<<<..<<.>..>>.<<<.>.<<.>>>....<<<.>>>>.<<<.>.....<<.>>>>>.<<<<<.>>>>.<<<..>>.....<<<..>>>>>.<<<<..>..<<.>>>..<<<.>>>>.<<<.>.....<<.>>>>>.<<<<.>.<<..>>>>.<<<..>>.....<<<..>>>>>.<<<<..>..>>>.<<<.>>.<<<.>>.<<<.>>.>>>.<<....<<<.>>>>.<<<.>.....<<.>>>>>.<<<<<.>>>>.<<<..>>.....<<<..>>>>>.<<.....<<.>>>.<<<.>.....<<.>>>>>.<<<<.>.<<..>>>>.<<<..>>.....<<<..>>>>>.<<<<.>..>>>>>>>>>]<<<--------------[>>]>[<<-<<<<<...>>.<<<<.>>>>>.<<<<.>..>>>>>>>>>]<<<--[>>]>[<<-<<<<.......>.<<<<.>>>>>.<<<<.>..>>>>>>>>>]<<<<[+++++++[->++++++++<]>--.<]<]>>,[<+>>++++[-<--------->]+<+[>>]>[<<-<<<.<<<.>>>>.<<..<<.>>.>.<.....>>.<<<<<.>>>>.<<<.>.....<<.>>>>>.<<<<.>>...>.<<<.>...>>.<<.....>>>.<<....<<<.>>>>.<<<.>>.....<<<.>>>>>.<<<<.>..<<.>>>...>.<.....>>.<<<<<.>>>>.<<<.>.....<<.>>>>>.<<<<.>>...>>.<<..<<.>>>.<<.....>>>.<<....<<<.>>>>.<<<.>>.....<<<.>>>>>.<<<..>>>>>>>>>]<<<----------------[>>]>[<<-<<<<<.....>>>>>>>>>]<<<--[>>]>[<<-<<<<.....>>>>>>>>]<<<<[+++++++[->++++++++<]>--.<]>,]
Try it online!
Here it is! The brainfuck code that composes two brainfuck codes. Use a "!" to separate the two input code snippets. For example snippet A: >,[>,]<[.<]
, snippet B: ,[...,]
. Input for my program: >,[>,]<[.<]!,[...,]
. It will not terminate if no "!" is found.
This does essentially the same as my VBA Version. The code that is generated is the same as in the VBA version (note that the examples in the VBA post were made before the latest change in the brainfuck snippets).
Explanation
This is my source code:
[
Tape: "+"(43) "-"(45) "<"(60) ">"(62) "["(91) "]"(93) readA(1) printInput(0) input else exitIf exitElse
]
++++ ++++ ++++ ++++ [->+++>+++>++++>++++>++++++>++++++<<<<<<]>----->--->---->-->----->--->+
<<<.....<<.>>....<<.>>.<<..>... print init routine
>>>>[ while readA
>+ set printInput = true
>, read character
->[-]++++[-<-------->] decrease input by 33 to check for "!"
+ set else flag
# A check for "!"
<[ if input not "!"
>> go to exitIf
]
>[ else
<<- set printInput = false
<- set readA = false
<<<..>.<.....>>.<<<.....>>.<<<< print routine between A and B
.>>..>>.<<<.>>>>.<<<...>>>.<<.<
<<..>>>>.<<<..>>.....<<<..>>>>>
.<<<<<.>>>>.<<<.>.....<<.>>>>>.
<<<<.>>..>>>
>>>>> go to exitElse
]
# A check for "dot"
<<<----- ----- --- decrease input by 13 (total 46) to check for dot
[ if input not dot
>> go to exitIf
]
>[ else
<<- set printInput = false
<<<<..<<.>..>>.<<<.>.<<.>>>.... print list storing routine
<<<.>>>>.<<<.>.....<<.>>>>>.<<<
<<.>>>>.<<<..>>.....<<<..>>>>>.
<<<<..>..<<.>>>..<<<.>>>>.<<<.>
.....<<.>>>>>.<<<<.>.<<..>>>>.<
<<..>>.....<<<..>>>>>.<<<<..>..
>>>.<<<.>>.<<<.>>.<<<.>>.>>>.<<
....<<<.>>>>.<<<.>.....<<.>>>>>
.<<<<<.>>>>.<<<..>>.....<<<..>>
>>>.<<.....<<.>>>.<<<.>.....<<.
>>>>>.<<<<.>.<<..>>>>.<<<..>>..
...<<<..>>>>>.<<<<.>..>>>>>
>>>> go to exitElse
]
# A check for "less than"
<<<----- ----- ---- decrease input by 14 (total 60) to check for "less than"
[ if input not "less than"
>> go to exitIf
]
>[ else
<<- set printInput = false
<<<<<...>>.<<<<.>>>>>.<<<<.>..>>>>> print A move left routine
>>>> go to exitElse
]
# A check for "greater than"
<<<-- decrease input by 2 (total 62) to check for "greater than"
[ if input not "greater than"
>> go to exitIf
]
>[ else
<<- set printInput = false
<<<<.......>.<<<<.>>>>>.<<<<.>..>>>>> print A move right routine
>>>> go to exitElse
]
# print remaining character
<<<<[ if printInput
+++++++[->++++++++<]>--.< print original character
]
< go to readA
]
>>, read next character
[ while input
<+ set printInput = true
# B check for comma
>>++++[-<----- ---->]+<+ decrement input by 44 to check for comma
[ if input not comma
>> go to exitIf
]
>[ else
<<- set printInput = false
<<<.<<<.>>>>.<<..<<.>>.>.<..... print B list reading routine
>>.<<<<<.>>>>.<<<.>.....<<.>>>>
>.<<<<.>>...>.<<<.>...>>.<<....
.>>>.<<....<<<.>>>>.<<<.>>.....
<<<.>>>>>.<<<<.>..<<.>>>...>.<.
....>>.<<<<<.>>>>.<<<.>.....<<.
>>>>>.<<<<.>>...>>.<<..<<.>>>.<
<.....>>>.<<....<<<.>>>>.<<<.>>
.....<<<.>>>>>.<<<..>>>>>
>>>> go to exitElse
]
# B check for "less than"
<<<----- ----- ----- - decrease input by 16 (total 60) to check for "less than"
[ if input not "less than"
>> go to exitIf
]
>[ else
<<- set printInput = false
<<<<<.....>>>>> print B move left routine
>>>> go to exitElse
]
# B check for "greater than"
<<<-- decrease input by 2 (total 62) to check for "greater than"
[ if input not "greater than"
>> go to exitIf
]
>[ else
<<- set printInput = false
<<<<.....>>>> print B move right routine
>>>> go to exitElse
]
# print remaining character
<<<<[ if printInput
+++++++[->++++++++<]>--.< print original character
]
>, input next character
]
VBA, 512 489 479 bytes
Sub f(A,B):Debug.?">>>>>->>>>->--<<<"&Replace(Replace(Replace(A,"<","<<<[+]-<<"),">",">>>>>>>[+]-<<"),".",">>-<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>>+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]-<<")&">>[>>>>>]<<<<<[+<<[-]<<<]>++[-->>>>>++]+[-<<<<<+]->>"&Replace(Replace(Replace(B,"<","<<<<<"),">",">>>>>"),",","[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<"):End Sub
Explanation
The VBA code changes the brainfuck code in a way, that the output of snippet A will be stored in a list and the input of snippet B will be read from that list.
It first initializes some variables
>>>>>->>>>->--<<<
Then it reads snippet A and replaces every <
by <<<[+]-<<
, every >
by >>>>>>>[+]-<<
and every .
by the storing routine
>>>>>>>[+]-<<"),".",">>->+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]--<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>+<<
after that it deletes the memory of snippet A and makes changes to the stored list, so it can be read as input for snippet B:
>>[>>>>>]<<<<<[+<<[-]<<<]>++[-->>>>>++]+[-<<<<<+]->>>>>-<<<
Then snippet B will be read, every <
will be replaced by <<<<<
, every >
will be replaced by >>>>>
and every ,
will be replaced by the list reading routine:
[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<
Brainfuck source code
This is my source for the brainfuck portions of the code. I will explain that in detail later.
[
Tape: tempMem data out/in dataAnchors outAnchors (all repeating)
dataAnchors of Snippet A: -1 = memory has been used, -2 = active memory cell for copying to out/in
dataAnchors of Snippet B: -1 = active memory cell for copying from out/in
outAnchors of Snippet A: -1 = start of list, -2 = next output position
outAnchors of Snippet B: -1 = character has been read (or start of input)
]
### Init
>> two blank data cells (for non wrapping pointer)
>>>- set start of input
>> >>- set first "used" flag
>-- set end of input
<<< return to first usable data cell
### A move right routine
>>>>> move to the next data cell
>>[+]- clear and set "used" flag
<< return to data cell
### A move left routine
<<<[+]- clear and set "used" flag of previous data cell
<< go to data cell
### A print routine
>>- set flag
<<[ while value greater 0
- decrement value
<+ increment tempMem
>>>>+[-<<<<<+] find start of input
+[-->>>>>++]-- find end of input
<<+ increment input
>>+[-<<<<<+]- find start of input
<++[-->>>>>++]--find flag
<< go to active data cell
]
<[->+<] move stored value back to data
>>>>+[-<<<<<+] find start of input
+[-->>>>>++] find end of input
>>>>>- set new end of input
[-<<<<<+]- return to start of input
<++[-->>>>>++]- return to and delete data flag
<< go to data cell
### After snippet A: Delete memory of A and configure out/in list
>>[>>>>>]<<<<< go to last used data
[+<<[-]<<<] delete each data
>++[-->>>>>++] find and delete end of input flag
+[-<<<<<+]- go to start of input
>> go to first data cell
### B move right routine
>>>>> go to next data cell
### B move left routine
<<<<< go to previous data cell
### B reading routine
[-] set cell = 0
>>- set flag
>[>>>>>]+[-<<<<<+]- find start of input
>>>[ if value greater 0
- decrement value
<<<[<<<<<]>>>> go to initial start of input (combined with next)
+[->>>>>+]- find mem cell flag
<<+ increment mem cell
>>>[>>>>>]+[-<<<<<+]->>> return to input cell
]
>>- set new start of input
[<<<<<]>>>> go to initial start of input (combined with next)
+[->>>>>+] find and delete mem cell flag
<< go to mem cell
Output for test case 1: f ",[..,]",",[...,]"
>>>>>->>>>->--<<<,[>>->+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]--<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>+<<>>->+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]--<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>+<<,]>>[>>>>>]<<<<<[+<<[-]<<<]>++[-->>>>>++]+[-<<<<<+]->>>>>-<<<[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<[...[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<]
Try it online!
Output for test case 2: f ">,[>,]<[.<]",",[...,]"
>>>>>->>>>->--<<<>>>>>>>[+]-<<,[>>>>>>>[+]-<<,]<<<[+]-<<[>>->+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]--<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>+<<<<<[+]-<<]>>[>>>>>]<<<<<[+<<[-]<<<]>++[-->>>>>++]+[-<<<<<+]->>>>>-<<<[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<[...[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<]
Try it online!
Output for test case 3: f ",.",",."
>>>>>->>>>->--<<<,>>->+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]--<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>+<<>>[>>>>>]<<<<<[+<<[-]<<<]>++[-->>>>>++]+[-<<<<<+]->>>>>-<<<[-]>>->[>>>>>]+[-<<<<<+]->>>[-<<<[<<<<<]>>>>+[->>>>>+]-<<+>>>[>>>>>]+[-<<<<<+]->>>]>>-[<<<<<]>>>>+[->>>>>+]<<.
Try it online!
Complex test case:
Snippet A: Build alphabet triangle >+++++[<+++++>-]<+[>>[>[.>]]>++++++++++.--[<++++++++>-]<[+.<]<-]>>,>[.>]++++++++++.[<[.<]>,>[.>]<]
Try it online!
Snippet B: Sort input in ascending order >>,[>>,]<<[[-<+<]>[>[>>]<[.[-]<[[>>+<<-]<]>>]>]<<]
Try it online!
Result:
>>>>>->>>>->--<<<>>>>>>>[+]-<<+++++[<<<[+]-<<+++++>>>>>>>[+]-<<-]<<<[+]-<<+[>>>>>>>[+]-<<>>>>>>>[+]-<<[>>>>>>>[+]-<<[>>-<<[-<+>>>>+[-<<<<<+]+[-->>>>>++]--<<+>>+[-<<<<<+]-<++[-->>>>>++]--<<]<[->+<]>>>>+[-<<<<<+]+[-->>>>>++]>>>>>-[-<<<<<+]-<++[-->>>>>++]-<<>>>>>>>[+]-<<]]>>>>>>>[+]-<<++++++++++>>-<<[-<+>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