esolang

Bubblegum

A language made to chew bubblegum in code golf. A program is the output, compressed.

Dennis, 2015 · Turing complete: unknown · ●●○ moderate · esolangs.org

Tested with 5 known programs · Hello World on Try It Online: identical · Test method

Run it

Expected: Hello, World!

The idea

Dennis, a user of the Programming Puzzles and Code Golf site, made Bubblegum in 2015 for one kind of task: print a fixed text in as few bytes as possible. It does not have commands. It guesses what the source is and decodes it.

Read the story

The reference interpreter is about 30 lines of Python. It tries four readings in order and takes the first one that works. Most real Bubblegum programs are compressed streams written by a tool, not by hand.

The machine

TryIf the source isThen
1One exact Python program, checked by its SHA-256Run it on a line of input
2A raw DEFLATE streamPrint what it inflates to
3A raw LZMA2 streamPrint what it decompresses to
4Anything elseRead the bytes as one number and write it in bijective base 96

In the last case the digits 1 to 95 are the printable characters from space to ~, and 96 is a linefeed. So every byte string is a valid program and prints something.

The one Python program is a joke about what counts as a real language. Given a prime p it prints p − 1, for other single numbers it prints 0, and for several numbers it prints their sum. That is enough to test primes and add numbers, the two tasks the site once used as the bar.

Example

TIO keeps programs that are not text as an xxd dump.

TIO keeps programs that are not text as an xxd dump. This one is 11 bytes of DEFLATE:

A plain word is not a compressed stream, so it falls through to base 96:

Expected: :=XTPJ8zlx,

The Python program, run on 7:

Expected: 6

It uses Wilson's theorem: (p − 1)! leaves remainder p − 1 when divided by p exactly when p is prime.

This implementation decodes DEFLATE with Node's zlib and has its own LZMA2 decoder. It was compared with the reference on 3,610 byte strings, including broken and truncated streams, and printed the same bytes for all of them.

Patterns it teaches

  • Code golf: for a fixed text, a general compressor often beats any hand-written program.
  • Encoding tricks: the base-96 fallback means there is no syntax error. Every source decodes to some text.

Try this

  1. Run shib run bubblegum -e 'a' and work out by hand why it prints what it does.
  2. Find a one-byte program that prints a linefeed.
  3. Hard: compress a poem with DEFLATE and with LZMA2 and see which program is shorter.

Further

Also in Golfing

Write your own in Run Convert a program from Bubblegum

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run bubblegum -e 'bubblegum'
shib manual bubblegum

Source

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

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

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