olang | O Programming Language


3 Getting Started

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!

3.1 An olang program

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

3.2 Functions

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))
}

3.3 Comments

Comments starts with a #.

# Hi I'm a comment and I'll be ignored by the compiler.

3.4 Variables

var answer: u32 = 42

3.5 Flow control

Any non zero expr is true.

3.5.1 If-Else

if expr {
  # statement
} else if expr {
  # statement
} else {
  # statement
}

While loop

while expr {
  # statement
}

3.5.2 While loop

while expr {
  # statement
}

3.6 Primitive data types

‘u8’

Unsigned 8 bits.

‘u16’

Unsigned 16 bits.

‘u32’

Unsigned 32 bits.

‘u64’

Unsigned 64 bits.

3.7 Binary Operations

3.7.1 Logical

‘Equals’
expr1 == expr2

Results zero (false) or one (true).

‘Less’
expr1 < expr2

Results zero (false) or one (true).

‘Less Equal’
expr1 <= expr2

Results zero (false) or one (true).

‘Greater’
expr1 > expr2

Results zero (false) or one (true).

‘Greater Equal’
expr1 >= expr2

Results zero (false) or one (true).

‘Or’
expr1 || expr2

Results zero (false) if both are true or one (true) if any is true.

‘And’
expr1 && expr2

Results zero (false) if any is false or one (true) if both are true.

3.7.2 Bitwise

‘Shift left’
n << bits
‘Shift left’
n >> bits
‘And’
n & bits
‘Or’
n | bits

3.7.3 Arithmetic

‘Addition’
expr1 + expr2
‘Subtraction’
expr1 - expr2
‘Multiplication’
expr1 * expr2
‘Division’
expr1 / expr2
‘Remaining’
expr1 % expr2

3.8 Pointers

Dealing with memory address.

3.8.1 Address of (&)

Get the address of a variable.

var my_var: u32 = 0

var my_pointer: u32* = &my_var

3.8.2 Dereferencing (*)

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.