Jump to content

Whitespace (programming language)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 45.62.76.35 (talk) at 08:43, 9 December 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Whitespace hello world program with syntax highlighting
  tabs
  spaces


The language itself is an imperative stack-based language. The virtual machine on which the programs run has a stack and a heap. The programmer is free to push arbitrary-width integers onto the stack (currently there is no implementation of floating point numbers) and can also access the heap as a permanent store for variables and data structures.

History

Whitespace was created by Edwin Brady and Chris Morris in 2002. Slashdot gave a review of this programming language on 1 April 2003.[1]

The idea of using whitespace characters as operators for the C++ language had been facetiously suggested five years earlier by Bjarne Stroustrup.[2]

Syntax

Commands are composed of sequences of spaces, tab stops and linefeeds. For example, tab-space-space-space 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, tab-space-space-tab-space-tab-tab-linefeed is the binary number 1001011, which is 75 in decimal. All other characters are ignored and thus can be used for comments.

The sign of a number is given by its first character, [Space] for positive and [Tab] for negative.[3]

Code is written as an Instruction Modification Parameter (IMP) followed by the operation.[3] The table below shows a list of all the IMPs in Whitespace.

IMP Meaning
[Space] Stack Manipulation
[Tab][Space] Arithmetic
[Tab][Tab] Heap Access
[LineFeed] Flow Control
[Tab][LineFeed] I/O

Each IMP is followed by one operation defined for that IMP, and a parameter if needed. The list of operations supported in Whitespace is:[3]

IMP Command Parameter Meaning
[Space] [Space] Number Push the number onto the stack
[Space] [LF][Space] - Duplicate the top item on the stack
[Space] [Tab][Space] Number Copy the nth item on the stack (given by the argument) onto the top of the stack
[Space] [LF][Tab] - Swap the top two items on the stack
[Space] [LF][LF] - Discard the top item on the stack
[Space] [Tab][LF] Number Slide n items off the stack, keeping the top item
[Tab][Space] [Space][Space] - Addition
[Tab][Space] [Space][Tab] - Subtraction
[Tab][Space] [Space][LF] - Multiplication
[Tab][Space] [Tab][Space] - Integer Division
[Tab][Space] [Tab][Tab] - Modulo
[Tab][Tab] [Space] - Store in heap
[Tab][Tab] [Tab] - Retrieve from heap
[LF] [Space][Space] Label Mark a location in the program
[LF] [Space][Tab] Label Call a subroutine
[LF] [Space][LF] Label Jump to a label
[LF] [Tab][Space] Label Jump to a label if the top of the stack is zero
[LF] [Tab][Tab] Label Jump to a label if the top of the stack is negative
[LF] [Tab][LF] - End a subroutine and transfer control back to the caller
[LF] [LF][LF] - End the program
[Tab][LF] [Space][Space] - Output the character at the top of the stack
[Tab][LF] [Space][Tab] - Output the number at the top of the stack
[Tab][LF] [Tab][Space] - Read a character and place it in the location given by the top of the stack
[Tab][LF] [Tab][Tab] - Read a number and place it in the location given by the top of the stack

The "copy" and "slide" operations were added in Whitespace 0.3 and may not be supported by all implementations.[3]

Sample code

The following is a commented Whitespace program that simply prints "Hello, world!", where each Space, Tab, or Linefeed character is preceded by the identifying comment "S", "T", or "L", respectively:

S S S T	S S T	S S S L
T	L
S S S S S T	T	S S T	S T	L
T	L
S S S S S T	T	S T	T	S S L
T	L
S S S S S T	T	S T	T	S S L
T	L
S S S S S T	T	S T	T	T	T	L
T	L
S S S S S T	S T	T	S S L
T	L
S S S S S T	S S S S S L
T	L
S S S S S T	T	T	S T	T	T	L
T	L
S S S S S T	T	S T	T	T	T	L
T	L
S S S S S T	T	T	S S T	S L
T	L
S S S S S T	T	S T	T	S S L
T	L
S S S S S T	T	S S T	S S L
T	L
S S S S S T	S S S S T	L
T	L
S S L
L
L

Note that when Whitespace source code is displayed in some browsers, the horizontal spacing produced by a tab character is not fixed, but depends on its location in the text relative to the next horizontal tab stop. Depending on the software, tab characters may also get replaced by the corresponding variable number of space characters.

See also

References

  1. ^ Timothy (1 April 2003). "New Whitespace-Only Programming Language". Slashdot. Retrieved 23 January 2014.
  2. ^ Stroustrup, Bjarne. "Generalizing Overloading for C++2000" (PDF). Florham Park, NJ, USA: AT&T Labs. Retrieved 23 January 2014.
  3. ^ a b c d Cite error: The named reference whitespace was invoked but never defined (see the help page).