39 lines
1.7 KiB
Plaintext
39 lines
1.7 KiB
Plaintext
import std::io;
|
|
type meters = f64;
|
|
def show(n: int): string { return $"int {n}"; }
|
|
def show(n: f64): string { return $"float {n}"; }
|
|
def show(m: meters): string { return $"{m} m"; }
|
|
def show(text: span<u8>): string { return $"text {text}"; }
|
|
def show<T>(items: T[2]): string { return $"{items.length} items"; }
|
|
def show(a: int, b: int): string { return $"pair {a} {b}"; }
|
|
struct vec { var x: f64; var y: f64; }
|
|
def vec::scale(&mut this, by: f64): void { this.x *= by; this.y *= by; }
|
|
def vec::scale(&mut this, by: vec): void { this.x *= by.x; this.y *= by.y; }
|
|
def vec::of(n: f64): vec { return vec{ n, n }; }
|
|
def vec::of(x: f64, y: f64): vec { return vec{ x, y }; }
|
|
struct stack<T> { var items: T[4]; var count: u64; }
|
|
def stack<T>::push(&mut this, value: T): void { this.items[this.count] = value; this.count++; }
|
|
def stack<T>::push(&mut this, values: T[2]): void { for v in values { this.push(v); } }
|
|
trait Named { def name(&this): string; }
|
|
struct dog: Named { val n: span<u8>; }
|
|
def dog::name(&this, prefix: span<u8>): string { return $"{prefix}{this.n}"; }
|
|
def dog::name(&this): string { return this.n; }
|
|
def greet(x: &Named): string { return $"hi {x.name()}"; }
|
|
def apply(f: def(int): string, n: int): string { return f(n); }
|
|
def main(): void {
|
|
val m: meters = 3;
|
|
val small: i32 = 7;
|
|
val text := "abc";
|
|
val sir := "sir ";
|
|
println($"{show(1)}, {show(1.5)}, {show(m)}, {show(text)}, {show([1, 2])}, {show(2, 3)}, {show(small)}");
|
|
var v := vec::of(2);
|
|
v.scale(3);
|
|
v.scale(vec::of(1, 2));
|
|
var s := stack::<int>{ [0, 0, 0, 0], 0 };
|
|
s.push(1);
|
|
s.push([2, 3]);
|
|
val d := dog{ "rex" };
|
|
val f: def(f64): string = show;
|
|
println($"{v.x} {v.y} {s.items} {s.count} {greet(d)} {d.name(sir)} {f(2)} {apply(show, 5)}");
|
|
}
|