.bes.is

A "concatenative language"

Predefined operations
dup

Duplicates the top value on the stack

[1, 2, 3] dup
[1, 1, 2, 3]
drop

Drops the top value on the stack

[1, 2, 3] drop
[2, 3]
swap

Swaps the top two values on the stack

[1, 2, 3] swap
[2, 1, 3]
if ... then

Drops the top value on the stack and tests it. If it is false, or 0, the program skips ahead past the matching 'then', otherwise the words in between are run. The 'then' itself is removed either way: it only marks the end of the branch, it never runs.

[1, 2, 3] if dup then drop
[2, 3] dup drop
[2, 2, 3] drop
[2, 3]

Or

[0, 2, 3] if dup then drop
[2, 3] drop
[3]
= <> < > <= >=

Compares the top two values and leaves a flag in their place, true or false, which 'if' can take straight off the stack. They read in the order they are written, so '2 3 <' asks whether 2 is below 3.

[] 2 3 < if 10 then
[2] 3 < if 10 then
[3, 2] < if 10 then
[true] if 10 then
[] 10
[10] <==
true false

Puts a flag on the stack, the same one a comparison leaves behind.

[3] true
[true, 3]
and

True only if both flags on top of the stack are true.

[true, false] and
[false]
or

True if either of the two flags on top of the stack is true.

[true, false] or
[true]
invert

Turns the flag on top of the stack into its opposite.

[false] invert
[true]
: word ... ;

Creates a new definition.

Example: ": double 2 * ;" defines a new word called double. Any subsequent mention of double will replace the word with its definition.

[] : double 2 * ; 3 double
[] 3 double
[3] double
[3] 2 *
[2, 3] *
[6] <==
,,

Unquotes the string on top of the stack: its words are put back in front of the program and run as if they had been written there.

[5] " 2 * " ,,
[ 2 * , 5] ,,
[5] 2 *
[2, 5] *
[10] <==
Example programs
Fahrenheit to Celsius Replace program
                : f>c   32 - 5 * 9 / ;
                212 f>c
            
Countdown Replace program

A word that calls itself is the only loop there is, and 'if' is what stops it.

                : countdown   dup if 1 - countdown then ;
                5 countdown
            
Factorial Replace program
                : !   dup 1 > if dup 1 - ! * then ;
                7 !
            
Fibonacci Replace program

Every number in the sequence 0, 1, 1, 2, 3, 5, 8 is the sum of the two before it, so 'fib' calls itself twice: once for each of them, with 'swap' digging the original number back out from under the first result.

                : fib   dup 1 > if dup 1 - fib swap 2 - fib + then ;
                6 fib
            
Quoting Replace program

A string is data sitting on the stack until ',,' puts it back into the program, where it is code again.

                : double   " 2 * " ,, ;
                : quadruple   double double ;
                5 quadruple