esolang

Macmac

Every command is a function call. There are no loops, only macros that call themselves.

David.werecat, 2012 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 3 known programs · Test method

Run it

Expected: !

The idea

David.werecat made Macmac in 2012. A line is one value, and a value is a number, null or a function call whose parameters are values too. The memory is two stacks and one register. To repeat anything you define a macro, <name>{value}, and put a use of it, [name], inside its own body. An if runs only the branch it picks, and that is what stops the repeating.

Read the story

A macro can be defined anywhere, even as the parameter of a function, so running one branch of an if can decide what a macro means.

The machine

FunctionDoes
store(x) recall()Set and read the register
push1(x) pop1() peek1() size1()Stack 1, and the same with 2 for stack 2
add sub mul div modInteger arithmetic on two values
and or xor notBitwise
get() put(x)Read a byte, print a character
ifsame ifdiff ifless ifmoreCompare two values, then give the third, or the fourth, or 0
exec(...)Run all its parameters in order
<name>{value} [name]Define a macro; run it

Spaces and tabs are ignored, and a line that starts with >> is a comment. Values are 32-bit integers.

Example

The conditional macro.

The conditional macro. Running con defines mac in the branch that ifsame picks, so mac prints !.

A countdown. The macro prints the register, takes 1 off and runs itself again while the register is above 0.

Expected: 54321

The Fibonacci program prints 1, 1, 2 and on up to 987, one number a line. Three macros count the hundreds, tens and ones of a number on stack 2, one call per unit, and a fourth prints the digits.

Patterns it teaches

  • Stack machines: two stacks and a register, reached only through functions.

Further

Functional paradigm

Stack-based

Write your own in Run Convert a program from Macmac

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run macmac -e '<con>{ifsame(0,0,<mac>{put(33)},<mac>{null})}
[con]
[mac]'
shib manual macmac

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/macmac.js

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

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