[
This program was built with BFStak v0.3 (https://github.com/wmww/bfstack)
It should run in any standard Brainfuck environment

EOF may be 0, -1 or cell unchanged

use "../framework/bfstack.bf"
]

bfstack/header{
++++++++++[[>>>+<<<-]>>>-]+>+>>>>+>++>+>+>>>+[[-]<<<<<[>>>+>+<<<<-]>>>[<<<+>>>-]>
}

std(1)
bfstack/std{
[>+<-[[<+>-]>-<]>[<+>-]<[-<<<[>>+>+<<<-]>>[<<+>>-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<[
>+>+<<-]>[<+>-]>[>+<-[[<+>-]>-<]>[<+>-]<[->+><+><<]]<[>+<-]>[>[-]+++++<[-]]]]<[>
+<-]>[>[-]++++<[-]]]]<[>+<-]>
}

main_module(2)
bfstack/mod_start{ [>+<-[[<+>-]>-<]>[<+>-]<[- <<<[>>+>+<<<-]>>[<<+>>-]> }

main(1)
bfstack/sub_start{ [>+<-[[<+>-]>-<]>[<+>-]<[-<<[>+>+<<-]>[<+>-]>[>+<-[[<+>-]>-<]>[<+>-]<[->+> }
    = M S L 0 | 0 1 | `i 0 0 0 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    i is the number of bf characters since a newline

    get and copy input character
    >,[>+>> >>> >>> >>+<<< <<< <<< <<-]>>> >>
    = 0 1 | i 0 c 0 | 0 0 | `0 0 0 0 | 0 0 | c 0 0 0

    invoke 2::process_char(2)
    ++>++>+>>>>
    = 0 1 | i 0 c 0 | 0 0 | 2 2 1 0 | 0 0 | `c 0 0 0
    bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
    = 0 1 | i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | `found_match 0 0 0

    found_match will be true if the character was a brainfuck character
    [
        if found_match bump i
        <<< <<< <<< <<<+>>> >>> >>> >>>[-]
        = 0 1 | !i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | `0 0 0 0
        = 0 1 |  * 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | `0 0 0 0
    ]

    copy i (used to determine if to print newline)
    <<< <<< <<< <<<[>+>> >>> >>> >>>+<<< <<< <<< <<<-]>[<+>-]>>> >>> >>> >>> >
    = M S L 0 | 0 1 | i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | i 0 `0 0

    put the max line length in the 2nd slot to compare
    ++++++++++[<++++++++>-]<
    = M S L 0 | 0 1 | i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | i `80 0 0
    = M S L 0 | 0 1 | i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | i `max 0 0

    invoke 2::cell_eq(3)
    <<< <<< <++>+++>+>>>>
    = 0 1 | i 0 c 0 | 0 0 | 2 3 1 0 | 0 0 | `i max 0 0
    bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
    = 0 1 | i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | `newline 0 0 0

    if newline print a newline and clear i
    [
        [-]++++++++++
        = 0 1 | i 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | `10 0 0 0
        .[-]
        <<< <<< <<< <<<
        [-]
        >>> >>> >>> >>>
        = 0 1 | 0 0 c 0 | 0 0 | 0 0 0 0 | 0 0 | `0 0 0 0
    ]

    <<< <<< <<< <

    = M S L 0 | 0 1 | i 0 `c 0

    if c is not 0 or neg 1 do tail recursion by setting the return code to 0 and setting our label to 1
    if c is null we will return
    [+[
        <<<-<<<[-]+>>>>>>[-]
        = M S 1 0 | 0 0 | i 0 `0 0
    ]]<<
    = M S * 0 | 0 * | `i 0 0 0
bfstack/sub_end{ <<]]<[>+<-]>[>[-]+++++<[-]]]]<[>+<-]> }

process_char(2)
bfstack/sub_start{ [>+<-[[<+>-]>-<]>[<+>-]<[-<<[>+>+<<-]>[<+>-]>[>+<-[[<+>-]>-<]>[<+>-]<[->+> }
    = 0 1 | `c 0 0 0 | 0 0
    [>+<-]>>
    = 0 1 | 0 c `0 0 | 0 0

    [-][ check + ]
    >
    ++++++++++
    ++++++++++
    ++++++++++
    ++++++++++
    +++
    = 0 1 | 0 c 0 `43 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | 0 c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | 0 c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | 0 c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | 0 c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | 0 c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | eq c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check , ]
    >+
    = 0 1 | * c 0 `44 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check - ]
    >+
    = 0 1 | * c 0 `45 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check . ]
    >+
    = 0 1 | * c 0 `46 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check < ]
    >++++
    ++++++++++
    = 0 1 | * c 0 `60 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check > ]
    >++
    = 0 1 | * c 0 `62 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check open brace ]
    >++++++++
    ++++++++++
    ++++++++++
    +
    = 0 1 | * c 0 `91 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    [-][ check close brace ]
    >++
    = 0 1 | * c 0 `93 | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
    do_test{
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        copy c
        <<[>+>> >>> >>> >>+<<< <<< <<< <<-]>[<+>-]>
        = 0 1 | * c 0 `bf | 0 0 | 0 0 0 0 | 0 0 | c 0 0 0
        copy bf (the brainfuck character we're checking)
        [>+>> >>> >>> >+<<< <<< <<< <-]>[<+>-]>>> >>> >>>
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | c `bf 0 0
        invoke 2::cell_eq(3)
        <<< <<< <++>+++>+>>> >
        = 0 1 | * c 0 bf | 0 0 | 2 3 1 0 | 0 0 | `c bf 0 0
        bfstack/invoke{ <<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>> }
        = 0 1 | * c 0 bf | 0 0 | 0 0 0 0 | 0 0 | `eq 0 0 0
        [<<< <<< <<< <<<+>>> >>> >>> >>>-]<<< <<< <<< <
        = 0 1 | * c `0 bf | 0 0 | 0 0 0 0 | 0 0 | 0 0 0 0
        = 0 1 | eq c `0 ~
    }

    >[-]<<<
    = 0 1 | `found_match c 0 0
    copy found_match
    [>>+>+<<<-]>>>[<<<+>>>-]
    = 0 1 | found_match c found_match `0
    if found_match print the character
    <[[-]<.>]
    = 0 1 | found_match c `0 0
    <[-]<
    return found_match
    = ~ 0 1 | `found_match 0 0 0
bfstack/sub_end{ <<]]<[>+<-]>[>[-]+++++<[-]]]]<[>+<-]> }

cell_eq(2)
bfstack/sub_start{ [>+<-[[<+>-]>-<]>[<+>-]<[-<<[>+>+<<-]>[<+>-]>[>+<-[[<+>-]>-<]>[<+>-]<[->+> }
    = 0 1 | `a b 0 0
    [>-<-]+
    = 0 1 | `1 * 0 0
    >[<->[-]]<
    = ~ 0 1 | `ret 0 0 0
bfstack/sub_end{ <<]]<[>+<-]>[>[-]+++++<[-]]]]<[>+<-]> }

bfstack/mod_end{ [>[-]++++<[-]]]]<[>+<-]> }

= ~

bfstack/footer{
[>+<-[[<+>-]>-<]>[<+>-]<[-<<<[>>+>+<<<-]>>[<<+>>-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<[
>+>+<<-]>[<+>-]>[>+<-[[<+>-]>-<]>[<+>-]<[->+>[-][]++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++>++>+>>
>><<]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<<<[-]>[-]>[-]>>>><<<<<<<<]]<[>+<-]>[>[
-]+++++<[-]]]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-<<[>+>+<<-]>[<+>-]>[>+<-[[<+>-]>
-<]>[<+>-]<[->+>[-][]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++.[-]<<]]<[>+<-]>[>[-]+++++<[-]]]]<[>+<-]>
[>[-]++++<[-]]]]<[>+<-]>[>[-]+++<[-]]>[<+>-]<+[>+<-[[<+>-]>-<]>[<+>-]<[->+<]]<[>
+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[->+<+[-<<<<<<+>[[-]<->]<]>+<<<+<<[<<+<<<<<<+>>>>>>
>>-]<<[>>+<<-]>>>[>>+<<<<<+>>>-]>>[<<+>>-]<[>+>+<<-]>[<+>-]>>+[->>>>>+>[[-]<->]<
[>+<-]<<<<<<[>>>>>>+<<<<<<-]<<<<<<[>>>>>>+<<<<<<-]<<<<<<[>>>>>>+<<<<<<-]>>>>>>>>
>>>>>>>>>>>]+<<[-]<[-]<[-]<[-]>>>>[<<+>>-]<<<<<<[>>>+<<<-]<<<<<<[>>>>>>>>+<<<<<<
<<-]>>>>>>>>>>>>]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-[-][]++++++++++.++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++.-.---------.---------
--------------------------------------------------.[-]]]<[>+<-]>[>+<-[[<+>-]>-<]
>[<+>-]<[-[-][]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++.+++++++++++++..+++++++++++++.-----------------------------.-.+++.++++++++++
+++++++++++++++++.------------------.++.-----------.----------------------------
------------------------------.[-]]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-[-][]+++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++..
+++++++++++++.-----------------------------.-.+++.+++++++++++++++++++++++++++.--
----------.++.-------------------.----------------------------------------------
----------.[-]]]<[>+<-]>[>+<-[[<+>-]>-<]>[<+>-]<[-[-][]+++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++..+++++++++++++.------
-----------------------.-.+++.+++++++++++++++++++++++++++.-------------------.--
---------.+.+++.+++++++.--------------------------------------------------------
----------.[-]]]<[>+<-]>[>[-][]+++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++.+++++++++++++..+++++++++++++.-----------------------------.
-.+++.+++++++++++++++++++++++++++.----------------------------.++++++++++++.----
-------.+.++++++++++++++++++++++++++.-------------------------------------------
----<[>+<-]>+++++.--------------------------------------.[-]<[-]]>]

}
