esolang

GolfScript

A stack language where almost every symbol means four different things, depending on what is on the stack. Built to win code-golf contests, and it did.

Darren Smith, 2007 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 6 known programs · Hello World on Try It Online: identical except the line end · Test method

Run it

Expected: 55

The idea

Darren Smith released GolfScript in December 2007. It is often called the first language made only for code golf: solving a task in as few characters as possible. Before it, golfers used Perl or Ruby. After it, a whole family of golfing languages followed (CJam, Pyth, Jelly, 05AB1E and more).

Read the story

The interpreter is a short Ruby program. A GolfScript program is a sequence of tokens: numbers, strings, arrays, blocks in { } and one-character operators. Values are pushed on a stack, operators pop and push. It is concatenative, like Forth and Joy.

The golfing trick is overloading by type. There are only four types (integer, array, string, block), and most operators do something sensible for each combination. * multiplies numbers, repeats an array, joins an array with a string, runs a block n times, or folds a block over an array. One character, five jobs.

Input and output are free. Before the program runs, all of standard input is pushed as one string. When it ends, the whole stack is printed. So a program that leaves its answer on the stack needs no print statement.

The machine

One stack. Variables exist (:name assigns the top without popping it), and a variable holding a block runs it when named.

OpIntegersArrays / stringsBlocks
+ -Add, subtractConcatenate, remove elementsConcatenate code
*MultiplyRepeat; or join with a stringRun n times; or fold over an array
/ %Divide, moduloSplit into pieces; every nth elementEach / map
~Bitwise notEvaluate a string; dump an array onto the stackRun it
,[0 1 … n−1]LengthFilter (with an array)
( )Decrement, incrementTake off the first / last element
. \ ; @ $Duplicate, swap, drop, rotate three, copy the nth
` | Turn any value into code that recreates it | |
! \| & ^Not, or, and, xorSet union, intersection, symmetric difference

There are also words such as if, do, while, zip, base, print and puts.

Example

The sum of 1 to n.

The sum of 1 to n. With input 10:

~),{+}*

Trace: the stack starts as the string "10". ~ evaluates it: 10. ) increments: 11. , makes the array [0 1 2 … 10]. {+} pushes a block, and * with an array and a block folds: 0 + 1 + … + 10. The stack holds 55, which is printed. Seven characters.

A quine, from the specification:

{".~"}.~

{".~"} pushes a block. . duplicates it, ~ runs one copy, which pushes the string .~. The stack now holds the block and the string, and printing a block prints its source with the braces. Output: {".~"}.~.

Both run on a port of the reference Ruby interpreter.

Run it

The sum from the example.

The sum from the example. The input 10 is the string on the stack when the program starts.

Expected: {".~"}.~

The quine prints itself.

Expected: 0 1 4 9 16

5, makes [0 1 2 3 4], {.*}% squares each one, and * with a string joins the array with a space.

Expected: one,two,three

/ with two strings splits the first at each space, and * joins the pieces again with a comma.

This implementation differs from the reference. #{...} inside a double-quoted string stays plain text (the reference hands it to Ruby as code), and a negative power stops the run instead of giving a fraction.

Patterns it teaches

  • Code golf: the language that made golfing languages a genre.
  • Concatenative: a program is a sequence of functions on a stack; gluing two programs together composes them.
  • Stack machines: implicit input on the stack, implicit output of the stack.
  • Quine: because printing is implicit and blocks print as source, quines are almost free. The block is both the code and the data.
  • Turing completeness: \.;+{}~ alone match a universal subset of Underload.

Try this

  1. Predict the output of 5, and of 5,{.*}%.
  2. Write a program that prints its input reversed. (Hint: -1%.)
  3. Hard: print the first 10 Fibonacci numbers, one per line, in under 20 characters.

Further

Golfing languages

Stack-based

Write your own in Run Convert a program from GolfScript

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: golfscript.

Command line

shib run golfscript -e '-1%' --input 'hello'
shib manual golfscript

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/golfscript.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org