npiet

Implementation of programming language Piet

npiet is a Piet interpreter written in C. It allows programs in format .ppm, and since version 0.3 — .png and .gif. The distribution includes the interpreter itself, a visual editor npietedit (allowing to automate small things like choosing color of next block depending on the current color and required command and calculating the number of pixels in the current block) and foogol to Piet translator.

Using npietedit
Using npietedit

Examples:

Hello, World!:

Example for versions npiet 1.2

This example uses only 2 commands — push and out (char). To print one character, one has to create a block of any color; the number of pixels in it should be equal to ASCII-code of this character. After this, one should create a block of color one shade darker of arbitrary size immediately to the right of that block (this will perform push command). Finally, a block of color one hue to the left of the original one of arbitrary size will perform out(char) command. After this, this sequence repeats for the next character.

To end program execution, we use a loop — a single-color block surrounded with black pixels. When the instruction pointer gets inside this block, it can’t leave in any direction.

The decorative version of the example shows that blocks can have any shape, even with pixels of other colors inside.

"Hello, World!" in Piet (basic)
"Hello, World!" in Piet (basic)

"Hello, World!" in Piet (basic, 5x scale)
"Hello, World!" in Piet (basic, 5x scale)

"Hello, World!" in Piet (decorative)
"Hello, World!" in Piet (decorative)

"Hello, World!" in Piet (decorative, 5x scale)
"Hello, World!" in Piet (decorative, 5x scale)

Hello, World!:

Example for versions npiet 1.2

This example was generated using a translator by Sergei Lewis. It provides two tools — a translator from a simple C-like language into an assembler and from the assembler into the Piet image. In this case the original program looked like this:

main()
{
asm{ @"Hello, World!\r\n" }
}

Hello, World! in Piet (autogenerated)
Hello, World! in Piet (autogenerated)

Hello, World! in Piet (autogenerated, 5x scale)
Hello, World! in Piet (autogenerated, 5x scale)

Factorial:

Example for versions npiet 1.2

This example was generated automatically. The original program translated into the image follows; it uses iterative factorial calculation. Note that values of 13! and larger are wrong due to overflow.

main()
{
  f = 1;
  for ( i = 0; i <= 16; i++ )
  {
    __outn(i);
    asm{ @"! = " }
    __outn(f);
    __out(10);
    f = f * (i+1);
  }
}

Factorial in Piet (autogenerated)
Factorial in Piet (autogenerated)

Factorial in Piet (autogenerated, 4x scale)
Factorial in Piet (autogenerated, 4x scale)

Fibonacci numbers:

Example for versions npiet 1.2

This example was generated automatically. The original program translated into the image follows; it uses iterative calculation of Fibonacci numbers.

main()
{
  f1 = 0;
  f2 = 1;
  for ( i = 1; i <= 16; i++ )
  {
    __outn(f2);
    __out(44);
    __out(32);
    f2 = f1 + f2;
    f1 = f2 - f1;
  }
  __out(46);
  __out(46);
  __out(46);
  __out(10);
}

Fibonacci numbers in Piet (autogenerated)
Fibonacci numbers in Piet (autogenerated)

Fibonacci numbers in Piet (autogenerated, 4x scale)
Fibonacci numbers in Piet (autogenerated, 4x scale)