Clem
A stack of functions. Numbers, commands and lists of them all wait on the stack, and the one way to run code is a while loop.
Orby, 2014 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Orby made Clem in 2014. It is a stack language with first-class functions. A number pushes itself, and commands in parentheses push themselves as a function. Everything on the stack is such a function, and a number is a function of one element.
Read the story
Functions are joined with . and split with /, so a program can build code on the stack before it runs it. There is no if and no call. The only command that runs a function is w, which runs it again and again while the top of the stack is a number other than 0.
The machine
| Command | Does |
|---|---|
-10 +11 5 | Push a constant |
"Hi!" | Push each character, the last one first |
(...) | Push the commands inside as one function |
@ | Rotate the top three |
# $ % | Duplicate, swap, drop |
. | Join the top onto the end of the one below |
/ | Split the first element off a function |
+ - | Add or take 1 from a constant |
< | Read a character, -1 at the end of the input |
> c | Print a constant as a character or as a number |
w | Pop a function and run it while the top is a constant other than 0 |
q h | Quit; print the command list |
The specification leaves a few things open. In this implementation @ brings the third function to the top, c prints the number with nothing after it, and constants are 32-bit integers.
Example
One example prints with a string and a loop.
One example prints with a string and a loop. (>)w prints characters until it meets the 0 pushed first, and the 10 is the line feed.
The tutorial adds two numbers. It joins (-) and ($+$) into (-$+$), which takes 1 from the top and gives it to the number below, and w runs it until the top is 0. On -10 and 11 that leaves 1, and on 1 and 10 it leaves 11. The c at the end prints it.
A cat. It adds 1 to every character it reads, so that the -1 at the end of the input becomes the 0 that stops the loop.
The quine on the specification is 57 characters. It keeps its second half as a string inside a function and joins two copies of it, two quote marks, a ( and a line feed into one function. Run once, that function prints a (, a quote mark, the half, a quote mark, the half again and the line feed with (>)w. It also prints the 1 left under it as byte 1 at the very end, which a terminal does not show.
Patterns it teaches
- Stack machines: every value, code included, lives on one stack.
- Code as data: functions are joined and split like any other value.
- Quine: a string of the program's second half, printed twice.
Further
See also
Functional paradigm
Stack-based
Write your own in Run Convert a program from Clem
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: clem.
Command line
shib run clem -e '0 10 "Hi!"
(>)w'shib manual clemSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/clem.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org