Lyx Code Examples
Practical code samples to learn the Lyx programming language. From basics to OOP.
1. Hello World
The simplest possible Lyx program.
HELLO.LYX
fn main(): int64 { PrintStr("Hello Lyx\n"); return 0; }
2. Variables & Storage Classes
Four storage classes control mutability and lifetime: var, let, co, con.
VARIABLES.LYX
fn main(): int64 { // var - mutable var x: int64 := 10; x := x + 1; // allowed // let - immutable after init let y: int64 := 20; // y := 0; // ERROR: cannot assign to let // co - runtime constant (readonly) co z: int64 := x + y; // con - compile-time constant (no storage) con LIMIT: int64 := 100; PrintInt(x); // 11 PrintStr("\n"); return 0; }
3. Arrays
Stack-allocated arrays with literals, indexing, and assignment.
ARRAYS.LYX
fn main(): int64 { // Array literal var arr: array := [10, 20, 30]; // Read element var first: int64 := arr[0]; // 10 // Assign element arr[0] := 100; // Dynamic index var i: int64 := 1; var second: int64 := arr[i]; // 20 PrintInt(arr[0]); // 100 PrintStr("\n"); return 0; }
4. Structs
User-defined types with struct literals and field access.
STRUCTS.LYX
type Point = struct { x: int64; y: int64; }; fn main(): int64 { // Struct literal with field initialization var p: Point := Point { x: 10, y: 20 }; // Field access PrintInt(p.x); // 10 PrintStr("\n"); // Field assignment p.x := 42; PrintInt(p.x); // 42 PrintStr("\n"); return 0; }
5. Struct Methods
Structs can have instance methods with self and static methods.
METHODS.LYX
type Counter = struct { count: int64; // Instance method with self fn increment(): int64 { self.count := self.count + 1; return self.count; } // Static method - no self, called as Counter.get() static fn zero(): Self { return Counter { count: 0 }; } }; fn main(): int64 { var c: Counter := Counter { count: 10 }; c.increment(); PrintInt(c.count); // 11 PrintStr("\n"); return 0; }
6. OOP — Classes
Full OOP with inheritance, constructors, and destructors.
OOP.LYX
// Base class type Animal = class { name: pchar; fn speak() { PrintStr("Some sound\n"); } }; // Derived class with inheritance type Dog = class extends Animal { breed: pchar; // Constructor fn Create(n: pchar, b: pchar) { self.name := n; self.breed := b; } // Override method fn speak() { PrintStr("Woof!\n"); } // Destructor fn Destroy() { PrintStr("Dog destroyed\n"); } }; fn main(): int64 { // Heap allocation with constructor var d: Dog := new Dog("Buddy", "Labrador"); d.speak(); // "Woof!" dispose d; // calls Destroy() and frees memory return 0; }
7. Pipe Operator
Chain functions with readable left-to-right data flow using |>.
PIPE.LYX
fn double(x: int64): int64 { return x * 2; } fn addOne(x: int64): int64 { return x + 1; } fn main(): int64 { var x: int64 := 5; // Pipe: readable left-to-right var result: int64 := x |> double() |> addOne(); // Equivalent to: addOne(double(x)) = 11 PrintInt(result); // 11 PrintStr("\n"); return 0; }
8. External Functions
Integrate C libraries with extern fn and varargs.
EXTERN.LYX
// Declare external functions extern fn malloc(size: int64): pchar; extern fn free(ptr: pchar): void; extern fn printf(format: pchar, ...): int64; fn main(): int64 { // Allocate memory let ptr: pchar := malloc(64); // Printf with varargs printf("Hello %s, number: %d\n", "World", 42); // Free memory free(ptr); return 0; }
9. Control Flow
if/else, while loops, and switch/case statements.
CONTROL.LYX
fn classify(n: int64): int64 { if (n > 0) { PrintStr("positive\n"); } else { if (n < 0) { PrintStr("negative\n"); } else { PrintStr("zero\n"); } } // Switch/case switch (n % 3) { case 0: PrintStr("divisible by 3\n"); case 1: PrintStr("remainder 1\n"); default: PrintStr("remainder 2\n"); } return 0; } fn main(): int64 { var i: int64 := 0; while (i < 3) { classify(i); i := i + 1; } return 0; }
10. Standard Library
Use the built-in standard library modules.
STDLIB.LYX
// Import standard library modules import std.math; import std.string; fn main(): int64 { // Math functions var x: int64 := -42; PrintInt(Abs64(x)); // 42 PrintStr("\n"); // String functions var s: pchar := "Hello World"; PrintInt(StrLength(s)); // 11 PrintStr("\n"); // Int to string conversion var numStr: pchar := IntToStr(12345); PrintStr(numStr); PrintStr("\n"); return 0; }