Skip to content

Hello world

Giorgio Garofalo edited this page Jan 19, 2022 · 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 'set variable' statement (variable.set );
  2. The pixel right next to it defines the variable ID (or name if you prefer). From now on, this orange-ish color 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 in order to define its content. In Pikt, characters are the only non-customizable parts 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 executing this editable code. "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 is 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 that you can wrap your code wherever you wish.
  4. The white pixels match a whitespace and are skipped;
  5. The magenta pixel defines a print statement (print ) that takes an expression as an argument, which happens to be the orange-ish pixel we had defined earlier. You can also use the print statement with the string directly as an argument, but we declared a variable for demonstration purposes.

By running Pikt with this image as source and with the -printoutput argument set, we can see the generated Kotlin code:

var `FFB400` = "Hello world!"
println(`FFB400`())

You might notice the weird parenthesis after the argument, that's because Pikt is not able to check at compile time whether a pixel represents a variable or a function, so it treats it as a function call, changing the behavior of the invoke() operator from within the stdlib, so that a variable just returns itself when called.

Running the generated executable (or interpreting via -Dinterpret=jvm) successfully prints Hello world! out.