Welcome to Kairo! It’s traditional when learning a new language to write a program that prints “Hello, World!” to the screen. We’ll follow this tradition here.
:::note This guide assumes basic familiarity with the command line. Kairo makes no specific demands about your editing tools or where your code lives. Feel free to use your favorite IDE. Many IDEs support Kairo; check their documentation for details. :::
Writing and Running a Kairo Program
First, create a new directory for your Kairo project. Ideally in ~/projects/hello_world
Next, create a new source file and call it main.hlx. Kairo files use the .hlx extension. If you’re using more than one word in your filename, use an underscore to separate them (e.g., hello_world.hlx).
Open the main.hlx file you just created and enter the following code:
fn main() {
print("Hello, World!");
}
Compiling and Running the Program
Save the file and return to your terminal in the ~/projects/hello_world directory. To compile your Kairo program, run:
kairo main.hlx
This command tells the Kairo compiler to compile main.hlx and output an executable named hello_world.
To run your compiled program, use the following command:
:::tip Unix is also MacOS, Linux and Linux like operating systems. :::
You should see the output:
Hello, World!
Congratulations! You’ve written and run your first Kairo program. You’re officially a Kairo programmer—welcome!
Writing a Kairo Program Without a Main Function
Kairo provides a feature that allows you to write programs without explicitly defining a main function, similar to scripting languages like Python. This is done using the #[no-entry] attribute.
Create a new file called hello_no_entry.hlx and enter the following code:
#[no-entry]
print("Hello, World!");
This code does not include an explicit main function. The #[no-entry] attribute allows the compiler to automatically determine what needs to go into the main function. However, using #[no-entry] can cause undefined behavior, so it is not recommended for complex programs.
Compiling and Running the No-Entry Program
To compile this Kairo program, run:
kairo hello_no_entry.hlx
You should see the output:
Hello, World!
Anatomy of a Kairo Program
Let’s review the “Hello, World!” program in detail:
The Main Function
fn main() {
}
These lines define a function named main. The main function is special: it is always the first code that runs in every executable Kairo program. Here, the first line declares a function named main that has no parameters and returns nothing. If there were parameters, they would go inside the parentheses ().
The function body is wrapped in {}. Kairo requires curly brackets around all function bodies. It’s good style to place the opening curly bracket on the same line as the function declaration, with one space in between.
The Print Statement
print("Hello, World!");
This line does all the work in this little program: it prints text to the screen. There are several important details here:
- Indentation: Kairo style uses four spaces for indentation, not tabs.
- Fucntions:
printcalls a Kairo function, that would call the internal std::out fucntion to print. - Strings: The
"Hello, World!"string is passed as an argument toprint, and the string is printed to the screen. - Semicolons: The line ends with a semicolon
;, which indicates that the statement is complete.
Compiling and Running Are Separate Steps
You’ve just run a newly created program, so let’s examine each step in the process.
Before running a Kairo program, you must compile it using the Kairo compiler by entering the kairo command and passing it the name of your source file, like this:
kairo main.hlx -o hello_world --debug
This shows the source code file with the .hlx extension and the executable file (hello_world.exe on Windows, but hello_world on other platforms). From here, you run the executable file like this:
You would also notice a file with the .pdb extension if your on windows, thats used by the debugger to understand errors and tracebacks, to allow you to see what bugs there are in your code if any.
If your main.hlx is your “Hello, World!” program, this line prints Hello, World! to your terminal.
Kairo is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Kairo installed.
Just compiling with kairo is fine for simple programs, but as your project grows, you’ll want to manage all the options and make it easy to share your code. Future sections will introduce tools and best practices for writing real-world Kairo programs.
Happy coding! 🚀