Crazy Programming Languages Challenge - Part 4 - Piet

  1. Brainfuck
  2. LOLCODE
  3. Whitespace
  4. Piet <- You are here
  5. MalbolgeZombie
Overview

This is perhaps the most beautiful programming language that I have seen. In the words of its creator, David Morgan-Mars, the code is "in the form of abstract art". It is named after Piet Mondrian, a Dutch painter who pioneered abstract art. The language is created in pictures that look like his works. There are some fantastic examples that I recommend looking at here, where people have not only produced functioning programs, but also added the extra challenge of making them as artistic as possible.

Each "pixel" of an image is called a codel. This is a single unit of the image, which is not the same as literal pixels that an image would be made up of.

Codels make a colour block, which are "contiguous block of any number of codels of one colour". The can be any size and shape and only diagonal codels don't count as being contiguous.

There are 6 colours: yellow, green, cyan, blue, magenta, red each of which can be light, normal or dark, plus black and white.

Like Whitespace, this works using a stack which stores only integers that can be treated as numbers or Unicode character codes.

Commands are a little difficult to explain. They are created by the changes in colour and in lightness rather than the colours themselves. You can actually start with any colour and a command would be a number of steps from that colour to another colour.

For example, lets say a codel is green, the command for adding is one step along the colour cycle therefore I would use cyan to "add". If my first colour was red, the "add" command would be yellow. There is also a cycle with the darkness/lightness.

The order that I wrote the colours earlier is the order of the cycles.

There is a nice little table that has the main commands:

It is a little confusing to get explain and to understand at first.

Installing and Running

You there are no "official" tools for using this language, but there is a list of ones that people have made here.

I decided to use http://zobier.net/piet/ which is completly web based so it required no setup or installation:

As you can see, it provides a nice little interface with all of the available colours and allows me to step through the execution.

Remember earlier I was talking about the colour cycle and how the colour of the command depends on the current colour - well, in the top right, you can see the available operations and what colour these should be.

Building "Hello, World!"

This should be interesting.

My understanding is that the best way of doing this, like with previous languages, is to push the ASCII number to the stack, and then print it. But you can see that my editor is not very big and so doesn't allow for many commands to be run. "H" alone would take up 72 of the 100 squares.

So, I will just be printing "Hi!" and doing mathematical operations to save space (like I did with Brainfuck).

So the steps that my program will follow:

  1. Add 9 to the stack
  2. Add 8 to the stack
  3. Multiply the top two items of the tack (9 and 8) and put the result (72) there
  4. Print the top item of the stack as an ASCII character (H)

The most complex part of this language is figuring out how it traverses across the colours. My initial attempts kept crashing the browser as it seemed that it was not as simple as just having adjacent colours.

Taken a starting colour, representing a number is simply colouring in that number of blocks with the same colour. If I want to store more than one number, then the next block has to be lighter than the previous.

So my program looks like this so far:

You can see I am starting with a dark green colour. I have a block of 9 squares. Next I have a block of 8 squares. This is lighter as light green is the next shade on the shade cycle described earlier. Next, is one block of the normal green.

It is is on the bottom right of the previous block because that is where I believe the pointer ends up. This tells the program to store the previous two numbers into the stack. If I kept cycling through the shades, I would have kept adding numbers.

The light blue is to multiply. It is light blue because, as in the table above, it is one step along the hue cycle from the previous colour.

Finally, we have a dark green to print the top of the stack as a character.

You can actually try this out yourself. If you go to the editor and paste the following into the box beneath the colours on the right hand side then click any colour, you will see it loaded:

10jjjhhtttttjjjhhtttttjjjhhtttttttthhikjtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt

Word of warning, you should step through instead of play as this version of the program doesn't terminate so it crashes the browser.

So, I will continue with the other characters in the same way. I am able to start with any colour I like after I have printed, so I went with purple for i and then blue for !. Hopefully you can follow the program:

You can run it yourself with:

10jjjhhtttttjjjhhtttttjjjhhtttttttthhikjttaaattttsssqpattttqqqomttttqqqqnnnnpttrrrnnnnpttrrttnnnpdecst

It traverses down the shades of purple which adds 3, 7 and 5 to the stack. The red block multiplies the first two numbers (5 and 7) and then the yellow multiplies the top two numbers again (35 and 3). Then the dark red prints the i.

Then we move onto the blue storing and multiplying 3 and 11. The black squares are used as a sort of flow control. It stops the pointer from going from the light pink to the green, but instead forces it to go left and then down to the last teal square.

The last teal square causes the program to ask for an input. I had difficulty correctly terminating the programming and I am not sure if it was because of a bug in the editor. This was a sort of hack that allowed me to just press play and reach the end.

You will see the output it produces:

Summary

This was, by far, the trickiest language to get my head around of this challenge but also the most interesting. Producing a picture at the end is a pretty cool way of representing code and seeing the art that people had made from it was great!

Due to its complexity, I probably didn't do a very good job explaining it, so once again, here are some resources that I used:

Next week is the final part of this challenge and will be Malbolge.


© 2012-2017