62 lines
1.8 KiB
Plaintext
62 lines
1.8 KiB
Plaintext
# A program with no imports, which reads `core` alone, and names its types and
|
|||
|
|
# functions with nothing before them: `vector`, `box`, and `function`, the types
|
||
|
|
# the syntax makes, traits, lanes, and atomics. It writes through `system`, by its
|
||
|
|
# path, which reads that too.
|
||
|
|
|
||
|
|
struct tally : equatable {
|
||
|
|
var count: i64;
|
||
|
|
}
|
||
|
|
|
||
|
|
def tally::equals(&this, other: &tally): bool {
|
||
|
|
return this.count == other.count;
|
||
|
|
}
|
||
|
|
|
||
|
|
def say(text: strview): void {
|
||
|
|
system::fd::stdout.put(text);
|
||
|
|
system::fd::stdout.put("\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
def total<T>(values: span<T>): T where T: integer {
|
||
|
|
var sum: T = 0;
|
||
|
|
for &each in values {
|
||
|
|
sum = sum + each;
|
||
|
|
}
|
||
|
|
return sum;
|
||
|
|
}
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
var numbers := vector::<i64>::new();
|
||
|
|
for i in 0..5 {
|
||
|
|
numbers.append(i * i);
|
||
|
|
}
|
||
|
|
val view: span<i64> = numbers.span;
|
||
|
|
say($"vector {numbers.length} total {total(view)} first {view[0]} last {view[4]}");
|
||
|
|
|
||
|
|
val boxed := box::<i64>::new(7);
|
||
|
|
say($"owner {boxed.get()}");
|
||
|
|
|
||
|
|
val doubled := function::<def(i64): i64>::new(def(x: i64): i64 { return x * 2; });
|
||
|
|
val both := pair::<i64, strview> { first = doubled(21), second = "pair" };
|
||
|
|
say($"function {both.first} {both.second}");
|
||
|
|
|
||
|
|
var found: maybe<i64> = nothing {};
|
||
|
|
if view.last is some(&at) {
|
||
|
|
found = at;
|
||
|
|
}
|
||
|
|
val parsed: result<i64, strview> = 12;
|
||
|
|
if found is some(value) && parsed is value(twelve) {
|
||
|
|
say($"maybe {value} result {twelve}");
|
||
|
|
}
|
||
|
|
|
||
|
|
var text := string::from("core", nothing {});
|
||
|
|
text.append(" alone");
|
||
|
|
val words: strview = text.span;
|
||
|
|
say($"string {text} {words.length} {words.upper()} {text.span == words}");
|
||
|
|
|
||
|
|
var lanes := u32x4::splat(3) + u32x4::of([1, 2, 3, 4]);
|
||
|
|
say($"lanes {lanes.sum()} {lanes.smallest()} {lanes.largest()} {count_ones(255u)}");
|
||
|
|
|
||
|
|
var counter := atomic::<u64>::new(1);
|
||
|
|
_ = counter.fetch_add(4);
|
||
|
|
say($"atomic {counter.load()} equal {tally { count = 1 } == tally { count = 1 }}");
|
||
|
|
}
|