esolang

Chaincode

One number goes in, every line changes it, and what the first line gives back is printed.

Roman Gräf, 2017 · Turing complete: no · ●○○ easy · esolangs.org

Tested with 3 known programs · Test method

Run it

Expected: 7

The idea

Roman Gräf made Chaincode in 2017. A program works on one number, the current value, which is read from the input before anything runs. Each command changes that number. Each line is a function, and the first line is main. A , calls the line below with the current value and takes what it gives back. At the end of a line the value is returned, and what main returns is printed.

Read the story

There is no loop, and a line can only call the one below it, so every program ends.

The machine

CommandDoes
+ -Add or take 1
a s * /Add, take, multiply or divide by the next input number
iRead the next input number
\Divide the next input number by the value
d hDouble or halve
^ vMultiply or divide by 10
p qSquare or square root
c _Round up or down
!Factorial
=1 if the value equals the next input number, else 0
l L nLogarithm to base 2, 10 or e
,Call the line below
> < |Copy into the switch, copy from it, swap with it
(...) #Comments

The switch is a second number that starts at 0. The specification gives each command as a line of Java, the language of its interpreter, and its = gives 1.0 or 0.0, so values are doubles. It does not say how the result is printed; this implementation writes it as JavaScript writes numbers, so 7 and not 7.0, with a line feed after it.

Example

The two examples, adding two numbers and a square root.

The two examples, adding two numbers and a square root.

Expected: 1.4142135623730951

The two lines. Main calls the line below, which adds the next input number to the value it was given, and main returns that sum.

Expected: 3.75

Main hands 4 to the line below, which squares it, and adds 1 to the 16 it gets back.

Expected: 17
Expected: 3628800

Patterns it teaches

  • Concatenative: commands side by side compose, each one a function of the same single number.

Further

Also in Functional and concatenative

Write your own in Run Convert a program from Chaincode

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run chaincode -e 'a' --input '1.5 2.25'
shib manual chaincode

Source

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

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

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