Examples

See Lyx in action.

Hello World

The classic starting point - minimal and efficient.

The simplest program in Lyx. The main function is the entry point. PrintStr outputs a string.

hello.lyx
fn main(): int64 {
    PrintStr("Hello, World!\n");
    return 0;
}

Fibonacci

Demonstrates recursion and function calls.

A classic example for recursion. The fib function calls itself to calculate the Fibonacci sequence. Note the use of if and for loops.

fibonacci.lyx
fn fib(n: int64): int64 {
    if n <= 1 {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

fn main(): int64 {
    for i := 0; i < 10; i++ {
        PrintInt(fib(i));
        PrintStr(" ");
    }
    PrintStr("\n");
    return 0;
}

Struct with Methods

Object-oriented programming with structs and methods.

Structs are user-defined data types. In Lyx they can also have methods. The self parameter allows access to own fields.

struct.lyx
type Point = struct {
    x: int64;
    y: int64;
    
    fn Create(newX, newY: int64): Point {
        var p: Point;
        p.x := newX;
        p.y := newY;
        return p;
    }
    
    fn distance(self: Point): int64 {
        return self.x * self.x + self.y * self.y;
    }
}

fn main(): int64 {
    var p := Point::Create(3, 4);
    PrintInt(p.distance());  // 25
    return 0;
}

External Functions

Integration with system libraries like libc.

With extern C functions can be included. This is useful for system calls or using libraries like libc. The ... enables varargs.

extern.lyx
extern fn printf(fmt: pchar, ...): int64;
extern fn exit(code: int64): void;

fn main(argc: int64, argv: pchar): int64 {
    if argc < 2 {
        printf("Usage: %s <name>\n", argv[0]);
        exit(1);
    }
    printf("Hello, %s!\n", argv[1]);
    return 0;
}

SIMD Arrays

Parallel array operations for maximum performance.

Parallel arrays use SIMD (Single Instruction Multiple Data) for maximum performance. Operations are executed on multiple data simultaneously.

simd.lyx
import std.math;

fn main(): int64 {
    // Create parallel array of 1000 f64 elements
    var arr: parallel Array<f64>;
    arr.size := 1000;
    
    // Fill with values
    for i := 0; i < 1000; i++ {
        arr[i] := i as f64;
    }
    
    // SIMD operations - processes multiple elements at once
    arr := arr * 2.0;  // Multiply all by 2
    arr := arr + 1.0;  // Add 1 to all
    
    PrintInt(arr[0] as int64);  // 1
    return 0;
}

OOP - Classes and Inheritance

Full object-oriented programming with classes, constructors and inheritance.

Lyx supports full OOP with classes, inheritance, constructors and destructors. class extends enables inheritance, new creates objects on the heap.

class.lyx
// Basisklasse
type Animal = class {
    name: pchar;
    
    fn Create(n: pchar) {
        self.name := n;
    }
    
    fn speak(self: Animal) {
        PrintStr("Some sound\n");
    }
};

// Abgeleitete Klasse mit Vererbung
type Dog = class extends Animal {
    breed: pchar;
    
    fn Create(n, b: pchar) {
        super.Create(n);
        self.breed := b;
    }
    
    fn speak(self: Dog) {
        PrintStr("Woof! Woof!\n");
    }
};

fn main(): int64 {
    // Objekte mit new erstellen (Heap-Allokation)
    var dog := new Dog("Buddy", "Labrador");
    
    dog.speak();      // Output: Woof! Woof!
    PrintStr(dog.name);
    PrintStr(" is a ");
    PrintStr(dog.breed);
    PrintStr("\n");
    
    // Speicher freigeben
    dispose dog;
    
    return 0;
}

Arrays

Static and dynamic arrays with push, sort, and more.

Arrays in Lyx can be static (fixed size) or dynamic (with push/pop). Use .size to get the length and .sort() to sort elements.

arrays.lyx
fn main(): int64 {
    // Statisches Array mit fester Größe
    var nums: Array<int64>;
    nums.size := 5;
    
    // Array befüllen
    for i := 0; i < 5; i++ {
        nums[i] := i * 10;
    }
    
    // Array durchlaufen
    for i := 0; i < nums.size; i++ {
        PrintInt(nums[i]);
        PrintStr(" ");
    }
    PrintStr("\n");  // 0 10 20 30 40
    
    // Dynamisches Array
    var dynArr: Array<pchar>;
    dynArr.push("apple");
    dynArr.push("banana");
    dynArr.push("cherry");
    
    for i := 0; i < dynArr.size; i++ {
        PrintStr(dynArr[i]);
        PrintStr(" ");
    }
    PrintStr("\n");  // apple banana cherry
    
    // Array sortieren
    var sorted: Array<int64>;
    sorted.push(3);
    sorted.push(1);
    sorted.push(4);
    sorted.push(1);
    sorted.push(5);
    sorted.sort();
    
    for i := 0; i < sorted.size; i++ {
        PrintInt(sorted[i]);
    }
    PrintStr("\n");  // 11345
    
    return 0;
}

Maps / Dictionaries

Key-value stores for efficient data lookup.

Maps provide key-value storage. Access values with map[key], check existence with contains(), and iterate over keys with .keys().

maps.lyx
fn main(): int64 {
    // Map (Key-Value Store)
    var ages: Map<pchar, int64>;
    
    // Schlüssel-Wert-Paare hinzufügen
    ages["Alice"] := 30;
    ages["Bob"] := 25;
    ages["Charlie"] := 35;
    
    // Werte abrufen
    PrintStr("Alice ist ");
    PrintInt(ages["Alice"]);
    PrintStr(" Jahre alt.\n");
    
    // Prüfen ob Schlüssel existiert
    if ages.contains("Bob") {
        PrintStr("Bob gefunden!\n");
    }
    
    // Map iterieren
    var keys := ages.keys();
    for i := 0; i < keys.size; i++ {
        PrintStr(keys[i]);
        PrintStr(": ");
        PrintInt(ages[keys[i]]);
        PrintStr("\n");
    }
    
    // Wert entfernen
    ages.remove("Bob");
    
    // Map mit verschiedenen Typen
    var mixed: Map<pchar, void>;
    mixed["name"] := "Lyx";
    mixed["version"] := 1;
    mixed["active"] := true;
    
    return 0;
}