Serenity
Everything is an object, and nothing is forbidden. You can call an integer, or change the constant 12345 into 12347.
Hakerh400, 2020 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 10 known programs · Test method
Run it
The idea
Hakerh400 made Serenity in 2020. Every value is an object with a prototype and keys, and the keys are objects too. Integers, characters, symbols, strings, functions, scopes and even the stack frames are such objects. There are no runtime errors, because every operation works on every object. Multiplying two strings uses their integer values, which are 0.
Read the story
A program is one object whose key insts holds an array, and it is called as a function. Each element of that array either is an instruction symbol, which runs, or is pushed on the function's own stack. The specification says its interpreter is the full specification.
The machine
| Instructions | Do |
|---|---|
add sub mul div mod lt eq ... | Arithmetic on integer values; eq compares identity |
dupe copy move swap pop disc | Move items, counted from the top |
get set has delete, getl setl ... | Keys, through the prototype chain or only the object's own |
getv setv enter leave | Variables are keys of the current scope |
arr str char pusha popa clone obj | Make and change objects |
bind args call ret crg cbs | Functions, closures and calls |
jmp jz jnz | Jump to an index; name: and :name in an array are labels |
in out | The whole input as a string; print a string and stop |
prod* | Replace every reference to a key of the given object with its value |
out is the only way to stop. A function that runs off its end returns, and when the main function does, nothing is left to run and the program never halts.
Example
The digits keep a string in s and a counter in i.
The digits keep a string in s and a counter in i. loop: and :loop become the index of the element after the label.
The reverse pops the input's last character and pushes it onto a new string until the input is empty.
prod* rewrites every object at once. It turns the constant 12345 into 12347, so the program prints the last digit of 12347.
The same trick renames an instruction. After prod* the symbol print in the body is the instruction out.
The specification defines the dictionary product on another page. This implementation keeps the first object's keys and replaces each value that is a key of the second with its value there. With that reading the interpreter test prints PQcdefgRQ8, as the specification says it should, and so do its other prod* examples. The program that adds two integers also works.
Patterns it teaches
- Stack machines: every function works on a stack of its own.
- Self-modifying code: a function body is an array the program can change, and
prod*rewrites the whole program at once.
Further
See also
Also in Functional and concatenative
Write your own in Run Convert a program from Serenity
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: serenity.
Command line
shib run serenity -e '{insts: [
in out
]}' --input 'cat'shib manual serenitySource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/serenity.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org