Get started Browse docs
On this page 5 sections
Get started
Your first program
Build and run the smallest Orbit program, then read its exit code. A 0 means success, and a 3 is a result you can check.
You will write a four-line program, build it into a binary, run it, and read its result from the shell. You need a built compiler first: see install the compiler.
The entry point
Every Orbit program starts at fn main() -> int. The number main returns is the process exit code: the number the shell reports when the program ends. By convention 0 means success and any other number means something else happened.
That gives you a test with nothing to install. A program that returns 0 passed. A program that returns 3 reports 3, and you check that it is the 3 you expected. The compiler’s own test suite checks programs the same way.
Create hello.orb:
fn main() -> int {
return 0
}
That is a complete program. It does nothing, and it reports success by exiting 0.
Build it
orbit build compiles one file into one native binary. Name the output with -o:
.\orbit.exe build hello.orb -o hello.exe
./orbit build hello.orb -o hello
Unless the compiler folder is on your PATH, run orbit by its local path, as shown. Every example in these docs does this.
Run it, read the answer
Run the binary, then ask the shell what it returned:
.\hello.exe
echo $LASTEXITCODE # 0
./hello
echo $? # 0
Now change the program to return 3, rebuild, and run again. The shell reports 3. That is a complete test: you expected 3 and observed 3.
The exit-code loop
From here on, every snippet in these docs follows the same loop: write, build, run, read the number. Four examples to build the habit, each one verified against the 0.1.0 fixed-point compiler:
20 seconds — Call a function from main. The sum is the exit code.
fn add(left: int, right: int) -> int {
return left + right
}
fn main() -> int {
return add(20, 22)
}
./orbit build tour1.orb -o tour1
./tour1
echo $? # 42
Exit
42
30 seconds — val never changes, var mut does. name.len() is 5, so this exits 8.
fn main() -> int {
val name = "orbit"
val retries: int = 3
var mut total: int = 0
total = total + retries
return total + name.len()
}
./orbit build tour2.orb -o tour2
./tour2
echo $? # 8
Exit
8
30 seconds — Recursion, if, and comparison work as you would expect. fact(5) is 120.
fn fact(n: int) -> int {
if n <= 1 {
return 1
}
return n * fact(n - 1)
}
fn main() -> int {
return fact(5)
}
./orbit build tour3.orb -o tour3
./tour3
echo $? # 120
Exit
120
30 seconds — Early returns select a label, and the label selects the code. 7 is positive, so this exits 1.
fn classify(n: int) -> string {
if n < 0 { return "negative" }
if n == 0 { return "zero" }
return "positive"
}
fn main() -> int {
val label: string = classify(7)
if label == "positive" { return 1 }
return 0
}
./orbit build tour4.orb -o tour4
./tour4
echo $? # 1
Exit
1
Statements and comments
Indentation is up to you. The compiler ignores it, and orbit fmt can normalize it later. Write one statement per line.
What next
A program that listens for requests is the other half of the language. Your first service builds one and calls it with curl.