Welcome to the O programming language! This chapter will introduce you to the basics of the language’s syntax. We’ll cover variables, data types, operators, control flow, and functions.
By the end of this chapter, you’ll have a solid foundation in the O language
syntax and be ready to start writing your own programs. If you are already
familiar with C-like languages, you will find out that O
behave pretty
much the same with some small quirks. Let’s dive in!
An O programming language program starts with a main
function. This
function must return the program exit code.
fn main(): u8 { return 0 }
To compile the program you can use olc
.
olc my_prog.ol -o my_prog ./my_prog
Unlike C, O language does not require function prototypes. This means you can call a function before it’s defined, making your code more flexible and easier to read in many cases.
fn main(): u8 { return fib(8) } fn add(a: u32, b: u32): u32 { return a + b } fn fib(n: u32): u32 { if n <= 2 { return n } return add(fib(n - 1), fib(n - 2)) }
var answer: u32 = 42
Any non zero expr is true.
if expr { # statement } else if expr { # statement } else { # statement }
While loop
while expr { # statement }
while expr { # statement }
Unsigned 8 bits.
Unsigned 16 bits.
Unsigned 32 bits.
Unsigned 64 bits.
expr1 == expr2
Results zero (false) or one (true).
expr1 < expr2
Results zero (false) or one (true).
expr1 <= expr2
Results zero (false) or one (true).
expr1 > expr2
Results zero (false) or one (true).
expr1 >= expr2
Results zero (false) or one (true).
expr1 || expr2
Results zero (false) if both are true or one (true) if any is true.
expr1 && expr2
Results zero (false) if any is false or one (true) if both are true.
n << bits
n >> bits
n & bits
n | bits
expr1 + expr2
expr1 - expr2
expr1 * expr2
expr1 / expr2
expr1 % expr2
Dealing with memory address.
Get the address of a variable.
var my_var: u32 = 0 var my_pointer: u32* = &my_var
Accessing the value of the address a pointer points to.
var my_var: u32 = 0 # The result of *my_pointer is the value of my_var (0). *my_pointer *my_pointer = 42
The program above sets the my_var
’s to 42.