First program

View source

Create a project, run it, change it, build an executable, format the code and see what a runtime error looks like.

In this page you create a small project, run it, change it, and build an executable. You need Actus installed. If actus --version does not work yet, start with Install.

Create a project

Run this in a terminal:

bash
actus new hello
text
Initialized empty Git repository in /home/you/hello/.git/
created `hello`

Next steps:
  cd hello
  actus check
  actus build --emit exe
  actus build --release --emit exe
  actus run

actus new makes a new folder called hello. It also starts a Git repository in it. If you do not want that, use actus new hello --no-git. Move into the folder:

bash
cd hello

What is in the project

The project has four files:

text
hello/
├── .gitignore
├── Actus.lock
├── Actus.toml
└── src/
    └── main.act

Actus.toml describes the project:

toml
[package]
name = "hello"
version = "0.1.0"
edition = "alpha"
entry = "main"

[build]
target = "host"
profile = "debug"
runtime = "std"
  • name and version identify your package.
  • edition = "alpha" selects the language edition.
  • entry = "main" says that the program starts at the verb called main.
  • target = "host" builds for the computer you are using.
  • profile = "debug" is the default build mode. Add --release to a command to get an optimized build.
  • runtime = "std" lets the program use the standard library.

src/main.act is your program:

actus
"""Entry point of the new program: print a greeting to standard output."""
verb main() {
    println(string: "Hello, world!");
}

A verb is a function. main is where the program starts. println writes a line of text. The word before the colon, string, is the label that says what kind of value follows. The text between the """ marks above the verb is its documentation. The compiler keeps it with the verb, and strict checking asks for it on every verb.

The .gitignore file keeps build output out of Git.

Run it

bash
actus run
text
Hello, world!
process exited with status 0

actus run builds the program, runs it once, and removes the temporary executable. The first line is printed by your program. The second line is printed by actus run to tell you how the program ended. Status 0 means success.

Actus.lock was written by actus new. It records what the build depends on, and the other commands read it. You rarely touch it.

Change the program

Open src/main.act and replace its contents with this:

actus
verb main() {
    println(string: "Hello, world!");
    println(string: "Counting to three:");
    for erg i in 0..3 {
        println(int: i);
    }
    erg temperature: Int = 18;
    if temperature > 25 {
        println(string: "warm");
    } else if temperature > 10 {
        println(string: "mild");
    } else {
        println(string: "cold");
    }
}

Some things to notice:

  • println(string: ...) prints text and println(int: ...) prints an integer. Each one ends the line. Printing lists the other forms, including print, which does not.
  • for erg i in 0..3 repeats the loop body for i equal to 0, 1 and 2. The end of the range, 3, is not included.
  • erg temperature: Int = 18; creates a variable that you own. The name comes first, then the type, then the value.
  • if, else if and else pick one branch. The condition must be a true-or-false value.

Run it again:

bash
actus run
text
Hello, world!
Counting to three:
0
1
2
mild
process exited with status 0

Build an executable

actus run does not keep anything. To get a file you can run on its own, use actus build:

bash
actus build --emit exe
text
built `/home/you/hello/capsula/debug/x86_64-unknown-linux-gnu/main.bin`

The message names the file that was built. Your path will be different. The folder name contains the name of your system, for example x86_64-unknown-linux-gnu. Run the file by typing the path it printed:

bash
./capsula/debug/x86_64-unknown-linux-gnu/main.bin
text
Hello, world!
Counting to three:
0
1
2
mild

The capsula folder holds everything the compiler generates. You can delete it at any time. It comes back the next time you build.

Without --emit exe, actus build stops early and only writes an object file (main.obj). That file is not a program you can run.

Linking an executable needs a linker from your system. On Linux and macOS Actus calls cc, so a C compiler such as gcc or clang must be installed. On Windows you need the MSVC toolset and the Windows SDK, which come with "Build Tools for Visual Studio". See Install. actus run links an executable too, so it needs the same tools.

Format the code

actus fmt puts your source in the standard layout, so every Actus program looks the same. To only check, without changing anything, add --check:

bash
actus fmt --check
text
./src/main.act is formatted

Now make the code untidy on purpose. Change the first println line so it has no indent and extra spaces:

actus
println(string:    "Hello, world!");

Check again. The command exits with status 1 when a file needs formatting:

bash
actus fmt --check
text
./src/main.act is not formatted

Run actus fmt without --check to fix the file:

bash
actus fmt
text
formatted `./src/main.act`

The line is back to println(string: "Hello, world!");. If a file is already formatted, actus fmt leaves it alone and says so:

bash
actus fmt
text
`./src/main.act` is already formatted

Check the code

actus check reads your program and looks for mistakes, but it does not build anything. It is faster than a build.

bash
actus check
text
checked `/home/you/hello/src/main.act` successfully

When something is wrong, check tells you where. Replace the program with this one, which puts text in an Int variable:

actus
verb main() {
    erg count: Int = "three";
    println(int: count);
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1027] at 2:5: type mismatch for `count`: expected `Int`, found `String`

The message gives the file, the error code, the line and column (2:5), and what went wrong. actus run and actus build report the same error and stop.

A runtime error

Some mistakes can only be found when the program runs. Replace the program with this one:

actus
verb main() {
    erg level: u8 = 250u8;
    println(string: "Adding 10 to 250...");
    level = level + 10u8;
    println(string: "Done.");
}

A u8 holds numbers from 0 to 255, so 250 plus 10 does not fit. The file is valid, and actus check accepts it. Run it:

bash
actus run
text
Adding 10 to 250...
error: integer overflow at src/main.act:4:13
process exited with status 101

The program printed its first line, then stopped at the addition. The message says what happened (integer overflow) and where (src/main.act, line 4, column 13). The line Done. never prints. Status 101 is the status Actus uses when a runtime check fails. Overflow, division by zero and an index outside an array are checked in the same way. An executable you built reports the same message and exits with status 101, which you can see with echo $? in a Unix shell.

Actus stops here instead of giving a wrong number. If you do want the number to wrap around or to stay at the largest value, say so with a different operator. +% wraps and +| saturates:

actus
verb main() {
    erg level: u8 = 250u8;
    erg capped: u8 = level +| 10u8;
    erg wrapped: u8 = level +% 10u8;
    println(int: capped);
    println(int: wrapped);
}
text
255
4
process exited with status 0

Where to go next

You have created a project, run it, built it, formatted it and checked it. Continue with Variables and values to learn the language step by step. You can also go back to the Introduction for the ideas behind the language, or look at the releases (opens in a new tab) for what is new.