04 How is a Whitespace program structured?

Whitespace is a command-oriented stack-based programming language that provides programmers with a stack and heap.

All operations operate internally on integers of arbitrary bit length. However, there is an option to output a character identified by its ASCII code value.

The commands and control statements consist of defined sequences of spaces (s), tab characters (t), and linefeed (l).

There is no syntax element to identify comments. Instead, comments, which themselves must not contain whitespaces, can be entered anywhere in the source code. The interpreter ignores all characters that have no meaning for it.

Commands

Commands are composed of sequences of spaces, tab stops and linefeeds. For example, t-s-s-s performs arithmetic addition of the top two elements on the stack. Data is represented in binary using spaces (0) and tabs (1), followed by a linefeed; thus, s-s-s-t-s-t-t-l is the binary number 0001011, which is 11 in decimal. All other characters are ignored and thus can be used for comments.

Code is written as an Instruction Modification Parameter (IMP) followed by the operation and a parameter if needed.

IMPMeaning
sStack Manipulation
t sArithmetic
t tHeap Access
lFlow Control
t lInput/Output

Stack manipulation

IMPCommandParameterMeaning
ssNumberPush the number onto the stack
sl sDuplicate the top item on the stack
sl tSwap the top two items on the stack
sl lDiscard the top item on the stack

Arithmetic

IMPCommandParameterMeaning
t ss sAddition
t ss tSubtraction
t ss lMultiplication
t st sInteger Division
t st tModulo

Heap Access

IMPCommandParameterMeaning
t tsStore in heap
t tsRetrieve from heap

Flow Control

IMPCommandParameterMeaning
ls sLabelMark a location in the program
ls tLabelCall a subroutine
ls lLabelJump to a label
lt sLabelJump to a label if the top of the stack is zero
lt tLabelJump to a label if the top of the stack is negative
lt tEnd a subroutine and transfer control back to the caller
ll lEnd the program

Input/Output

IMPCommandParameterMeaning
t ls sOutput the character at the top of the stack
t ls tOutput the number at the top of the stack
t lt sRead a character and place it in the location given by the top of the stack
t lt tRead a number and place it in the location given by the top of the stack

Data

While the commands use a three-valued place system, the data is mapped in the dual system: In the first place from the left, a “t” represents a negative number; a “l” represents a positive number. Further on, “t” stands for 1 and “l” for 0. The specification states that numbers can have any bit length, but compilers and interpreters can set a reasonable upper limit here. A datum is terminated by a line break.