📜 ⬆️ ⬇️

Esoteric language Piet



In the category of graphic esoteric languages , Piet is best known. The programs on it look like pictures of abstract art: 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 for solving real practical problems.

reference


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


')
EYAP are specific and general purpose. The esoteric of the language is that it is understood, most often, only by the author. Which again brings us back to the fact that such a language should be studied solely for scientific purposes. For example, you can solve the problem especially beautifully at the programming competition - there it is possible. The site esolangs.org contains information on more than a thousand such languages.

So, Piet: the story


The language was invented by David Morgan-Mar and named it by the artist Pete Mondrian (originally - Piet Mondrian ). The idea was to make programs in the language look like abstractionism.

Description


Language specific. Several implementations are available. Transitions between the 20 available colors are commands. Each program on Pita is a two-dimensional image, which is divided into pixels. An 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 shortened.

Elements of the language model


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. Block - the minimum part in the construction of text programs. Groups of pixels that correspond to one block when zoomed in are codel. Magnification is necessary for detailed consideration and accuracy.

Pete's memory is organized in a stack. The data type of the language is integers. This is the only type. Character processing takes place in the form of their ASCII codes.

This language has a pointer to instructions with a complex structure:


How does the instruction guide go through the program?


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

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

So when meeting the pointer with the black block, the direction changes. The search for valid is performed until the program sees a block with color. You can also change direction manually. This is done by the commands: switch and pointer.

Matching transitions between teams and colors


In the first column there is a change in hue, in the rest - a change in brightness. Zero, one unit darker and two darker, respectively.
0-pushpop
oneaddsubtractmultiply
2dividemodnot
3greaterpointerswitch
fourduplicaterollin (int)
fivein (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 - remove and discard an element from the stack;
subtract, add, divide, multiply, mod - arithmetic operations are performed with two top 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 - remove 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 team is here .

An introduction to Pete is here.

Examples


Fibonacci numbers






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

Euclidean algorithm




Author - Clint Herron. The goal of the program is to determine the greatest common factor of a pair of integers. The 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 what the code for calculating factorial looks like.



Some more interesting abstract pictures here .

Interpreters written in different languages


On With - npiet .

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



With it, you can choose 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 foogol-> Piet translator in the archive.

» C ++ interpreter .
» Interpreter on Ruby .
» Interpreter on AuotHotkey .
"The text file of the interpreter Pete on Python - the task is complicated.
» Interpreter for Piet on Pearl , documentation . In English.
» More tools .

Source: https://habr.com/ru/post/313152/


All Articles