Quipu

From Esolang
Jump to navigation Jump to search

Quipu is an esoteric programming language inspired by Quipus (also known as "talking knots") – recording devices historically used by Incas. Quipu was originally designed by Vladimir Kostyukov in Jan 2013.

Specification

A program in Quipu is represented by vertical threads (cords) that contain various types of knots. The number of threads is unlimited as well as the number of knots on each thread. All knots of every thread should be aligned to the same indentation level from the beginning of the source file.

Both types of Quipu entities (threads and knots) have values and can be evaluated. Knots evaluate by themselves, threads by reference knot ([]) evaluating. The self values of threads are stored in the zero knots. It allows to use self values as implicit argument in calculations.

Data Types

There are two simple data types available in a Quipu program: big integer numbers and strings.

Comments

Quipu supports multiline comments that start with " and end with ". Comments are allowed only in the beginning of the source file. The typical use case for comments is labeling the Quipu threads by numbers to increase the readability of the program.

Program Execution

Before executing the values of all threads are 0 (zero).

Execution starts with the first thread (then the second, etc.). At each step of execution the current knot is evaluated. The execution continues until the last knot of the last thread has been evaluated, or a halt knot (::) is evaluated.

When jumping, the value of a thread is assigned to the value of the last evaluated knot in the thread.

There are two reasons for jumps:

  • All knots of the current thread have been evaluated (jump to next thread).
  • Evaluation jumps knot (jump to specified thread).

There are two ways to get an exception during the execution of a Quipu program:

  • Referencing (with the [] knot) a nonexistent thread;
  • Jumping to a nonexistent thread.

Knots Table

Let n be the number of the current knot and (n) be the value of the current knot.

Knot Meaning
[] This knot merges with knot n - 1. The new knot evaluates as the value of thread (n - 1).
'a'b'c\n String.
1#2#3%4@5& The number 12345. # - thousands, % - hundreds, @ - tens, & - units. The sequence of # digits represents value * 1000. Example: 6#5#4#3%2@1& = 654 * 1.000 + 321 = 654.321.
;; Delimiter for numbers and strings.
\/ Input from console the value of knot n.
/\ Output (n - 1) to console. This knot merges with knot n - 1. The new knot evaluates as (n - 1).
^^ The value of the current thread.
## Copy of knot n - 1.
++ The sum of (n - 2) and (n - 1).
-- The difference between (n - 2) and (n - 1).
** The product of (n - 2) and (n - 1).
// The quotient of (n - 2) and (n - 1).
%% The modulo of (n - 2) and (n - 1).
== Jump to thread (n - 1) if (n - 2) is equal to zero. This knot merges with n - 2 and n - 1. The new knot evaluates as (n - 2).
<< Jump to thread (n - 1) if (n - 2) is less than zero. This knot merges with n - 2 and n - 1. The new knot evaluates as (n - 2).
<= Jump to thread (n - 1) if (n - 2) is less than or equal to zero. This knot merges with n - 2 and n - 1. The new knot evaluates as (n - 2).
>> Jump to thread (n - 1) if (n - 2) is greater than zero. This knot merges with n - 2 and n - 1. The new knot evaluates as (n - 2).
>= Jump to thread (n - 1) if (n - 2) is greater than or equal to zero. This knot merges with n - 2 and n - 1. The new knot evaluates as (n - 2).
?? Jump to thread (n - 1). This knot merges with n - 2 and n - 1. The new knot evaluates as (n - 2).
:: Halt the execution.

Examples

Hello World!

'H
'e
'l
'l
'o
' 
'W
'o
'r
'l
'd
'!
\n
/\

Cat program

\/
/\

Prints the numbers from 0 to 99

"0  1  2"

1& 1& 1&
[] ++ []
/\    1%
\n    --
/\    3& 
      ==   
      0&
      ??

Calculates the sum of numbers from 0 to 99

"0  1  2  3"

1& 1& 1& 0&
[] ++ [] []
++    1% /\
      --
      3&
      ==
      0&
      ??

