Examples

These examples are adapted from programs exercised by the parser, semantic, SLangIR, and integration tests.

Variables and printing

void main()
{
    int number = 12;
    double ratio = 2.5;
    boolean flag = false;
    character marker = '!';
    string label = "number";
    print(label, number, marker, flag, ratio);
}

Conditionals

void main()
{
    int score = 75;
    if (score >= 90) {
        print("A");
    } elseif (score >= 80) {
        print("B");
    } elseif (score >= 70) {
        print("C");
    } else {
        print("D");
    }
}

Arrays and loops

void main()
{
    int values[4];
    int index = 0;

    while (index < 4) {
        values[index] = index * 10;
        print(values[index]);
        index++;
    }
}

Array sizes and indices use integer literals or identifiers.

For loop

void main()
{
    for (int index = 0; index < 5; index++) {
        print(index);
    }
}

Recursive Fibonacci

int fibonacci(int value)
{
    if (value <= 1) {
        return value;
    }
    return fibonacci(value - 1) + fibonacci(value - 2);
}

void main()
{
    int result = fibonacci(10);
    print(result);
}

This compiles to a native executable that prints 55.

Explicit conversion

void main()
{
    int code = 65;
    character letter = (character) code;
    string text = (string) letter;
    print(text);
}

SLang does not perform implicit numeric conversions; use one of the supported explicit casts when the target type differs.