esolang

Element

Words push themselves, symbols do the work, and logic lives on its own second stack.

PhiNotPi, 2012 · Turing complete: unknown · ●●○ moderate · esolangs.org

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

Run it

Expected: Element

The idea

PhiNotPi, a code-golf regular, made Element in 2012 and called it "very compact and human-readable". Every command is one character. Anything that is not a command is a bare word, and a bare word simply pushes itself as a string. So Hello is a complete push, with no quotes.

Read the story

The trick is the split between two stacks. The main stack holds data: numbers and strings, which are the same thing, as in Perl (the reference interpreter is in Perl). The control stack holds truth values and loop counts. Tests like < or ? read the main stack and push their answer onto the control stack. Loops look at the control stack and never pop it.

The machine

A main stack of scalars, a control stack, and a hash of variables. An empty stack pops as the empty string, which counts as 0.

CharDoes
word \cPush a bare word; \ escapes one character into it; spaces separate words
_ ` | Read a line; pop and print
; ~value name ; stores a variable; ~ replaces a name with its value
? < > =Test the main stack, push 1 or 0 onto the control stack
' "Move the top from main to control, or back
& \| !And, or, not on the control stack
[ … ]Run the body as many times as the control top says
{ … }Run the body while the control top is true
+ * / % ^ -Arithmetic; - negates one number
# : @Drop; x n : pushes n copies of x; x y @ moves item x (0 is the top) to place y
( ) $ . ,Split off the first or last character, length, concatenate, character and code

Example

The puzzle: print "Element" without letters.

The puzzle: print "Element" without letters. It pushes the codes 116 and 110, a loop makes 109 and 108 from them and slips a 101 under each, then 69 goes on top. The last loop turns each code into a character with ,, drops the leftover number with # and prints with ` .

Patterns it teaches

  • Stack machines: two stacks with a clean division of labour, data and control.
  • Code golf: bare words make strings free, which is where golf programs spend most bytes.
  • Quine: the quine is a string followed by the code that prints it escaped, then plain.

Try this

  1. Read a number and print its square.
  2. Print the numbers 1 to 5. You need a counter on the main stack and a count on the control stack.
  3. Hard: read two lines and print the longer one.

Further

Golfing languages

Stack-based

Write your own in Run Convert a program from Element

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run element -e '1_'\''['\''1+2:"*]`' --input '5'
shib manual element

Source

Module
langs/stack/ports.js
Interpreter
langs/stack/element.js

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

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