Getting started
In this chapter you install Cromo, write your first program, and meet cromo, the command that builds, runs, tests and formats Cromo code.
#Installation
Cromo is installed for your user, without administrator rights, with one line. On Windows, in PowerShell:
irm https://cromolang.org/install.ps1 | iexOn Linux:
curl -sSf https://cromolang.org/install.sh | shThe script downloads the archive of the latest version, checks its SHA-256, and unpacks it in ~/.cromo (%USERPROFILE%\.cromo on Windows). It adds ~/.cromo/current/bin to the PATH and, if it finds Visual Studio Code, installs the Cromo extension. Open a new terminal, then check the installation:
cromo versioncromo 0.27.1 (LLVM 23.1.4)That is all. cromo contains the compiler and the linker: it needs no C compiler, no Visual Studio and no Windows SDK, and the programs it writes do not depend on the C runtime of the system.
Note: the archive can also be installed by hand: download it from cromolang.org, unpack it anywhere and add its
binfolder to thePATH.cromofinds the library in thelibfolder next tobin.
#Hello, world!
Create a folder for your programs and, in it, a file named hello.cro:
i32 main() {
println("Hello, world!")
ret 0
}Then run it:
cromo run hello.croHello, world!Let us look at the program:
i32 main()declares the functionmain, where every program starts. The type comes before the name, as in C:mainreturns ani32, a 32-bit integer, which becomes the exit code of the program.printlnprints a line. Its first argument is a format string, which can contain{}for the values that follow it:println("{} + {} = {}", a, b, a + b).retreturns from a function. Cromo writesretwhere C writesreturn.- There are no semicolons: a statement ends at the end of its line.
A main that returns nothing is also allowed, and the exit code is then 0:
void main() {
println("Hello again!")
}Hello again!#Building an executable
cromo run compiles the program into a temporary folder, runs it and deletes it. To keep the executable, use cromo build:
cromo build hello.cro # writes hello.exe on Windows, hello on Linux
./helloBy default cromo builds a debug executable: it is optimized, but it checks every integer operation for overflow, and it contains the information that a debugger needs. With --release integers wrap around on overflow, as in C, and the executable is smaller:
cromo build --release hello.croThe executable runs on any machine with the same system, without installing anything. cromo can also write an executable for the other system:
cromo build --target linux hello.cro # on Windows: an executable for Linux
cromo build --target windows hello.cro # on Linux: hello.exe for Windows#A project
A project is a folder with a file main.cro. Without a file name, cromo build, cromo run and cromo test use the main.cro of the current folder, and the executable is named after the folder:
mkdir greeter
cd greeter
# write main.cro
cromo runThe other files of the folder are modules, which main.cro imports by name: chapter 11 explains them.
#Formatting
Cromo code has one style, and cromo fmt writes it: four spaces of indentation, the opening brace on the same line, one space around operators. There are no options to discuss:
cromo fmt # every .cro file of this folder and its subfolders
cromo fmt hello.cro # one file
cromo fmt --check # only lists the files that are not formatted#Visual Studio Code
The Cromo extension, which the installation adds to Visual Studio Code, colors the code and uses cromo lsp, the language server of Cromo, to show:
- the errors while you write, without compiling by hand;
- the type and the declaration of a name, when the mouse is over it;
- the definition of a name, with F12;
- the members of a value, after a dot;
- the formatting of the file, when it is saved.
Any editor that speaks the Language Server Protocol can use cromo lsp in the same way.
#Summary
cromo run file.crocompiles and runs a program;cromo buildwrites the executable.- A program starts from
main, which returns its exit code. - The type comes before the name;
retreturns; statements end at the end of the line. cromo fmtformats the code, and the extension for Visual Studio Code shows the errors while you write.
In the next chapter you will write a complete program, before learning the details.