esolang

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

Expected: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393

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

WordDoes
25 -14Push a number
: name ... ;Define a word
*name =nameDeclare a variable; pop into it
@namePop, and run name if that was not 0
[namePop, and run name until a 0 is popped
$namePop n, and run name n times
dup swap pop sizeStack work
+ - * / mod < >Arithmetic and comparison
. ..Print a number and a line break; print a character
alloc free get putRaw 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.

Expected: Hello, world!
Expected: 3 2 1
Expected: 1

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

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 maentwrog

Source

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