Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
Whitespace (programming language)
Esoteric programming language

Whitespace is an esoteric programming language with syntax where only whitespace characters (space, tab and linefeed) have meaning – contrasting typical languages that largely ignore whitespace characters.

As a consequence of its syntax, Whitespace source code can be contained within the whitespace of code written in a language that ignores whitespace – making the text a polyglot.

Whitespace is an imperative, stack-based language. The programmer can push arbitrary-width integer values onto a stack and access a heap to store data.

An interpreter, along with its Haskell source code, is provided by the Whitespace creators.

Related Image Collections Add Image
We don't have any YouTube videos related to Whitespace (programming language) yet.
We don't have any PDF documents related to Whitespace (programming language) yet.
We don't have any Books related to Whitespace (programming language) yet.
We don't have any archived web articles related to Whitespace (programming language) yet.

History

Edwin Brady and Chris Morris, who also developed the Kaya and Idris languages, created Whitespace in 2002 at the University of Durham. Slashdot published a review on April Fool's Day 2003.4

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

Syntax

Whitespace defines a command as a sequences of whitespace characters. For example, [Tab][Space][Space][Space] performs arithmetic addition of the top two elements on the stack.

A command is written as an instruction modification parameter (IMP) followed by an operation and then any parameters. 6

IMP sequences include:

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

Operator sequences, organized by IMP, include:7

IMPOperatorParameterMeaningMinimum size of the stack required*
[Space][Space]NumberPush the number onto the stack0
[Space][LF][Space]-Duplicate the top item on the stack1
[Space][Tab][Space]Number nCopy the nth item on the stack (given by the argument) onto the top of the stack**n+1
[Space][LF][Tab]-Swap the top two items on the stack2
[Space][LF][LF]-Discard the top item on the stack1
[Space][Tab][LF]Number nSlide n items off the stack, keeping the top item**may be less than n+1,

but at least 1 item(top of the stack)is required

[Tab][Space][Space][Space]-Addition2
[Tab][Space][Space][Tab]-Subtraction2
[Tab][Space][Space][LF]-Multiplication2
[Tab][Space][Tab][Space]-Integer division2 (top may not be 0)
[Tab][Space][Tab][Tab]-Modulo2 (top may not be 0)
[Tab][Tab][Space]-Store in heap2
[Tab][Tab][Tab]-Retrieve from heap1
[LF][Space][Space]LabelMark a location in the program0
[LF][Space][Tab]LabelCall a subroutine0
[LF][Space][LF]LabelJump to a label0
[LF][Tab][Space]LabelJump to a label if the top of the stack is zero1
[LF][Tab][Tab]LabelJump to a label if the top of the stack is negative1
[LF][Tab][LF]-End a subroutine and transfer control back to the caller0 (callstack: 1)
[LF][LF][LF]-End the program0
[Tab][LF][Space][Space]-Output the character at the top of the stack1
[Tab][LF][Space][Tab]-Output the number at the top of the stack1
[Tab][LF][Tab][Space]-Read a character and place it in the location given by the top of the stack1
[Tab][LF][Tab][Tab]-Read a number and place it in the location given by the top of the stack1

*Due to Haskell being lazily evaluated, the effect of the stack being too small may only show when, for example, invoking the outputting a number side-effect.

**The copy and slide operations were added in Whitespace 0.3.8

Comments

Characters other than space, tab and linefeed are ignored and thus can be used for comments.

Numbers

A numeric value is represented as a sequence of space and tab characters that represent 0 and 1 respectively and terminated by a linefeed. The first character represents the sign of the value – space for positive and tab for negative. Subsequent characters before the terminator represent the binary digits of a value.

For example:

  • [space][tab][space][space][tab][space][tab][tab][linefeed] (STSSTSTTL) is positive since it starts with space and represents 1001011 in binary, which is 75 in decimal.
  • [tab][tab][tab][space][space][tab][space][linefeed] (TTTSSTSL) is negative since it starts with tab and represents -110010 in binary which is −50 in decimal.

Labels

A label is used for control flow. It is a linefeed-terminated sequence of spaceand tab characters. As there is only one namespace, all labels must be unique. 9

Sample code

The following source code is for a Whitespace "Hello, world!" program. For clarity, it is annotated with S, T and L before each space, tab, and linefeed.

S S S T S S T S S S L:Push_+1001000=72='H'_onto_the_stack T L S S :Output_'H';_S S S T T S S T S T L:Push_+1100101=101='e'_onto_the_stack T L S S :Output_'e';_S S S T T S T T S S L:+1101100=108='l' T L S S S S S T T S T T S S L:+1101100=108='l' T L S S S S S T T S T T T T L:+1101111=111='o' T L S S S S S T S T T S S L:+101100=44=',' T L S S S S S T S S S S S L:+100000=32=Space T L S S S S S T T T S T T T L:+1110111=119='w' T L S S S S S T T S T T T T L:+1101111=111='o' T L S S S S S T T T S S T S L:+1110010=114='r' T L S S S S S T T S T T S S L:+1101100=108='l' T L S S S S S T T S S T S S L=+1100100=100='d' T L S S S S S T S S S S T L:+100001=33='!' T L S S :Output_'!';_L L L:End_the_program

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.

References

  1. "Whitespace". Compsoc. Archived from the original on 18 June 2015. Retrieved 8 December 2015. https://web.archive.org/web/20150618184706/http://compsoc.dur.ac.uk/whitespace/tutorial.php

  2. Parker, Matt (2019). Humble Pi: a comedy of maths errors. Allen Lane. p. 21. ISBN 978-0-241-36023-1. 978-0-241-36023-1

  3. Parker, Matt (2019). Humble Pi: a comedy of maths errors. Allen Lane. p. 21. ISBN 978-0-241-36023-1. 978-0-241-36023-1

  4. Timothy (1 April 2003). "New Whitespace-Only Programming Language". Slashdot. Retrieved 23 January 2014. http://developers.slashdot.org/story/03/04/01/0332202/New-Whitespace-Only-Programming-Language

  5. Stroustrup, Bjarne. "Generalizing Overloading for C++2000" (PDF). Florham Park, NJ, USA: AT&T Labs. Retrieved 23 January 2014. http://www.stroustrup.com/whitespace98.pdf

  6. "Whitespace". Compsoc. Archived from the original on 18 June 2015. Retrieved 8 December 2015. https://web.archive.org/web/20150618184706/http://compsoc.dur.ac.uk/whitespace/tutorial.php

  7. "Whitespace". Compsoc. Archived from the original on 18 June 2015. Retrieved 8 December 2015. https://web.archive.org/web/20150618184706/http://compsoc.dur.ac.uk/whitespace/tutorial.php

  8. "Whitespace". Compsoc. Archived from the original on 18 June 2015. Retrieved 8 December 2015. https://web.archive.org/web/20150618184706/http://compsoc.dur.ac.uk/whitespace/tutorial.php

  9. "Whitespace". Compsoc. Archived from the original on 18 June 2015. Retrieved 8 December 2015. https://web.archive.org/web/20150618184706/http://compsoc.dur.ac.uk/whitespace/tutorial.php