Maentwrog
The first language Chris Pressey designed, a small Forth with pointers, and the one before Befunge.
Chris Pressey, 1993 · Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Chris Pressey made Maentwrog in August 1993. It came with an interpreter in C and no documentation, so its esolangs.org page was written from that source and later became the official description.
Read the story
Like Forth, a program is words separated by whitespace, and each word runs as soon as it is read. Numbers push themselves, : name ... ; defines a new word, and a word can recurse. There is no if and no loop word. Instead a word can carry a prefix. @fib pops a value and runs fib if it was not 0, so a word that ends by calling itself with @ is a loop.
The machine
| Word | Does |
|---|---|
25 -14 | Push a number |
: name ... ; | Define a word |
*name =name | Declare a variable; pop into it |
@name | Pop, and run name if that was not 0 |
[name | Pop, and run name until a 0 is popped |
$name | Pop n, and run name n times |
dup swap pop size | Stack work |
+ - * / mod < > | Arithmetic and comparison |
. .. | Print a number and a line break; print a character |
alloc free get put | Raw memory, as C pointers |
rnd bye rem ... ; | Random number; stop; comment |
The specification notes that == should test equality, but in the official interpreter it only pops, and so it does in this implementation. The three debugging words debug, vars and words are left out, because only the C source says what they print.
Example
The Fibonacci program keeps two numbers in variables and calls itself with @fib while the newest is below…
The Fibonacci program keeps two numbers in variables and calls itself with @fib while the newest is below 100000.
The specification's own Hello World runs dup .. @puts, which prints the final 0 as a character before the loop sees it, so its output ends in a zero byte. The version above prints first and tests after.
The specification also has a prime number generator that keeps its primes in memory from alloc and walks it 8 bytes at a time. It prints the first 25 primes.
Patterns it teaches
- Stack machines: Forth's model, with prefixes in place of control words.
Further
See also
Stack-based
Write your own in Run Convert a program from Maentwrog
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: maentwrog.
Command line
shib run maentwrog -e '*a *b *c
0 =a 1 =b
: fib a b + =c c . b =a c =b c 100000 < @fib ;
1 . fib'shib manual maentwrogSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/maentwrog.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org