Skip to content
Sten edited this page Oct 6, 2023 · 4 revisions

Variable Declarations

You can declare variables using the let or const keyword.

let variable = "hello"
const constant = "world"

If you use the let keyword the variable is able to be changed later in the code. If you use the const keyword you will never be able to change it after declaration.

Functions

You can use the fn keyword to define a function.

fn test()
{
    // Function body
}

The function creates a new scope, any variables defined within the function will not be able to be accessed outside of it.

fn test()
{
    let testvar = 69
    print("test: " + testvar)
}

test()
print(testvar) // This will give an error because `testvar` only exists within the scope of the test function

If Statements

If statements can ofcourse be created by using the if keyword.

if (condition)
{
    // Only run if the condition is true
}

An if statement will behave differently depending on the type you pass to it.

  • Number
    • num != 0
  • Boolean
    • bool == true
  • Other (any other type)
    • other != null

Unlike the function, the if statement doesn't create a new scope. Therefore this is valid code:

if (true)
{
    let test = "test text"
    print("if statement: " + test)
}

print(test)

Else

You can create if-else statements by using the else keyword after closing the if statement.

if (condition)
{
    // Run the code if the condition is true
}
else
{
    // Run the code if the condition is false
}

You can also create else-if statements by adding an if after the else keyword. This can be stacked infinitely.

if (condition)
{
    // Run the code if the condition is true
}
else if (other_condition)
{
    // Run the code if the condition is false and other_condition is true
}
else
{
    // Run the code if both conditions are false
}

Loops

For Loop

The for loop is used to iterate over a list. It also creates a new scope just like the function which includes the iterator variable.

let list = ["item 1", "item 2", "item 3"]

for (item : list)
{
    print(item)
}

You can create a list of numbers using the range function like in Python.

for (i : range(10)) // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
{
    print(i)
}

You can get the length of an array or string using the len function.

let list = ["item 1", "item 2", "item 3"]

for (i : range(len(list)))
{
    print(list[i])
}

While Loops

While loops repeat until the condition is false. The while loop creates a new scope just like the function and for loop.

while (condition)
{
    // Run this code until the condition variable is false
}

Example usage:

let i = 0
while (i < 10)
{
    print(i)
    i += 1
}

Classes

You can create classes using the class keyword

class ClassName
{
    fn ClassName()
    {
        // Constructor
    }
}

const instance = ClassName() // Create an instance of the class

You can create a constructor which is called every time you create a new instance of the class.

You can also have variables inside the class

class ClassName
{
    const constant = "class constant"
    let variable = "class variable"

    fn ClassName()
    {
        // Constructor
        variable = "new variable"
    }
}

const instance = ClassName() // Create an instance of the class
print(instance.constant) // -> class constant
print(instance.variable) // -> new variable

You can access properties by separating the instance name and the property name with a "."

Clone this wiki locally