-
Notifications
You must be signed in to change notification settings - Fork 0
Hello world
Let's try our tool out by creating a simple 'Hello world!' program.
Let's analyze this code:
-
The first red pixel calls a 'set variable' statement (
variable.set
); -
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;
-
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 argb(65, 65, 65)
pixel. ASCII values can be found in an ASCII table but Pikt also offers thestrconvert=<string>
command:
java -jar pikt.jar strconvert="Hello world!"
prints the following:
RGB: 72 101 108 108 111 32 87 111 114 108 100 33
H e l l o W o r l d !
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.
-
The white pixels match a
whitespace
and are skipped; -
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`())
Running the generated executable (or interpreting via -interpret
) successfully prints Hello world!
out.