Esoteric language Piet



    In the category of graphic esoteric languages , Piet is most famous. The programs on it look like paintings of abstractionism: multi-colored images are used as operators. Colorful algorithms look beautiful, but due to the esoteric nature of the language, they are still poorly adapted to solve real practical problems.

    reference


    Esoteric programming languages ​​(ESNs) are created as an experiment or research. So developers explore the possibilities of languages. Sometimes they just have fun. In any case, this is training for the brain, which few people bother.



    ENP are specific and general purpose. Esoteric of the language just consists in the fact that it is understood, most often, only by the author. Which again brings us back to the fact that learning such a language should be for scientific purposes only. For example, you can solve the problem especially beautifully at a programming competition - you can. Esolangs.org contains information on more than a thousand such languages.

    So Piet: A Story


    The language was invented by David Morgan-Mar and named it after the artist Pete Mondrian (in the original - Piet Mondrian ). The idea was that the programs in the language looked like abstractionism.

    Description


    The language is specific. Several implementations are available. Transitions between the 20 available colors are commands. Each program on Pete is a two-dimensional picture, which is divided into pixels. Interesting detail: errors are simply ignored. That is, if you write an impossible command, the compiler will not stop on it. In some cases, debugging time may be reduced.

    Language Model Elements


    The language model contains three elements: the program is a two-dimensional image composed of pixels. The latter can be of any color. Only 20 participate in the program.



    There are blocks in Pete. These are connected sets of pixels of the same color. A block is the smallest part in building the text of programs. Groups of pixels that correspond to a single block when enlarged is a codel. Magnification is necessary for detailed consideration and accuracy.

    Pete's memory is organized as a stack. The language data type is integers. This is the only type. Processing of characters occurs in the form of their ASCII codes.

    This language has an instruction pointer with a complex structure:

    • pointer to the current block;
    • direction indicator (four in total, standard);
    • codel selection indicator (two relative directions left and right);

    How the instruction pointer goes through the program


    The edge is searched (may be intermittent if the block has a complex shape) of the current block. The most extreme pixel in the direction is selected. The pointer moves from the selected pixel to the next.

    The next command is a color change when changing the current block. The direction of the pointers remains the same for the general case of changing the block. A change in direction occurs when the pointer “collides” with the black block. They play the role of natural constraints and control structures in programs written in Pete.

    So when the pointer meets the black block, the direction changes. A valid search is performed until the program sees a block with color. You can change the direction and "manually." The commands: switch and pointer do this.

    Matching transitions between teams and colors


    In the first column - a change in shade, in the rest - a change in brightness. Zero, one darker and two darker, respectively.
    0-pushpop
    1addsubtractmultiply
    2dividemodnot
    3greaterpointerswitch
    4duplicaterollin (int)
    5in (char)out (int)out (char)

    List of basic commands with explanations


    push - add to the stack the number of pixels from the previous block;
    pop - extract and throw an element from the stack;
    subtract, add, divide, multiply, mod - arithmetic operations are performed with the two upper elements of the stack, the result is written to the stack;
    not - reads the value of the top element in the stack, replaces it with zero (if the value is not zero) or one in other cases;
    greater - extract the top elements in the amount of two pieces and add to the stack 1 for a larger first and 0 for a larger second;

    The rest of the teams are here . Read the

    introduction to Pete here.

    Examples


    Fibonacci numbers






    The program from the image displays the first hundred Fibonacci numbers. On one picture in each encoder is 1 pixel, and on the other 121. The black line shows the progress of the program. Start in the upper left corner. May contain an error. Can you find which one?

    Euclidean algorithm




    Posted by Clint Herron. The goal of the program is to determine the largest common factor in a pair of integers. Line numbers are the author’s own tags, in the middle is the operation, and in the right column is the approximate value of the stack.

    Program listing

    01: m = in(number)   m
    02: n = in(number)   nm
    03: dup              nnm
    04: push 3           3nnm
    05: push 1           13nnm
    06: roll             nmn
    07: r = m % n        rn
    08: dup              rrn
    09: push 1           1rrn
    10: greater          0rn
    11: not              1rn
    12: switch (stay left if it's not greater than 1, otherwise turn right)
                         rn
    LEFT PATH
    L1: dup              rrn
    GUIDE TO OLD CODE AT 03:
    RIGHT PATH
    R1: pop              n
    R2: out(number)

    And this is the code for calculating factorial.



    Some more interesting abstract pictures here .

    Interpreters written in different languages


    In C - npiet .

    Npiet is an interpreter that processes programs in the formats ".gif", ".ppm" and ".png". The distribution contains a visual program editor - called npietedit.



    With it, you can select the next color in the block automatically. The editor takes into account the color of the current block and the necessary command. There is also a translator foogol-> Piet in the archive.

    » Interpreter in C ++ .
    » Interpreter in Ruby .
    » Interpreter at AuotHotkey .
    » Pete's interpreter text file on Python - the task is getting complicated.
    » Interpreter for Piet on Pearl , documentation . In English.
    » More tools .

    Also popular now: