Skip to content
Cubed edited this page Feb 24, 2024 · 3 revisions

Welcome to the Orange wiki!

Orange is a statically typed compiled language built to be simple but still have enough features to be usable.

The Basics

Just like most languages, Orange starts off execution at the main function.

fn main
    println("Hello, World!")
end

Function headers start with the fn keyword and the body ends with the end keyword.

You'll notice that we don't have parentheses in the function header. Orange doesn't use parentheses in the syntax function definitions.

Variables are declared using the let keyword. We can perform basic arithmetic and assign values with familiar expression syntax from other languages.

fn main
    let x int
    let y int

    y = 4
    x = y * 2

    print("The values of x and y are: ")
    print(x)  // 8
    print(" and ")
    println(y)  // 4
end

You would also notice that the print and println functions can take integers or strings as their arguments. This is called polymorphism, a concept we will see more of in the language. The compiler internally creates a new instance of the function for each type of argument passed to it.

Here is the list of all the basic types that come with the language (as of Feb 2024):

  • int - A signed integer. Always takes up 4 bytes.
  • char - A simple character. One byte.
  • str - An alias for a pointer to characters.
  • void - A zero-sized type.
  • u64 - Takes up 8 bytes. Useful for hash algorithms or bit-optimised code
  • any - Represents any type. Can only be used as a pointer - &any

We can create pointers by prepending a type with &. eg:

  • &int points to an integer
  • &char points to a character. Similar to str, but the compiler will complain when assigning between the two types.
  • &any represents a generic pointer. any types are not allowed to exist independently, therefore variables of this type cannot be dereferenced.
  • And similarly for any other type...

An important point to note is that Orange is strongly typed. This means that a variable of one type cannot be assigned to a variable of any other type. Thus, the following is not allowed:

let x char
let y &int

y = &x  // ERROR

Functions

Every good programming language needs to have functions for it to be useful for us mortals to be able to write maintainable code.

fn double_it: int x -> int
    return x * 2
end

Here, we have defined a simple function double_it that takes an integer and returns an integer with the doubled value.

The syntax of the function header does not use parentheses. We have a colon : instead. The argument has a type and the name after the type - int x.

The return type in the signature is indicated by ->. It is optional as we saw in fn main, where the omission of the return type implies a return type of void.

We can call the function normally in our main function like so:

fn double_it: int x -> int
    return x * 2
end

fn main
    let y int
    let x int

    y = 4
    x = double_it(y)

    print("The values of x and y are: ")
    print(x)  // 8
    print(" and ")
    println(y)  // 4
end

Types

Let us make a linked list now. The linked list will contain integers.

type Int_node
    let data int
    let next &Int_node  // fields can be self-referential!
end

Types in Orange are created using the type keyword. Types can have fields defined by the let keyword.

Clone this wiki locally