Calculates the factorial of given number

"0  1  2 "

1& 1& 1&
[] -- []
** 2& 0&
1& >= >>
>> \/ 0&
1&    []
      /\

Contributed by User:Jo King based on this code golf answer.

Prints the first N members of the Fibonacci sequence

"0  1  2  3  4  5  6  7  8"

\/ 1& 3& 4& 3& 7& 3& 1& 7&
   -- [] [] [] [] [] ^^ []
   0&    1& 2& 6& /\ -- 7&
   []    -- [] == 7& 1& >>
   --    7& ++ ', [] >> '.
   7&    []    /\ 1&    /\
   []    **    '  >>
   **    1&    /\
   0&    ++
   []
   ++
   8&
   ==

99 bottles of beer

"0 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21"

'n 'N '  '  's '  '  ', '. 'T 'G 1& 1% 1@ 1@ 9& 2& 9& 1& 9& 1& 1@
'o 'o 'm 'b    'o 'o '  \n 'a 'o ++ ;; 2& 2& [] /\ [] /\ [] [] []
      'o 'o    'f 'n       'k '     1@ [] [] /\ 3& /\ 3& /\ /\ /\
      'r 't    '  '        'e 't    1& 2& /\ 7& [] 7& [] 7& 2& 7&
      'e 't    'b 't       '  'o    [] -- 3& [] /\ [] /\ [] [] []
         'l    'e 'h       'o '     -- 1@ [] /\ 4& /\ 5& /\ /\ /\
         'e    'e 'e       'n 't       6& /\ 1@ [] 1& [] 0& 3& 9@
               'r '        'e 'h       == 4& 2& /\ /\ /\ [] [] 9&
                  'w       '  'e          [] [] 5& 3& 6& /\ /\ /\
                  'a       'd '           /\ 1& [] [] [] 2& 4& 3&
                  'l       'o 's          5& -- /\ /\ /\ [] [] []
                  'l       'w 't          [] /\ 6& 5& 7& /\ /\ /\
                           'n 'o          /\ 3& [] [] [] 3& 5& 4&
                           '  'r          6& [] /\ /\ /\ [] [] []
                           'a 'e          [] /\ 7& 6& 1& /\ /\ /\
                           'n '           /\ 4& [] [] /\ 4& 6& 5&
                           'd 'a          7& [] /\ /\ 3& [] [] []
                           '  'n          [] /\ 2& 8& [] /\ /\ /\
                           'p 'd          /\ 5& /\ [] /\ 5& 7& 6&
                           'a '           1@ [] 3& /\ 5& [] [] []
                           's 'b          2& /\ [] \n [] /\ /\ /\
                           's 'u          [] 6& /\ /\ /\ 6& 0& 8&
                           '  'y          /\ [] 4&    8& [] [] []
                           'i '           3& /\ []    [] /\ /\ /\
                           't 's          [] 8& /\    /\ 8& 2&
                           '  'o          /\ [] 5&       [] []
                           'a 'm          4& /\ []       /\ /\
                           'r 'e          [] \n /\       \n 3&
                           'o '           /\ /\ 8&       /\ []
                           'u 'm          5& 1@ []          /\
                           'n 'o          [] 1& /\          4&
                           'd 'r          /\ ??             []
                              'e          8&                /\
                                          []                5&
                                          /\                []
                                                            /\
                                                            8&
                                                            []
                                                            /\

Exponentiation

This program takes two numbers a and b and calculates a to the power of b.

"0  1  2  3  4  5  6  7  8  9 10"
\/ \/ 5& 7& 5& 1& 7& 1& 1& 7& 3&
      [] [] [] 8& [] 8&    [] []
      1& 2& 5& [] 2& []    1@ /\
      -- %% [] -- // --    ==
         2& ** 0&    1&    2&
         []    []    []    ??
         **    **    **
         1&    4&    6&
         ++    []    []
         ^^    ++    ++
         **
         1&
         --
         8&
         []
         **
         1&
         ++

Implementations