esolang

Purple

Every instruction is three letters and does one subtraction. The program lives in the memory it changes.

Quintopia, 2015 · Turing complete: yes · ●●● hard · esolangs.org

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

Run it

Expected: J

The idea

Quintopia made Purple in 2015 as a smaller version of Aubergine, an earlier language of theirs. Aubergine had a conditional jump; Quintopia noticed it was not needed and folded what was left into one instruction with three arguments. So Purple has no command letters at all. Every three characters are an instruction, and nothing marks where one ends and the next begins.

The machine

The source text is loaded into an endless memory of integers, one character code per cell from address

  1. There are three registers, a, b and the instruction pointer i, all starting at 0.

An instruction xyz works out y - z and sends it to x. The letters mean:

LetterAs y or zAs x
a b iThe registerSet the register
A BThe memory cell that a or b points toWrite that cell
1The number 1End the program
oOne byte of inputPrint the value as a byte

After each instruction i moves on by 3. Since i is a register, writing it is a jump. The program ends as soon as one of the three cells at i holds any other character, so the text after the code is free for data. Values are unbounded; printing a value outside 0 to 255 prints nothing.

Example

The smallest program reads two bytes and prints their difference.

The smallest program reads two bytes and prints their difference. k is 107 and ! is 33, so it prints 74, a J.

Expected: cat

A cat program. bbb sets b to b - b, which is 0. oob prints an input byte minus b. iii sets i to 0, and the step of 3 that follows lands on oob again. It ends when the input runs out.

Expected: Hello, World!

The hello world reads its own second line backwards. Cell 0 starts as A, 65, and becomes a counter: each turn AA1 takes 2 off it and bA1 points b one cell below it, so b walks down the text two cells at a time and skips the spaces. b1B and o1b print the character there. bib, b1B and i1b set i back to 0 for the next turn, but the character itself is part of that sum. At the newline the jump lands in the text instead, and the program ends.

Patterns it teaches

  • Self-modifying code: there is no conditional jump, so loops end by changing the code until an instruction stops being valid.
  • Code as data: the letters of the program are numbers the program subtracts with.
  • Turing tarpit: one instruction, seven letters, and getting a 0 or a 1 takes planning.

Try this

  1. Print the sum of two input bytes, not the difference.
  2. Print each input byte twice.
  3. Hard: the truth machine, Aoab11bi1bABoAaiba. Work out why input 0 prints once and 1 forever.

Further

Self-modifying

Cell-based

Write your own in Run Convert a program from Purple

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run purple -e 'ooo' --input 'k!'
shib manual purple

Source

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

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

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