This challenge was posted as part of the April 2018 LotM challenge
Brain-Flak is a turing-tarpit language which has gained quite a lot of fame here on PPCG. The memory of the language is composed by two stacks, but a "hidden" third stack was discovered by Wheat Wizard, leading to some interesting new ways of thinking Brain-Flak programs.
So, what about giving that poor hidden third stack more visibility? Let's create a language where the third stack has the recognition it deserves! Here I present you Third-Flak.
The language
In Third-Flak there is only one stack, called the third stack. Operators work on the third stack in the same way they do in Brain-Flak, but here there are no []
,{}
,<>
nilads and no {...}
monad (so the only admissible characters in a Third-Flak program are ()[]<>
). Here is what each operator does (examples will be given representing the third stack with a list where the last element is the top of the stack):
()
is the only two-characters operator in Third-Flak. It increases the top of the third stack by 1. Example:[1,2,3]
→[1,2,4]
(
,[
,<
: all opening parentheses that are not covered by the previous case push a0
to the third stack. Example:[1,2,3]
→[1,2,3,0]
)
pops two elements from the third stack and pushes back their sum. Example:[1,2,3]
→[1,5]
]
pops two elements from the third stack and pushes back the result of subtracting the first from the second. Example:[1,2,3]
→[1,-1]
>
pops an element from the third stack. Example[1,2,3]
→[1,2]
And here are the other rules of the language:
At the beginning of execution the third stack contains only a single 0.
It is forbidden to have empty
[]
or<>
inside a program (they would be noops anyway if following the semantics of Third-Flak, but they actually have a different meaning in Brain-Flak that is not possible to recreate here).Parentheses always need to be balanced, except for the fact that trailing closing parentheses at the end of the program can be missing. As an example,
[()<(()
is a valid Third-Flak program (and the third stack at the end of the program would be[1,0,1]
).A program can only contain the six allowed characters
()[]<>
. Programs are guaranteed to be non-empty.
Note: it is implied by the previous rules that you won't have to deal with situations where you need to pop from an empty stack.
The challenge
Simple, write an interpreter for Third-Flak. Your program must take as input a Third-Flak program and return as output the state of the third stack at the end of the program.
Your output format is flexible as long as it is possible to unambiguously read from it the state of the third stack and the same number is always encoded in the same way (This is just a way of saying that any output format that's not a blatant way to try to cheat is fine).
Your output choice may restrict the range of numbers you can manage as long as this does not trivialize the challenge (since this would be a default loophole).
Test cases
For each test case the first line is the input, and the second line the output stack represented as a space separated list of numbers where the top of the stack is the last element.
[()<(()
0 1 0 1
[((((()()()()()))
0 0 0 5
((([()][()][()])))
-3
[<<(((()()()())(((((
0 0 0 0 0 4 0 0 0 0 0
[()]<(([()])><[()]
-1 0 -1
(())(()()())(())(())(()()())(())(())(()()())(())(()()()()())(()())(()()()()())(())(()()())(())(()()()()())(())(())(())(())(()()()()())(())(())(()()())(())(())(()()())(())(()()()()())(()())(()()()()())(())(()()())(())(()()()()())(())(())(())(())(())(())(()())(())(())(())(()())(()()()())(())(()()()()())(()())(()())(()())(()()())(())(()())(())(()()()()())(()())(()()()()())(())(())(())(())(())(()())(())(())(())(()()()()())(())(())(()()()()())(())(())(()()()()())(())(())(()())(())(()())(())(()())(())(()())(())(()())(()())(()())(()())(()())(()())(()())(()())(())(()()()()())(())(())(()()())(())(())(()()())(())(()()()()())(()())(()()()()())(())(()()())(())(())(()()())(()())(())(()()()()())(())(())(()()())(())(())(()()())(())(()()()()())(()())(()()()()())(())(())(())(()()())(())(())(()()())(())(())(()()())(())(()()()()())(()())(()()()()())(()()())(())(()()())(())(())(())(()())(()()())(()())(())(()()()()())(()())(())(()()())(())(()()()()())(())(())(())(()()())(())(())(())(()())(())(()())(()()()())(())(())(()()()()())(()())(()())(())(()()())(())(())(())(())(()()())(()())(())(())(()()()()())(())(())(()()()()())(())(())(()()()()())(())(())(()())(())(()())(())(()())(())(()())(()())(()())(()())(())(()()()()())(()())(())(()()())(())(()()()()())(()()()()())(())(()()())(())(())(()())(())(()()()()())(())(()()()()())(())(())(())(()()()()())(())(())(()())(())(()())(())(()())(())(()())(())(()())(()())(()())(()())(())(()()()()())(()())(())(()()())(())(())(()())(())(()()()()())(()())(()()()()())(())(()()())(())(())(()()()()())(())(()()()()())(())(())(())(()())(())(()()()()())(())(())(()())(())(()())(())(()())(()())(()())(()())(())(()()()()())(()())(())(()()()()())(())(()()())(())(())(()())(())(()()()()())(()())(()()()()())(())(()()())(())(())(())(()())(()()()())(())(())(()())(())(()()()()())(())(())(()()()()())(())(())(()()()()())(())(())(()())(())(()())(())(()())(())(()())(())(()())(()())(()())(()())(()())(()())(())(()()())(())(())(()())(())(()()()()())(()())(()()()()())(()()())(())(())(()())(())(())(()()()()())(()()()())(()())(()())(()()())(())(()())(())(()()()()())(()())(()()()()())(())(())(())(()()()())(()()()())(()()
718 2
[()]
breaks the rule that we don't need to worry about popping from an empty stack \$\endgroup\$e
is here. \$\endgroup\$