Whitespace is an esoteric programming language (esolang) created in 2003 by Edwin Brady and Chris Morris. Many elements of Whitespace are familiar, including the fact that it is imperative and stack-based, but it has one main distinguishing feature: the only significant characters in a Whitespace source code file are [Tab] (ASCII $9$), [Newline] (ASCII $10$), and [Space] (ASCII $32$). This, of course, has the intentionally humorous result that all source code files using only these characters appear completely blank in most text editors, which can be problematic if you have a boss who keeps looking over your shoulder for evidence of productivity. (A Whitespace source code file can, in fact, include other characters, but these are simply ignored, so we will not consider that possibility here.)
For this problem, you will work with a simplified version of Whitespace that we call Whitespace Lite, affectionately known as Litespace. Let $W$ denote the $3$-character set $\{ $[Tab], [Newline], [Space]$\} $. A Litespace program is a sequence of instructions, each of which consists of two or more characters from $W$. There is a stack available that holds signed integers; when a Litespace program begins, the stack is initially empty. In the list of instructions, given below, only one instruction (the last) is specifically intended to produce output, but most other instructions produce output if an error condition is encountered.
Litespace Instruction Set:
[Space][Space]<integer> — Push the specified integer onto the stack.
[Space][Newline][Space] — Make a copy of the topmost stack element and push this copy onto the stack. If the stack is empty, print “Invalid copy operation” on a line by itself.
[Space][Newline][Tab] – Swap the two topmost stack elements. If the stack contains fewer than two elements, print “Invalid swap operation” on a line by itself and leave the stack unchanged.
[Space][Newline][Newline] – Remove and discard the topmost stack element. If the stack is empty, print “Invalid remove operation” on a line by itself.
[Tab][Space][Space][Space] — Remove the two topmost stack elements and push their sum onto the stack. If the stack contains fewer than two elements, print “Invalid addition operation” on a line by itself and leave the stack unchanged.
[Tab][Space][Space][Tab] — Remove the two topmost stack elements and push their difference onto the stack (the topmost element subtracted from the second topmost element). If the stack contains fewer than two elements, print “Invalid subtraction operation” on a line by itself and leave the stack unchanged.
[Tab][Space][Space][Newline] — Remove the two topmost stack elements and push their product onto the stack. If the stack contains fewer than two elements, print “Invalid multiplication operation” on a line by itself and leave the stack unchanged.
[Tab][Space][Tab][Space] — Remove the two topmost stack elements and push their quotient onto the stack (the second topmost element divided by the topmost element); this is integer division, so discard any fractional part of the quotient. If the stack contains fewer than two elements, print “Invalid division operation” on a line by itself and leave the stack unchanged. If the stack contains two or more elements, the topmost of which is $0$, print “Division by zero” on a line by itself and leave the stack unchanged.
[Tab][Newline][Space][Tab] — Remove the integer on the top of the stack and print it on a line by itself (in the usual base-$10$ representation). If the stack is empty, print “Invalid print operation” on a line by itself.
In the first instruction above, an integer to be pushed onto the stack is in binary form, encoded as two or more characters from the set $\{ $[Space], [Tab]$\} $ followed by a [Newline] character. The first character encodes the sign of the integer ([Space] $=$ positive, [Tab] $=$ negative), and the following [Space] and [Tab] characters encode the bits in the binary representation of the absolute value (magnitude) of the integer, with the convention that [Space] $= 0$, [Tab] $= 1$. The first bit after the sign bit is most significant, and there are no leading zeros. For example, since $13$ in binary is $1101$, the encoding of $-13$ is
Note that there are two encodings of $0$, one for $+0$ and one for $-0$. These are interchangeable. However, in the last instruction above, if the value to be printed is zero, print “0”, not “-0”.
Given a Litespace program, produce the program’s output. For compactness, the program will be given as a string of characters from the set $\{ $‘T’, ‘N’, ‘S’$\} $, where ‘T’ denotes [Tab], ‘N’ denotes [Newline], and ‘S’ denotes [Space].
Input consists of a single non-empty string of characters from $\{ $‘T’, ‘N’, ‘S’$\} $ representing a valid sequence of Litespace instructions. The length of the string is at most $10\, 000$. It is guaranteed that the Litespace program produces at least one line of output. Also, any integer pushed onto the stack or generated by a successful arithmetic operation will be less than $2^{31}$ in absolute value.
Print the output of the Litespace program.
Sample Input 1 | Sample Output 1 |
---|---|
SSSTTSNTNST |
6 |
Sample Input 2 | Sample Output 2 |
---|---|
SSSTTNTSSSSSTTNSSSTTSTNTSSSTNSTTNST |
Invalid addition operation 12 3 |