-
Notifications
You must be signed in to change notification settings - Fork 0
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.
Let's analyze this code:
- The first red pixel calls a 'define variable' statement (
variable.define
); - 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;
- 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 bergb(72, 72, 72)
, the second isrgb(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. - The white pixels match a
whitespace
and are skipped; - The darker magenta pixel defines a method call (
methodcall
). It is not required to be on a new line; - The lighter magenta-colored pixel next to it refers to the method that should be called (
stdlib.print
here); - The orange-ish pixel we defined before is now used as an argument of 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.
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!"
print(`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 method, so it treats it as a method 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.