Skip to content

Hello world

Giorgio Garofalo edited this page May 4, 2021 · 19 revisions

Let's try our tool out by creating a simple 'Hello world!' program.

Hello world

Let's analyze this code:

  1. The first red pixel calls a 'define variable' statement (variable.define);
  2. The pixel right next to it defines the variable ID (or name if you prefer). From now on, this orange-ish color always refers to this variable;
  3. The pixels that follow represent the value of the variable. In this case the variable is a string, therefore we are using characters to define its content. In Pikt, characters are the only non-customizable part and are represented by grayscale pixels. For example "A" (ASCII 65) can be used in Pikt as a rgb(65, 65, 65) pixel. ASCII values can be found in any ASCII table but I personally prefer running this line of code: System.out.println(java.util.Arrays.toString(MY_STRING.getBytes())); with any Java compiler such as Rextester. Replacing MY_STRING with "Hello world!" returns [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33], which means the first pixel must be rgb(72, 72, 72), the second rgb(101, 101, 101) and so on. The higher the value the brighter the color, so you can notice that those two darker pixels match the space (ASCII 32) and the exclamation mark (ASCII 33). Also remember that Pikt treats the image as a linear sequence of pixels and ignores their coordinates at compile time, so you can wrap your code wherever you wish.
  4. The white pixels match a whitespace and are skipped;
  5. The darker magenta pixel defines a method call (methodcall). It is not required to be on a new line;
  6. The lighter magenta-colored pixel next to it refers to the method name (stdlib.print).
  7. The orange-ish pixel we defined before is now used as an argument for the print method. Remember that methods only take variables as arguments so that every pixel in the sequence is interpreted as a standalone argument pointing to a defined value.