esolang

Jug

Arithmetic as the water jug puzzle. Numbers live only as water, and water can only be poured.

Marz, 2007 · Turing complete: unknown · ●○○ easy · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: Hello, world!

The idea

Marz made Jug in 2007, after the language Liquid and the water jug puzzle from Die Hard. A number is the volume of water in a jug. There is no add or subtract. You fill a jug, empty it, or pour it into another until the first is empty or the second is full, and the arithmetic falls out of the sizes.

Read the story

Strings exist only to be printed. The specification says they cannot be stored in jugs, as they are not liquid.

The machine

FunctionDoes
jug(x,y)Make jug x of size y, empty
fill(x) empty(x)Fill or empty jug x
pour(x,y)Pour x into y until x is empty or y is full; gives the amount
volume(x)Give what is in jug x
echo(x)Print x
if_empty(x){...} else{...}Run one block or the other
drain(x){...}Run the block until jug x is empty

A call can be the value of another call, and & joins values into a string, as in 99&" bottles".

Example

The second is the 7 minus 2.
Expected: 5

The second is the 7 minus 2. A full jug of 7 pours into a jug of 2, and 5 stays behind.

This program multiplies 4 by 6 with a loop. Each round fills the 6 jug and pours it into a big one, then pours one unit of the 4 jug into a jug of size 1 and throws it away. After four rounds the 4 jug is empty.

Expected: 24

drain works out its jug once and checks it before every round. The Fibonacci pours jug 3 away before it copies it, so it prints 0, 1 and then 1 twenty times.

Patterns it teaches

  • Counter machines: jugs are counters with a ceiling, and a loop runs until one is empty.

Further

Also in Turing tarpits

Write your own in Run Convert a program from Jug

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run jug -e 'echo("Hello, world!");'
shib manual jug

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/jug.js

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

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