Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Latest commit

 

History

History
175 lines (136 loc) · 3.55 KB

rules.md

File metadata and controls

175 lines (136 loc) · 3.55 KB

Methods

To declare a method, use the meth keyword. The syntax should look like the following:

meth example() {
    [contents]
}

A method with arguments would look like this:

meth example(arg_a, arg_b) {
    [contents]
}

You can return a value from the method using the return keyword:

meth example() {
    return 5
}

You can also have single lined methods:

meth example() -> [contents]

Externally added methods can be called like this:

[library identifier]::[method name]()

An example, using the std#println(any) method would look like this: std::println([argument])

Conditions

You can check a condition with an if statement.

The syntax should look like the following:

if ([condition]) {
    [contents]
}

You can add additional expressions using elseif and else:

if ([condition]) {
    [contents]
} elseif ([condition 2]) {
    [contents]
} else {
    [contents]
}

It's worth noting that if you have elseif or else, they have to be on the same line as the last }.

Loops

You can run loops through either for or while.

For Loops

The syntax should look like this:

for ([index name] = [start] to [end]) {
    [contents]
}

[index name] defines the current index of the number. Normally, this should be set to i.

You can define the step as well, simply add step = [value] after the range:

for ([index name] = [start] to [end], step = [step]) {
    [contents]
}

While Loops

The syntax should look like this:

while ([condition]) {
    [contents]
}

Loop Keywords

Both types of loop have keywords that can be used.
continue ignores the rest of the code after it and proceeds onto the next iteration of the loop.
break ignores the rest of the code after it and ends the loop.

Variables

You can declare variables by using either the mut or const keywords.

mut is short for mutable, and these can be changed whenever.
const is short for constant, and these can never be changed.

The syntax should look like this: [finality] [name] = [expression]

Variables are only accessible within the current scope.

This means that any variables declared within braces will only be available in those braces. For example:

if (true) {
    mut a = true
    
    // accessible here
    std::println(a)
}

// will throw an error, not accessible here.
std::println(a)

Comments

You can declare comments by using // before the comment. The syntax should look like this:

// this is a comment

mut a = 5 // a comment can be after anything

Containers

You can define containers with the container keyword. The syntax should look like this:

container [name] ([constructor arguments]) {
    [contents]
}

The this variable is reserved for an instance of the container, so you can modify or get the properties.

Here's an example (note - "Lliw" is Welsh for colo(u)r, to avoid arguments):

container Lliw (red, green, blue) {
    meth average() {
        return (this::red + this::green + this::blue) / 3
    }
}

meth main(args) {
    const lliw = Lliw(32, 178, 254)
    
    // access raw value
    std::println(lliw::red)
    
    // access method
    std::println(lliw::average())
}

Importing

You can import containers and functions from other files using the use keyword. The syntax should look like this:

use "[path]"

So, if I wanted to access the containers and methods inside the secondary class, I would use:

use "secondary"