The challenge
Interpret the stack-based programming language underload.
The stack is a stack of strings that can be concatenated together.
Each underload program is a string of some of these instructions:
~
: Swap the top two elements of the stack.:
: Duplicate the top element of the stack.!
: Discard the top element of the stack.*
: Concatenate the top two elements of the stack together.(x)
: Push the string between the parenthesis to the stack. Parentheses can nest within strings, so(a(bc)d)
should push the stringa(bc)d
to the stack, nota(bc
.a
: Enclose the top element of the stack in parenthesis.^
: Pop the top element of the stack, and append it the program, directly after this instruction.S
: Pop the top element of the stack and print it.
Test cases
(Hello, world!)S
Hello, world!
(!@#$%^&*())S
!@#$%^&*()
(a)(b)~SS
ab
(a)(b)!S
a
(a)(b)*S
ab
(a)aS
(a)
(b)(a)^S
(b)
(a(:^)*S):^
(a(:^)*S):^
Input
The input will be a string, either through STDIN, as an argument to a function, or however your programming language takes input.
You can assume the input will have matched parenthesis and will not attempt to run invalid instructions.
Output
The output will also be a string.
This is code-golf, so the shortest answer in bytes is wins!
(a(bc)d)
should pusha(bc)d
to the stack. \$\endgroup\$