Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1 @@
|
||||
read past its mark
|
||||
@@ -0,0 +1,2 @@
|
||||
import std::io;
|
||||
def main(): void { println("read past its mark"); }
|
||||
@@ -0,0 +1 @@
|
||||
1 2 9 4 5 8 6 true 6
|
||||
@@ -0,0 +1,21 @@
|
||||
import std::io;
|
||||
struct holder : owning { var data: *mut u8; var tag: int; }
|
||||
var closed: int = 0;
|
||||
def holder::of(tag: int): holder { return holder { data = raw_allocate(8), tag = tag }; }
|
||||
def holder::close(&mut this): void { if this.data != null { closed++; raw_release(this.data); this.data = null; } }
|
||||
def holder::taken(src: &mut holder): holder { var made := holder { data = src.data, tag = src.tag }; src.data = null; return made; }
|
||||
def reads(h: holder): int { return h.tag; }
|
||||
def takes(h: holder): int { val held := holder::taken(&mut h); return held.tag; }
|
||||
def swaps(h: holder): int { h = holder::of(9); return h.tag; }
|
||||
def hands(h: holder): int { return reads(holder::taken(&mut h)); }
|
||||
def gives(h: holder): holder { return h; }
|
||||
def counts(n: int): int { n++; n = n * 2; return n; }
|
||||
def main(): void {
|
||||
print($"{reads(holder::of(1))} {takes(holder::of(2))} ");
|
||||
print($"{swaps(holder::of(3))} {hands(holder::of(4))} ");
|
||||
val kept := gives(holder::of(5));
|
||||
print($"{kept.tag} {counts(3)} ");
|
||||
var mine := holder::of(6);
|
||||
print($"{takes(holder::taken(&mut mine))} {mine.data == null} ");
|
||||
print($"{closed}\n");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
number 7 | text hi
|
||||
many 3 [1, 2, 3] | none
|
||||
held{text = bad} held{none = empty{}}
|
||||
10 z
|
||||
@@ -0,0 +1,35 @@
|
||||
import std::io;
|
||||
struct empty {}
|
||||
union held { var none: empty; var number: int; var text: span<u8>; var many: span<int>; }
|
||||
def read(h: held): string {
|
||||
if h is number(n) { return $"number {n}"; }
|
||||
if h is text(t) { return $"text {t}"; }
|
||||
if h is many(xs) { return $"many {xs.length} {xs}"; } else { return "none"; }
|
||||
}
|
||||
def fail_at(n: int): held { return held{ text = "bad" } if n < 0; return empty{}; }
|
||||
def forward(n: int): held {
|
||||
return why if fail_at(n) is text(why);
|
||||
return empty{};
|
||||
}
|
||||
def total(xs: span<held>): string {
|
||||
var sum := 0;
|
||||
var seen := string::new(nothing {});
|
||||
var i := 0 as u64;
|
||||
while i < xs.length {
|
||||
val each := xs[i];
|
||||
i++;
|
||||
if each is number(n) { sum += n; }
|
||||
if each is text(t) { seen.append(t); }
|
||||
}
|
||||
return $"{sum} {seen}";
|
||||
}
|
||||
def main(): void {
|
||||
val words := held{ text = "hi" };
|
||||
val one := held{ text = "z" };
|
||||
var ns: int[3] = [1, 2, 3];
|
||||
println($"{read(held{ number = 7 })} | {read(words)}");
|
||||
println($"{read(held{ many = span::<int>::over((&ns) as *int, 3) })} | {read(empty{})}");
|
||||
println($"{forward(-2)} {forward(2)}");
|
||||
var some: held[4] = [held{ number = 4 }, empty{}, one, held{ number = 6 }];
|
||||
println(total(span::<held>::over((&some) as *held, 4)));
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
same same same
|
||||
@@ -0,0 +1,16 @@
|
||||
import std::io;
|
||||
trait same<T> { def same(&this, other: &T): bool; }
|
||||
struct pt { var x: i64; }
|
||||
def pt::same(&this, other: &pt): bool { return this.x == other.x; }
|
||||
type point = pt;
|
||||
type spot = point;
|
||||
def check<T>(a: &T, b: &T): string {
|
||||
if const T is same<T> { return a.same(b) ? "same" : "differ"; }
|
||||
return "unknown";
|
||||
}
|
||||
def main(): void {
|
||||
val a: point = pt { 1 };
|
||||
val b: point = pt { 1 };
|
||||
val c: spot = pt { 2 };
|
||||
println($"{check::<pt>(&a, &b)} {check::<point>(&a, &b)} {check::<spot>(&c, &c)}");
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
true true true true 7 3 true true true
|
||||
released
|
||||
@@ -0,0 +1,27 @@
|
||||
import std::io;
|
||||
def aligned(block: *mut u8): bool {
|
||||
return block != null && (block as u64) % 16 == 0;
|
||||
}
|
||||
def main(): void {
|
||||
var small := raw_allocate(10);
|
||||
var class := raw_allocate(300);
|
||||
var paged := raw_allocate(5000);
|
||||
var odd := raw_allocate(1);
|
||||
print($"{aligned(small)} {aligned(class)} {aligned(paged)} {aligned(odd)} ");
|
||||
small[9] = 7;
|
||||
# What a block grows by may hold anything, so it is written before it is read.
|
||||
var grown := raw_resize(small, 40);
|
||||
grown[39] = 3;
|
||||
print($"{grown[9]} {grown[39]} {aligned(grown)} ");
|
||||
var moved := raw_resize(paged, 9000);
|
||||
print($"{aligned(moved)} ");
|
||||
var fresh := raw_resize(null, 1);
|
||||
println($"{aligned(fresh)}");
|
||||
raw_release(grown);
|
||||
raw_release(class);
|
||||
raw_release(odd);
|
||||
raw_release(moved);
|
||||
raw_release(fresh);
|
||||
raw_release(null);
|
||||
println("released");
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
before
|
||||
--- exit 1
|
||||
sakura: runtime error: Program aborted.
|
||||
at inner (tests/programs/an_abort_reports_where_it_was_1.sakura:3:44)
|
||||
at main (tests/programs/an_abort_reports_where_it_was_1.sakura:4:25)
|
||||
@@ -0,0 +1,4 @@
|
||||
import std::io;
|
||||
import system;
|
||||
def inner(): void { print("before "); abort(); }
|
||||
def main(): void { inner(); println("after"); }
|
||||
@@ -0,0 +1,2 @@
|
||||
[1, 9] [1, 2] 12 10 9 true
|
||||
[[1, 0], [2, 9]]
|
||||
@@ -0,0 +1,19 @@
|
||||
import std::io;
|
||||
struct point { var x: int; }
|
||||
def point::move(&mut this, by: int): void { this.x += by; }
|
||||
def grow(xs: &mut int[2]): void { xs[1] = 9; }
|
||||
def bump(p: &mut point): void { p.x++; }
|
||||
def total(xs: &int[2]): int { var t := 0; for x in xs { t += x; } return t; }
|
||||
def same<T>(x: &T): T { return x; }
|
||||
def main(): void {
|
||||
var xs := [1, 2];
|
||||
val alias := xs;
|
||||
grow(&mut xs);
|
||||
var p := point{ 1 };
|
||||
bump(p);
|
||||
p.move(same(10));
|
||||
println($"{xs} {alias} {p.x} {total(&xs)} {total(&[4, 5])} {same(true)}");
|
||||
var nested := [[1, 0], [2, 0]];
|
||||
grow(&mut nested[1]);
|
||||
println($"{nested}");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
else -2 -0.25 true 0
|
||||
2 0 3 b
|
||||
11 6 0
|
||||
@@ -0,0 +1,16 @@
|
||||
import std::io;
|
||||
enum color { red, green, blue }
|
||||
const words: span<span<u8>> = ["if", "else"];
|
||||
const numbers: span<i32> = [1, -2, 3];
|
||||
const reals: span<f32> = [1.5, -0.25, 2];
|
||||
const colors: span<color> = [color::blue, color::red];
|
||||
const nested: span<span<u8>> = [[1, 2], [], [3]];
|
||||
const none: span<i64> = [];
|
||||
def sum(xs: span<i64>): i64 { var t: i64 = 0; for x in xs { t += x; } return t; }
|
||||
def main(): void {
|
||||
val local: span<span<u8>> = ["a", "b"];
|
||||
val n: i64 = 5;
|
||||
println($"{words[1]} {numbers[1]} {reals[1]} {colors[0] == color::blue} {none.length}");
|
||||
println($"{nested[0][1]} {nested[1].length} {nested[2][0]} {local[1]}");
|
||||
println($"{sum([n, n + 1])} {sum([1, 2, 3])} {sum([])}");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
2
|
||||
close 2
|
||||
close 1
|
||||
done
|
||||
@@ -0,0 +1,8 @@
|
||||
import std::io;
|
||||
struct noisy : owning { var n: i64; }
|
||||
def noisy::close(&mut this): void { println($"close {this.n}"); }
|
||||
def count(xs: span<noisy>): u64 { return xs.length; }
|
||||
def main(): void {
|
||||
println($"{count([noisy{ n = 1 }, noisy{ n = 2 }])}");
|
||||
println("done");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
u8: 100 44 200 200 236 36 36 200 100
|
||||
wrote over 100, found 200
|
||||
i8: -100 -10 90 90 -34 -124 -124 90 -100
|
||||
wrote over -100, found 90
|
||||
u16: 32767 32751 65520 65520 65535 15 15 65520 32767
|
||||
wrote over 32767, found 65520
|
||||
i16: -30000 0 30000 30000 -16 -30016 -30016 30000 -30000
|
||||
wrote over -30000, found 30000
|
||||
u32: 5 3 4294967294 4294967294 4294967295 1 1 4294967294 5
|
||||
wrote over 5, found 4294967294
|
||||
i32: -2147483647 0 2147483647 2147483647 -1 -2147483648 -2147483648 2147483647 -2147483647
|
||||
wrote over -2147483647, found 2147483647
|
||||
u64: 1 0 18446744073709551615 18446744073709551615 18446744073709551615 0 0 18446744073709551615 1
|
||||
wrote over 1, found 18446744073709551615
|
||||
i64: -9223372036854775807 0 9223372036854775807 9223372036854775807 -1 -9223372036854775808 -9223372036854775808 9223372036854775807 -9223372036854775807
|
||||
wrote over -9223372036854775807, found 9223372036854775807
|
||||
bool: false true true false
|
||||
enum: true true
|
||||
pointer: 30
|
||||
struct: 45 255
|
||||
shared: true 200000 600000
|
||||
@@ -0,0 +1,171 @@
|
||||
import std::io;
|
||||
import std::process;
|
||||
import system;
|
||||
|
||||
extern import "kernel32.dll" when os == windows;
|
||||
|
||||
# Atomics of every width and kind, their operations giving what they held, and
|
||||
# two processes adding to a page they share, which loses nothing.
|
||||
|
||||
def show<T>(name: strview, a: &mut atomic<T>, low: T, high: T): void where T: integer, lockfree {
|
||||
a.store(low);
|
||||
val added := a.fetch_add(high);
|
||||
val subtracted := a.fetch_sub(low, order::acq_rel);
|
||||
val anded := a.fetch_and(high, order::relaxed);
|
||||
val ored := a.fetch_or(low);
|
||||
val xored := a.fetch_xor(high, order::release);
|
||||
val least := a.fetch_min(low);
|
||||
val most := a.fetch_max(high, order::acquire);
|
||||
val swapped := a.exchange(low);
|
||||
io::println($"{name}: {added} {subtracted} {anded} {ored} {xored} {least} {most} {swapped} {a.load()}");
|
||||
match a.compare_exchange(low, high) {
|
||||
value(found) => io::print($" wrote over {found}");
|
||||
error(found) => io::print($" found {found}");
|
||||
}
|
||||
match a.compare_exchange(low, high, order::release, order::relaxed) {
|
||||
value(found) => io::println($", wrote over {found}");
|
||||
error(found) => io::println($", found {found}");
|
||||
}
|
||||
}
|
||||
|
||||
enum state { idle, busy, done }
|
||||
|
||||
struct counters {
|
||||
var hits: atomic<u64>;
|
||||
var flags: atomic<u8>;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
var a8 := atomic::<u8>::new(0);
|
||||
var b8 := atomic::<i8>::new(0);
|
||||
var a16 := atomic::<u16>::new(0);
|
||||
var b16 := atomic::<i16>::new(0);
|
||||
var a32 := atomic::<u32>::new(0);
|
||||
var b32 := atomic::<i32>::new(0);
|
||||
var a64 := atomic::<u64>::new(0);
|
||||
var b64 := atomic::<i64>::new(0);
|
||||
show::<u8>("u8", &mut a8, 100, 200);
|
||||
show::<i8>("i8", &mut b8, -100, 90);
|
||||
show::<u16>("u16", &mut a16, 0x7FFF, 0xFFF0);
|
||||
show::<i16>("i16", &mut b16, -30000, 30000);
|
||||
show::<u32>("u32", &mut a32, 5, 0xFFFFFFFE);
|
||||
show::<i32>("i32", &mut b32, -2147483647, 2147483647);
|
||||
show::<u64>("u64", &mut a64, 1, 0xFFFFFFFFFFFFFFFF);
|
||||
show::<i64>("i64", &mut b64, -9223372036854775807, 9223372036854775807);
|
||||
|
||||
var flag := atomic::<bool>::new(false);
|
||||
val was := flag.exchange(true);
|
||||
io::println($"bool: {was} {flag.load()} {flag.compare_exchange(true, false) is value} {flag.load()}");
|
||||
|
||||
var now := atomic::<state>::new(state::idle);
|
||||
now.store(state::busy, order::release);
|
||||
val before := now.exchange(state::done);
|
||||
io::println($"enum: {before == state::busy} {now.load(order::acquire) == state::done}");
|
||||
|
||||
var words: u64[4] = [10, 20, 30, 40];
|
||||
var at := atomic::<*mut u64>::new(&mut words[0] as *mut u64);
|
||||
match at.compare_exchange(&mut words[0] as *mut u64, &mut words[2] as *mut u64) {
|
||||
value(_) => io::println($"pointer: {at.load()[0]}");
|
||||
error(_) => io::println("pointer: refused");
|
||||
}
|
||||
|
||||
var held := counters { hits = atomic::<u64>::new(0), flags = atomic::<u8>::new(0) };
|
||||
for i in 0..10u {
|
||||
_ = held.hits.fetch_add(i);
|
||||
_ = held.flags.fetch_or((1 << (i % 8)) as u8);
|
||||
}
|
||||
fence(order::seq_cst);
|
||||
io::println($"struct: {held.hits.load()} {held.flags.load()}");
|
||||
|
||||
# A page both processes keep, rather than a copy each.
|
||||
val page := shared_page();
|
||||
val sum := page as *mut atomic<u64>;
|
||||
val tries := (page + 8) as *mut atomic<u32>;
|
||||
val child := copy();
|
||||
for _ in 0..100000 {
|
||||
_ = sum[0].fetch_add(1);
|
||||
var seen := tries[0].load(order::relaxed);
|
||||
while true {
|
||||
match tries[0].compare_exchange_weak(seen, seen + 3, order::acq_rel, order::relaxed) {
|
||||
value(_) => break;
|
||||
error(found) => seen = found;
|
||||
}
|
||||
}
|
||||
}
|
||||
if child == 0 {
|
||||
exit(0);
|
||||
}
|
||||
var status: process_status;
|
||||
_ = wait(child, &mut status);
|
||||
io::println($"shared: {status.exited} {sum[0].load()} {tries[0].load()}");
|
||||
}
|
||||
|
||||
# The page both processes add to, and the copy of the program adding with this
|
||||
# one, whose number `copy` gives, or zero in the copy. Linux keeps a page mapped
|
||||
# shared in the copy `fork` makes. Windows cannot fork, so there the copy is the
|
||||
# program started again, writing nothing, which maps the page by the name it is
|
||||
# started with.
|
||||
when os == linux {
|
||||
def shared_page(): u64 {
|
||||
return system::linux::syscall(9, 0, 4096, 3, 0x21, 0xFFFFFFFFFFFFFFFF, 0) as u64;
|
||||
}
|
||||
|
||||
def copy(): i64 {
|
||||
return fork();
|
||||
}
|
||||
}
|
||||
else {
|
||||
# The variable the copy is started with, holding the name of the page.
|
||||
val page_variable: strview = "SAKURA_ATOMICS_PAGE";
|
||||
|
||||
# The page, as this program mapped it.
|
||||
var mapped: u64 = 0;
|
||||
|
||||
# The name of the page: the one the copy was given, or one of this program's
|
||||
# own.
|
||||
def page_name(): string {
|
||||
if process::variable(page_variable) is some(given) {
|
||||
return string::from(given, nothing {});
|
||||
}
|
||||
return $"sakura-atomics-{getpid()}";
|
||||
}
|
||||
|
||||
def shared_page(): u64 {
|
||||
val name := page_name();
|
||||
var wide: u16[64];
|
||||
for i in 0..name.length {
|
||||
wide[i] = name[i] as u16;
|
||||
}
|
||||
val mapping := pages::CreateFileMappingW(0xFFFFFFFFFFFFFFFF, null, 0x04, 0, 4096, (&wide) as *u16);
|
||||
mapped = pages::MapViewOfFile(mapping, 0x02, 0, 0, 4096);
|
||||
return mapped;
|
||||
}
|
||||
|
||||
# The copy is slow to start, so it says it has mapped the page, in the word
|
||||
# after what the two add to, and the program waits for that, for ten seconds
|
||||
# at most, so that the two add at once, as after `fork`.
|
||||
def copy(): i64 {
|
||||
val ready := (mapped + 12) as *mut atomic<u32>;
|
||||
if process::variable(page_variable) is some(_) {
|
||||
ready[0].store(1);
|
||||
return 0;
|
||||
}
|
||||
var device := path::parse(null_device) else (_) { return -1; };
|
||||
var quiet := io::file::open(&device, open_flags::write) else (_) { return -1; };
|
||||
val started := respawn($"{page_variable}={page_name()}".view(), fd::stdin, quiet.handle, fd::stderr);
|
||||
val until := clock(clock_kind::steady) + 10000000000;
|
||||
while started > 0 && ready[0].load() == 0 && clock(clock_kind::steady) < until {
|
||||
_ = sleep(1000000);
|
||||
}
|
||||
return started;
|
||||
}
|
||||
}
|
||||
|
||||
# What Windows gives of memory that processes share: a mapping of the system's
|
||||
# own file of pages, by a name, and a view of it.
|
||||
extern module pages when os == windows;
|
||||
|
||||
pub def CreateFileMappingW(
|
||||
file: u64, security: *mut u8, protection: u32, size_high: u32, size_low: u32, name: *u16
|
||||
): u64;
|
||||
pub def MapViewOfFile(mapping: u64, access: u32, offset_high: u32, offset_low: u32, size: u64): u64;
|
||||
@@ -0,0 +1 @@
|
||||
7 1
|
||||
@@ -0,0 +1,11 @@
|
||||
import std::io;
|
||||
struct serial : attribute { val name: span<u8>; val aliases: span<u8>[2]; }
|
||||
struct weight : attribute { val value: i64 = 1; }
|
||||
const def double(x: i64): i64 { return x * 2; }
|
||||
@serial{ name = "point", aliases = ["p", "pt"] }
|
||||
struct point { @weight{ value = double(21) } var x: i64; @weight var y: i64; }
|
||||
enum axis { @weight x, y }
|
||||
@weight def sum(p: point): i64 { return p.x + p.y; }
|
||||
def main(): void {
|
||||
println($"{sum(point{ 3, 4 })} {axis::y as i64}");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
61440 65520 4080 3855
|
||||
986880 3855
|
||||
30720
|
||||
240
|
||||
8 15 9 4294967283 48 3
|
||||
8
|
||||
@@ -0,0 +1,36 @@
|
||||
import std::io;
|
||||
|
||||
# A struct has the bitwise operators and shifts by the methods that give them,
|
||||
# `bit_and`, `bit_or`, `bit_xor`, `bit_not`, `shift_left`, and `shift_right`, as it
|
||||
# has arithmetic, their compound assignments too, and a bound asking for them
|
||||
# gives a type parameter the operators. Integers give the methods as well.
|
||||
|
||||
struct bits { var word: u64; }
|
||||
def bits::bit_and(&this, other: &bits): bits { return bits { word = this.word & other.word }; }
|
||||
def bits::bit_or(&this, other: &bits): bits { return bits { word = this.word | other.word }; }
|
||||
def bits::bit_xor(&this, other: &bits): bits { return bits { word = this.word ^ other.word }; }
|
||||
def bits::bit_not(&this): bits { return bits { word = ~this.word }; }
|
||||
def bits::shift_left(&this, by: &u32): bits { return bits { word = this.word << by }; }
|
||||
def bits::shift_right(&this, by: &u32): bits { return bits { word = this.word >> by }; }
|
||||
|
||||
trait masks { def bit_and(&this, other: &self): self; def bit_not(&this): self; }
|
||||
|
||||
def clear<T>(a: T, b: T): T where T: masks { return a & ~b; }
|
||||
|
||||
def main(): void {
|
||||
val a := bits { word = 0xF0F0 };
|
||||
val b := bits { word = 0xFF00 };
|
||||
io::println($"{(a & b).word} {(a | b).word} {(a ^ b).word} {(~a).word & 0xFFFF}");
|
||||
io::println($"{(a << 4).word} {(a >> 4).word}");
|
||||
var c := a;
|
||||
c &= b;
|
||||
c |= bits { word = 1 };
|
||||
c <<= 1;
|
||||
c ^= bits { word = 3 };
|
||||
c >>= 2;
|
||||
io::println($"{c.word}");
|
||||
io::println($"{clear(a, b).word}");
|
||||
val x: u32 = 12;
|
||||
io::println($"{x.bit_and(&10)} {x.bit_or(&3)} {x.bit_xor(&5)} {x.bit_not()} {x.shift_left(&2)} {x.shift_right(&2)}");
|
||||
io::println($"{clear(x, 4 as u32)}");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true [true, false, false] true true 1 true false true true true false true false 200 2.3333333333333335 1.5 true 57 [10, 21, 32] 0:10 1:20 2:30
|
||||
@@ -0,0 +1,26 @@
|
||||
import std::io;
|
||||
struct nothing {}
|
||||
union value { var none: nothing; var flag: bool; var number: i64; var real: f64; }
|
||||
def main(): void {
|
||||
val yes: bool = 3 > 2;
|
||||
val flags: bool[3] = [true, !yes, yes && false];
|
||||
val held := value{ flag = true };
|
||||
val three := value{ number = 3 };
|
||||
print($"{yes} {flags} {held.flag} {three is number} {yes as u8} {7 as bool} {0 as bool} {flags == [true, false, false]} ");
|
||||
val xs := [10, 20, 30];
|
||||
var i: i64 = -1;
|
||||
val n: u64 = 3;
|
||||
val small: u8 = 200;
|
||||
val wide: i16 = small;
|
||||
var total: f64 = 7;
|
||||
print($"{i < xs.length} {xs.length > i} {i == n} {n != i} {i >= n} {wide} {total / xs.length} {n * 0.5} {i < 0.5} ");
|
||||
var sum := 0;
|
||||
for k in 0..xs.length { sum += xs[k]; }
|
||||
for k in -2..1 { sum += k; }
|
||||
for k in 3..3 { sum += 1000; }
|
||||
var ys := xs;
|
||||
for at, &mut y in ys { y += at as i64; }
|
||||
var shown := $"";
|
||||
for at, x in xs { shown.append($"{at}:{x} "); }
|
||||
println($"{sum} {ys} {shown}");
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
d1 d1 d1 d1d1 d1true
|
||||
? ? ? .. ?
|
||||
@@ -0,0 +1,35 @@
|
||||
import std::io;
|
||||
trait shows { def show(this: &shows): string; }
|
||||
struct wrap<T> { var v: T; }
|
||||
def wrap<T>::plain(this: &wrap<T>): string {
|
||||
if const T is shows { return this.v.show(); }
|
||||
return "?";
|
||||
}
|
||||
def wrap<T>::inverted(this: &wrap<T>): string {
|
||||
if const T is not shows { return "?"; } else { return this.v.show(); }
|
||||
}
|
||||
def wrap<T>::guarded(this: &wrap<T>): string {
|
||||
if const T is not shows { return "?"; }
|
||||
return this.v.show();
|
||||
}
|
||||
def wrap<T>::looped(this: &wrap<T>, times: int): string {
|
||||
var out := string::new(nothing {});
|
||||
for i in 0..times {
|
||||
if const T is not shows { out.append($"."); continue; }
|
||||
out.append($"{this.v.show()}");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
def wrap<T>::joined(this: &wrap<T>): string {
|
||||
if const !(T is shows && T is equatable) { return "?"; }
|
||||
return $"{this.v.show()}{this.v == this.v}";
|
||||
}
|
||||
struct dot: shows { var n: int; }
|
||||
def dot::show(this: &dot): string { return $"d{this.n}"; }
|
||||
def dot::equals(&this, other: &dot): bool { return this.n == other.n; }
|
||||
def main(): void {
|
||||
val d := wrap{ v = dot{ n = 1 } };
|
||||
val i := wrap{ v = 7 };
|
||||
println($"{d.plain()} {d.inverted()} {d.guarded()} {d.looped(2)} {d.joined()}");
|
||||
println($"{i.plain()} {i.inverted()} {i.guarded()} {i.looped(2)} {i.joined()}");
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
node 1
|
||||
leaf 2
|
||||
leaf 1
|
||||
node 3
|
||||
leaf 4
|
||||
leaf 3
|
||||
hidden 6
|
||||
hidden 5
|
||||
done
|
||||
@@ -0,0 +1,22 @@
|
||||
import std::io;
|
||||
import lib;
|
||||
struct leaf : owning { var n: int; }
|
||||
def leaf::close(&mut this): void { if this.n != 0 { println($"leaf {this.n}"); } }
|
||||
struct node : owning { var a: leaf; var b: leaf; }
|
||||
def node::close(&mut this): void { if this.a.n != 0 { println($"node {this.a.n}"); } }
|
||||
def main(): void {
|
||||
{ var held := box::new(node{ a = leaf{ n = 1 }, b = leaf{ n = 2 } }); }
|
||||
{ var many := vector::<node>::new(); many.append(node{ a = leaf{ n = 3 }, b = leaf{ n = 4 } }); }
|
||||
{ var kept := holder::make(); }
|
||||
println("done");
|
||||
}
|
||||
module lib;
|
||||
struct hidden : owning { var n: int; }
|
||||
def hidden::close(&mut this): void { if this.n != 0 { println($"hidden {this.n}"); } }
|
||||
pub struct holder : owning { var one: box<hidden>; var many: vector<hidden>; }
|
||||
pub def holder::make(): holder {
|
||||
var made := holder{ one = box::new(hidden{ n = 5 }), many = vector::<hidden>::new() };
|
||||
made.many.append(hidden{ n = 6 });
|
||||
return made;
|
||||
}
|
||||
pub def holder::close(&mut this): void {}
|
||||
@@ -0,0 +1 @@
|
||||
true 5 7 true true 1 true true false true true true true 5 1 7
|
||||
@@ -0,0 +1,16 @@
|
||||
import std::io;
|
||||
enum color { red, green }
|
||||
struct pt { var x: i64; }
|
||||
def pt::equals(&this, other: &pt): bool { return this.x == other.x; }
|
||||
def same<T>(a: &T, b: &T): bool where T: equatable { return a == b; }
|
||||
def through(h: &hashable): u64 { return h.hash(); }
|
||||
def main(): void {
|
||||
val a: i64 = 5; val b: u8 = 7; val f: f64 = -0.0; val g: f64 = 0.0;
|
||||
val c := color::green; val p := &a as *i64;
|
||||
val arr: i64[3] = [1, 2, 3]; val other: i64[3] = [1, 2, 3];
|
||||
val pts: pt[2] = [pt{1}, pt{2}]; val more: pt[2] = [pt{1}, pt{3}];
|
||||
print($"{a.equals(&a)} {a.hash()} {b.hash()} {f.hash() == g.hash()} {f == g} {c.hash()} ");
|
||||
print($"{arr.equals(&other)} {arr.hash() == other.hash()} {pts == more} {pts[0] == more[0]} ");
|
||||
print($"{same::<*i64>(&p, &p)} {same::<color>(&c, &c)} {same::<pt[2]>(&pts, &pts)} ");
|
||||
println($"{through(&a)} {through(&c)} {through(&b)}");
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
peek seen
|
||||
ends a
|
||||
close a
|
||||
short b
|
||||
close b
|
||||
close cc
|
||||
2
|
||||
close e
|
||||
end
|
||||
close d
|
||||
close seen
|
||||
@@ -0,0 +1,20 @@
|
||||
import std::io;
|
||||
struct noisy : owning { var name: span<u8>; }
|
||||
def noisy::new(name: span<u8>): noisy { return noisy { name = name }; }
|
||||
def noisy::close(&mut this): void { println($"close {this.name}"); }
|
||||
def ends(n: noisy): void { println($"ends {n.name}"); }
|
||||
def back(n: noisy): noisy { return n; }
|
||||
def later(n: noisy, flag: bool = true): void { if flag { return; } }
|
||||
def main(): void {
|
||||
val short := def(n: noisy): void => println($"short {n.name}");
|
||||
val length := def(n: noisy): u64 => n.name.length;
|
||||
var seen := noisy::new("seen");
|
||||
val peek := def(): void { println($"peek {seen.name}"); };
|
||||
peek();
|
||||
ends(noisy::new("a"));
|
||||
short(noisy::new("b"));
|
||||
println($"{length(noisy::new("cc"))}");
|
||||
var kept := back(noisy::new("d"));
|
||||
later(noisy::new("e"));
|
||||
println("end");
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
close a
|
||||
close b
|
||||
close a
|
||||
close x0
|
||||
close x1
|
||||
close inner
|
||||
close gone
|
||||
close first
|
||||
end
|
||||
close second
|
||||
close m
|
||||
close e1
|
||||
close e0
|
||||
owner body
|
||||
close h
|
||||
close p2
|
||||
close p1
|
||||
@@ -0,0 +1,40 @@
|
||||
import std::io;
|
||||
struct noisy : owning { var name: span<u8>; }
|
||||
def noisy::new(name: span<u8>): noisy { return noisy { name = name }; }
|
||||
def noisy::close(&mut this): void { println($"close {this.name}"); }
|
||||
struct pair { var first: noisy; var second: noisy; }
|
||||
struct owner : owning { var held: noisy; }
|
||||
def owner::close(&mut this): void { println("owner body"); }
|
||||
def early(flag: bool): void {
|
||||
var a := noisy::new("a");
|
||||
if flag { return; }
|
||||
var b := noisy::new("b");
|
||||
}
|
||||
def looped(): void {
|
||||
var i := 0;
|
||||
while i < 3 {
|
||||
var label := $"x{i}";
|
||||
var x := noisy::new(label);
|
||||
i++;
|
||||
if i == 2 { break; }
|
||||
continue;
|
||||
}
|
||||
}
|
||||
def made(): noisy {
|
||||
var m := noisy::new("m");
|
||||
return m;
|
||||
}
|
||||
def main(): void {
|
||||
early(true);
|
||||
early(false);
|
||||
looped();
|
||||
{ var inner := noisy::new("inner"); }
|
||||
var p := pair { first = noisy::new("p1"), second = noisy::new("p2") };
|
||||
var o := owner { held = noisy::new("h") };
|
||||
var many: noisy[2] = [noisy::new("e0"), noisy::new("e1")];
|
||||
noisy::new("gone");
|
||||
var got := made();
|
||||
var over := noisy::new("first");
|
||||
over = noisy::new("second");
|
||||
println("end");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
--- exit 2
|
||||
tests/programs/compile_error_in_a_generic_property_1.sakura:9:39: invalid: A vector of two has no z. (In `z<f32, 2>`)
|
||||
tests/programs/compile_error_in_a_generic_property_1.sakura:14:39: invalid: A vector of fewer than four has no w to write. (In `w<f32, 2>`)
|
||||
@@ -0,0 +1,23 @@
|
||||
import std::io;
|
||||
|
||||
# A `compile_error` in the accessor of a generic type's property is reported
|
||||
# in the instance a value of the type given its arguments reads or writes, as
|
||||
# one in a method called is.
|
||||
struct vec<T, const N: u64> { var items: T[N]; }
|
||||
|
||||
pub val vec<T, const N: u64>::z: T {
|
||||
get { if const N < 3 { compile_error("A vector of two has no z."); } return this.items[2]; }
|
||||
}
|
||||
|
||||
pub var vec<T, const N: u64>::w: T {
|
||||
get => this.items[0];
|
||||
set { if const N < 4 { compile_error("A vector of fewer than four has no w to write."); } this.items[3] = value; }
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
val three := vec::<f32, 3> { items = [1.0, 2.0, 3.0] };
|
||||
println($"{three.z}");
|
||||
var two := vec::<f32, 2> { items = [1.0, 2.0] };
|
||||
println($"{two.z}");
|
||||
two.w = 4.0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
--- exit 2
|
||||
tests/programs/compile_error_says_text_worked_out_while_compiling_1.sakura:7:16: invalid: A constant says what is wrong.
|
||||
@@ -0,0 +1,8 @@
|
||||
import std::io;
|
||||
|
||||
# A `compile_error` given a constant holding text says that text.
|
||||
const said: strview = "A constant says what is wrong.";
|
||||
|
||||
def main(): void {
|
||||
compile_error(said);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
--- exit 2
|
||||
tests/programs/compile_error_says_text_worked_out_while_compiling_2.sakura:12:31: invalid: `b` is a field of `pair`
|
||||
tests/programs/compile_error_says_text_worked_out_while_compiling_2.sakura:7:18: invalid: `u32` takes 4 bytes, and `u8` 1 (In `same_size<u32, u8>`)
|
||||
@@ -0,0 +1,19 @@
|
||||
import std::io;
|
||||
|
||||
# A `compile_error` given interpolated text says what evaluating works out it
|
||||
# says, in a generic's instance, and at the declaration whose `at` it is given.
|
||||
def same_size<A, B>(): u64 {
|
||||
if const size_of::<A>() != size_of::<B>() {
|
||||
compile_error($"`{name_of::<A>()}` takes {size_of::<A>()} bytes, and `{name_of::<B>()}` {size_of::<B>()}");
|
||||
}
|
||||
return size_of::<A>();
|
||||
}
|
||||
|
||||
struct pair { var a: i64; var b: i64; }
|
||||
|
||||
def main(): void {
|
||||
println($"{same_size::<u64, i64>()} {same_size::<u32, u8>()}");
|
||||
for const f in members_of(decl_of::<pair>()) {
|
||||
if const f.name == "b" { compile_error($"`{f.name}` is a field of `{name_of::<pair>()}`", f.at); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
--- exit 2
|
||||
tests/programs/compile_error_waits_for_evaluating_to_choose_its_branch_1.sakura:24:41: invalid: broken
|
||||
tests/programs/compile_error_waits_for_evaluating_to_choose_its_branch_1.sakura:33:43: invalid: no field is b
|
||||
tests/programs/compile_error_waits_for_evaluating_to_choose_its_branch_1.sakura:34:66: invalid: no field is z
|
||||
tests/programs/compile_error_waits_for_evaluating_to_choose_its_branch_1.sakura:15:23: invalid: not the same size (In `same_size<uint, u8>`)
|
||||
@@ -0,0 +1,37 @@
|
||||
import std::io;
|
||||
|
||||
# A `compile_error` in a branch of `if const` that only evaluating decides is
|
||||
# reported once evaluating chooses that branch, as one in a branch the checker
|
||||
# decides is, and never where it chooses another: of a computed constant, of
|
||||
# `size_of` in a generic's instance, and of what a pass of `for const` over a
|
||||
# struct's fields describes.
|
||||
struct outcome { val failed: bool; }
|
||||
def build(n: i64): outcome { return outcome { failed = n > 5 }; }
|
||||
const broken: outcome = build(7);
|
||||
const fine: outcome = build(3);
|
||||
|
||||
def same_size<A, B>(): u64 {
|
||||
if const size_of::<A>() == size_of::<B>() { return size_of::<A>(); }
|
||||
else { compile_error("not the same size"); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
@usage{ on = target::fields }
|
||||
struct mark : attribute { val letter: u8 = 0; }
|
||||
struct marked { @mark{ letter = 'q' } var a: i64; @mark{ letter = 'z' } var b: i64; }
|
||||
|
||||
def main(): void {
|
||||
if const broken.failed { compile_error("broken"); }
|
||||
if const fine.failed { compile_error("fine, so never reported"); }
|
||||
if const broken.failed && !fine.failed {
|
||||
if const fine.failed { compile_error("a branch not taken within one taken"); }
|
||||
} else {
|
||||
compile_error("a branch not taken");
|
||||
}
|
||||
println($"{same_size::<u64, i64>()} {same_size::<u64, u8>()}");
|
||||
for const f in members_of(decl_of::<marked>()) {
|
||||
if const f.name == "b" { compile_error("no field is b"); }
|
||||
if const attached::<mark>(f)[0].letter == 'z' { compile_error("no field is z"); }
|
||||
if const f.name == "c" { compile_error("no field is c, so never reported"); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fine
|
||||
8 1
|
||||
a
|
||||
b
|
||||
@@ -0,0 +1,27 @@
|
||||
import std::io;
|
||||
|
||||
# Where evaluating chooses no branch of `if const` holding a `compile_error`,
|
||||
# the program compiles, and runs the branches chosen.
|
||||
struct outcome { val failed: bool; }
|
||||
def build(n: i64): outcome { return outcome { failed = n > 5 }; }
|
||||
const fine: outcome = build(3);
|
||||
|
||||
def same_size<A, B>(): u64 {
|
||||
if const size_of::<A>() == size_of::<B>() { return size_of::<A>(); }
|
||||
else { compile_error("not the same size"); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
@usage{ on = target::fields }
|
||||
struct mark : attribute { val letter: u8 = 0; }
|
||||
struct marked { @mark{ letter = 'q' } var a: i64; @mark{ letter = 'r' } var b: i64; }
|
||||
|
||||
def main(): void {
|
||||
if const fine.failed { compile_error("fine"); } else { println("fine"); }
|
||||
println($"{same_size::<u64, i64>()} {same_size::<u8, bool>()}");
|
||||
for const f in members_of(decl_of::<marked>()) {
|
||||
if const f.name == "c" { compile_error("no field is c"); }
|
||||
if const attached::<mark>(f)[0].letter == 'z' { compile_error("no field is z"); }
|
||||
println(f.name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
52 made 1 grown 2
|
||||
6 made 0 grown 1
|
||||
8 made 0 grown 1
|
||||
15
|
||||
top
|
||||
more
|
||||
@@ -0,0 +1,47 @@
|
||||
import std::io;
|
||||
|
||||
# A compound assignment calls the method changing its target in place where the
|
||||
# target's type has one taking the value, as `+=` calls `add_assign`, which gives
|
||||
# nothing, and otherwise writes what the operator's method gives over the target,
|
||||
# as `x = x + y` would. So a type owning memory can grow in place.
|
||||
|
||||
struct tally { var total: i64; var made: i64; var grown: i64; }
|
||||
def tally::add(&this, other: &tally): tally { return tally { total = this.total + other.total, made = this.made + 1, grown = this.grown }; }
|
||||
def tally::add(&this, k: &i64): tally { return tally { total = this.total + k, made = this.made + 1, grown = this.grown }; }
|
||||
def tally::add_assign(&mut this, other: &tally): void { this.total += other.total; this.grown++; }
|
||||
def tally::shift_left_assign(&mut this, by: &u32): void { this.total <<= by; this.grown++; }
|
||||
|
||||
struct box { var held: tally; }
|
||||
var box::inside: tally { get => this.held; set => this.held = value; }
|
||||
|
||||
# A type owning memory grows in place, through a property's `get` and `set` too.
|
||||
struct lines { var text: string; }
|
||||
def lines::add_assign(&mut this, more: &strview): void { this.text.append(more); this.text.append("\n"); }
|
||||
struct page { var body: lines; }
|
||||
var page::lines: lines {
|
||||
get => lines { text = copy(&this.body.text) };
|
||||
set => this.body = move(&mut value);
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
var t := tally { total = 1 };
|
||||
t += tally { total = 2 }; # add_assign
|
||||
t += 10 as i64; # no `add_assign` takes an `i64`, so `add`
|
||||
t <<= 2; # shift_left_assign
|
||||
io::println($"{t.total} made {t.made} grown {t.grown}");
|
||||
|
||||
var b := box { held = tally { total = 5 } };
|
||||
b.inside += tally { total = 1 };
|
||||
io::println($"{b.held.total} made {b.held.made} grown {b.held.grown}");
|
||||
|
||||
var all := vector::<tally>::new();
|
||||
all.append(tally { total = 7 });
|
||||
all[0] += tally { total = 1 };
|
||||
io::println($"{all[0].total} made {all[0].made} grown {all[0].grown}");
|
||||
|
||||
var written := lines { text = string::new(nothing {}) };
|
||||
for i in 0..3 { written += "line"; }
|
||||
var p := page { body = lines { text = string::from("top\n", nothing {}) } };
|
||||
p.lines += "more";
|
||||
io::print($"{written.text.length}\n{p.body.text}");
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
0 green shape{circle = 1.5}
|
||||
-100 green shape{count = 1}
|
||||
1996959894 256 755167117 44 0.1 -5
|
||||
narrow 3 wide 4 8 is 8 bits ordered other
|
||||
0 1
|
||||
@@ -0,0 +1,53 @@
|
||||
import std::io;
|
||||
enum color: u8 { red, green = 4, blue }
|
||||
union shape { var circle: f32; var count: i32; }
|
||||
struct entry { var weight: i16; var hue: color; var form: shape; }
|
||||
def make(): entry[2] {
|
||||
var out: entry[2];
|
||||
for i in 0..2 {
|
||||
out[i] = entry{ weight = -(i as i16) * 100, hue = color::green,
|
||||
form = i % 2 == 0 ? shape{ circle = 1.5 } : shape{ count = i as i32 } };
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const entries: entry[2] = make();
|
||||
def crc_table(): u32[256] {
|
||||
var table: u32[256];
|
||||
for i in 0..256 {
|
||||
var c := i as u32;
|
||||
for _ in 0..8 { c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1; }
|
||||
table[i] = c;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
const crc: u32[256] = crc_table();
|
||||
def doubled(): int { return base * 2; }
|
||||
const twice := doubled();
|
||||
const base := 21;
|
||||
struct counter { var n: int; }
|
||||
def counter::bump(&mut this): void { this.n++; }
|
||||
def bumped(): int { var c := counter{ 0 }; c.bump(); c.bump(); return c.n; }
|
||||
trait sized { const bytes: u64; }
|
||||
const i32::bytes: u64 = 4;
|
||||
const i64::bytes: u64 = 8;
|
||||
def show<T>(x: T): string where T: sized {
|
||||
const bits := T::bytes * 8;
|
||||
if const bits > 32 { return $"wide {x}"; } else { return $"narrow {x}"; }
|
||||
}
|
||||
def kind<T>(x: T): string {
|
||||
if const T is integer { return $"{x + 1} is {size_of::<T>() * 8} bits"; }
|
||||
else if const T is ordered { return "ordered"; }
|
||||
else { return "other"; }
|
||||
}
|
||||
const small: f32 = 0.1;
|
||||
const neg: i8 = -5;
|
||||
val copied := crc;
|
||||
def main(): void {
|
||||
for e in entries { println($"{e.weight} {e.hue} {e.form}"); }
|
||||
const local := bumped() + twice;
|
||||
println($"{crc[1]} {crc.length} {copied[255]} {local} {small} {neg}");
|
||||
println($"{show(3 as i32)} {show(4)} {kind(7 as u8)} {kind(2.5)} {kind(entries)}");
|
||||
var mine := crc;
|
||||
mine[0] = 1;
|
||||
println($"{crc[0]} {mine[0]}");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
[4, 3, 2, 1, 0] 13 7
|
||||
@@ -0,0 +1,40 @@
|
||||
import std::io;
|
||||
struct node { var value: int; var next: *mut node; }
|
||||
type block = *mut u8;
|
||||
def countdown(): int[5] {
|
||||
var head: *mut node = null;
|
||||
var blocks: block[5];
|
||||
for i in 0..5 {
|
||||
val made := raw_allocate(size_of::<node>()) as *mut node;
|
||||
(*made).value = i as int;
|
||||
(*made).next = head;
|
||||
head = made;
|
||||
blocks[i] = made as *mut u8;
|
||||
}
|
||||
var out: int[5];
|
||||
var at := head;
|
||||
var k := 0;
|
||||
while at != null { out[k] = (*at).value; k++; at = (*at).next; }
|
||||
for i in 0..5 { raw_release(blocks[i]); }
|
||||
return out;
|
||||
}
|
||||
def total(first: *int, count: u64): int {
|
||||
var sum := 0;
|
||||
for i in 0..count { sum += first[i]; }
|
||||
val last := first + (count - 1);
|
||||
return sum + (last - first) as int;
|
||||
}
|
||||
def summing(): int {
|
||||
var xs: int[4] = [1, 2, 3, 4];
|
||||
return total((&xs) as *int, 4);
|
||||
}
|
||||
def through_local(): int {
|
||||
var x := 5;
|
||||
val p := &mut x as *mut int;
|
||||
p[0] = 7;
|
||||
return x;
|
||||
}
|
||||
const down := countdown();
|
||||
const summed := summing();
|
||||
const written := through_local();
|
||||
def main(): void { println($"{down} {summed} {written}"); }
|
||||
@@ -0,0 +1 @@
|
||||
7000000000 -6000000000 12000000000
|
||||
@@ -0,0 +1,13 @@
|
||||
import std::io;
|
||||
# An operator calling a method still makes a constant while compiling.
|
||||
struct length { val ns: i64; }
|
||||
def length::add(&this, other: &length): length { return length { ns = this.ns + other.ns }; }
|
||||
def length::mul(&this, by: &i64): length { return length { ns = this.ns * by }; }
|
||||
def length::negate(&this): length { return length { ns = -this.ns }; }
|
||||
def seconds(count: i64): length { return length { ns = count * 1000000000 }; }
|
||||
const pause: length = seconds(1) + seconds(2) * 3;
|
||||
const back: length = -pause + seconds(1);
|
||||
const longest: length = seconds(2) * 3 + -seconds(1) + pause;
|
||||
def main(): void {
|
||||
println($"{pause.ns} {back.ns} {longest.ns}");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
add=9 mul=18 sub=3 42 20 9 def mul
|
||||
@@ -0,0 +1,20 @@
|
||||
import std::io;
|
||||
type op = def(i64, i64): i64;
|
||||
def add(a: i64, b: i64): i64 { return a + b; }
|
||||
def mul(a: i64, b: i64): i64 { return a * b; }
|
||||
def twice<T>(x: T): T where T: numeric { return x + x; }
|
||||
struct entry { val name: span<u8>; val run: op; }
|
||||
const ops: entry[3] = [entry{ name = "add", run = add }, entry{ name = "mul", run = mul },
|
||||
entry{ name = "sub", run = def(a: i64, b: i64): i64 { return a - b; } }];
|
||||
const dbl: def(i64): i64 = twice::<i64>;
|
||||
def made(): span<op> {
|
||||
val table := raw_allocate(2 * size_of::<op>()) as *mut op;
|
||||
table[0] = mul;
|
||||
table[1] = add;
|
||||
return table[0:2];
|
||||
}
|
||||
const kept: span<op> = made();
|
||||
def main(): void {
|
||||
for &e in ops { print($"{e.name}={e.run(6, 3)} "); }
|
||||
println($"{dbl(21)} {kept[0](4, 5)} {kept[1](4, 5)} {ops[1].run}");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
hello 5 true
|
||||
@@ -0,0 +1,4 @@
|
||||
import std::io;
|
||||
const greeting: span<u8> = "hello";
|
||||
const empty: *i64 = null;
|
||||
def main(): void { println($"{greeting} {greeting.length} {empty == null}"); }
|
||||
@@ -0,0 +1,7 @@
|
||||
5 28 3 7 5 2 0
|
||||
two three 2
|
||||
odd 4 26
|
||||
2 5 23
|
||||
true true true true
|
||||
11 true true
|
||||
8 true
|
||||
@@ -0,0 +1,60 @@
|
||||
import std::io;
|
||||
|
||||
# A constant holds views of another constant's array, which it shares rather
|
||||
# than copies: a view of a view, views within a struct and within memory the
|
||||
# constant allocated, an empty view at the array's end, and a reference to an
|
||||
# element.
|
||||
const primes: u64[5] = [2, 3, 5, 7, 11];
|
||||
const all: span<u64> = primes[:];
|
||||
const middle: span<u64> = primes[1:3];
|
||||
const inner: span<u64> = rest(middle);
|
||||
const none: span<u64> = primes[5:0];
|
||||
const largest: &u64 = &primes[4];
|
||||
|
||||
const words: strview[3] = ["one", "two", "three"];
|
||||
const later: span<strview> = words[1:];
|
||||
|
||||
struct table {
|
||||
var name: strview;
|
||||
var rows: span<u64>;
|
||||
}
|
||||
|
||||
const odd: table = table{ name = "odd", rows = primes[1:] };
|
||||
|
||||
def rest(of: span<u64>): span<u64> {
|
||||
return of.sub(1, of.length - 1) else { return of; };
|
||||
}
|
||||
|
||||
def halves(): span<span<u64>> {
|
||||
val made := raw_allocate(2 * size_of::<span<u64>>()) as *mut span<u64>;
|
||||
made[0] = primes[0:2];
|
||||
made[1] = primes[2:3];
|
||||
return span::<span<u64>>::over(made as *span<u64>, 2);
|
||||
}
|
||||
|
||||
const split: span<span<u64>> = halves();
|
||||
|
||||
def sum(of: span<u64>): u64 {
|
||||
var total := 0u;
|
||||
for each in of { total += each; }
|
||||
return total;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
println($"{all.length} {sum(all)} {middle[0]} {middle[2]} {inner[0]} {inner.length} {none.length}");
|
||||
println($"{later[0]} {later[1]} {later.length}");
|
||||
println($"{odd.name} {odd.rows.length} {sum(odd.rows)}");
|
||||
println($"{split.length} {sum(split[0])} {sum(split[1])}");
|
||||
|
||||
# The views are of the array itself, not of copies of it.
|
||||
println($"{all.data == primes[:].data} {inner.data == primes[2:].data} {split[1].data == primes[2:].data} {none.data == primes[5:0].data}");
|
||||
|
||||
# A reference to an element, as a constant and as the program runs, is to
|
||||
# the element where the array is.
|
||||
val held: &u64 = &primes[4];
|
||||
println($"{largest} {(largest as *u64) == all.data + 4} {(held as *u64) == all.data + 4}");
|
||||
|
||||
# A local constant views a global one.
|
||||
const first: span<u64> = primes[1:2];
|
||||
println($"{sum(first)} {first.data == all.data + 1}");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
3
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: A pre-condition failed: b != 0, with b = 0.
|
||||
at divide (tests/programs/contracts_1.sakura:6:3)
|
||||
at main (tests/programs/contracts_1.sakura:13:24)
|
||||
@@ -0,0 +1,14 @@
|
||||
# A pre-condition that fails stops the program, saying what it checked and
|
||||
# what its operands held.
|
||||
import std::io;
|
||||
|
||||
def divide(a: i64, b: i64): i64 {
|
||||
expects(b != 0);
|
||||
ensures(q: q * b + a % b == a);
|
||||
return a / b;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
io::println($"{divide(7, 2)}");
|
||||
io::println($"{divide(7, 0)}");
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
(a) closes what it opens
|
||||
((b) c) closes what it opens
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: An assertion failed: A parenthesis closes one never opened, with depth = -1.
|
||||
at balanced (tests/programs/contracts_2.sakura:12:7)
|
||||
at main (tests/programs/contracts_2.sakura:20:11)
|
||||
@@ -0,0 +1,21 @@
|
||||
# An assertion says what failed in words of its own, in place of the condition,
|
||||
# and an `ensures` in a loop's block is checked at each pass's end.
|
||||
import std::io;
|
||||
|
||||
def balanced(lines: span<strview>): void {
|
||||
for line in lines {
|
||||
var depth := 0;
|
||||
ensures(depth == 0);
|
||||
for c in line {
|
||||
continue if c == ' ';
|
||||
depth += c == '(' ? 1 : c == ')' ? -1 : 0;
|
||||
asserts(depth >= 0, "A parenthesis closes one never opened.");
|
||||
}
|
||||
io::println($"{line} closes what it opens");
|
||||
}
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
balanced(["(a)", "((b) c)"]);
|
||||
balanced(["(d))"]);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
first second first pass0 pass1 pass2 turn1 turn2 block
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: A post-condition failed: found == -1 || found % 2 == 0.
|
||||
at first_even (tests/programs/contracts_3.sakura:40:5)
|
||||
at main (tests/programs/contracts_3.sakura:54:28)
|
||||
@@ -0,0 +1,55 @@
|
||||
# An `ensures` is checked on every way out of its block once it is reached: a
|
||||
# `return`, a `break` or `continue` leaving the block, and the block's end. Of
|
||||
# several, the last written is checked first.
|
||||
import std::io;
|
||||
|
||||
var seen: string;
|
||||
|
||||
def saw(what: strview): bool {
|
||||
seen.append(what);
|
||||
seen.append(" ");
|
||||
return true;
|
||||
}
|
||||
|
||||
def returns(early: bool): i64 {
|
||||
ensures(saw("first"));
|
||||
return 1 if early;
|
||||
ensures(saw("second"));
|
||||
return 2;
|
||||
}
|
||||
|
||||
def loops(): void {
|
||||
for i in 0..4 {
|
||||
ensures(saw($"pass{i}"));
|
||||
continue if i == 1;
|
||||
break if i == 2;
|
||||
}
|
||||
var k := 0;
|
||||
while k < 2 {
|
||||
k++;
|
||||
ensures(saw($"turn{k}"));
|
||||
}
|
||||
{
|
||||
ensures(saw("block"));
|
||||
}
|
||||
}
|
||||
|
||||
def first_even(xs: span<i64>): i64 {
|
||||
var found: i64 = -1;
|
||||
for x in xs {
|
||||
ensures(found == -1 || found % 2 == 0);
|
||||
if x % 2 == 1 {
|
||||
found = x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
_ = returns(true);
|
||||
_ = returns(false);
|
||||
loops();
|
||||
io::println(seen.view());
|
||||
io::println($"{first_even([1, 2, 3][:])}");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
7
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: A post-condition failed: left == old(from.balance) - amount, with left = 5, old(from.balance) - amount = 4.
|
||||
at withdraw_wrongly (tests/programs/contracts_4.sakura:15:3)
|
||||
at main (tests/programs/contracts_4.sakura:23:34)
|
||||
@@ -0,0 +1,24 @@
|
||||
# An `ensures` names what the function returns, and `old` within it is what an
|
||||
# expression was where the `ensures` was reached.
|
||||
import std::io;
|
||||
|
||||
struct wallet { var balance: u64; }
|
||||
|
||||
def withdraw(from: &mut wallet, amount: u64): u64 {
|
||||
expects(amount <= from.balance, "A wallet gives no more than it holds.");
|
||||
ensures(left: left == old(from.balance) - amount);
|
||||
from.balance -= amount;
|
||||
return from.balance;
|
||||
}
|
||||
|
||||
def withdraw_wrongly(from: &mut wallet, amount: u64): u64 {
|
||||
ensures(left: left == old(from.balance) - amount);
|
||||
from.balance -= amount;
|
||||
return from.balance + 1;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
var held := wallet { balance = 10 };
|
||||
io::println($"{withdraw(&mut held, 3)}");
|
||||
io::println($"{withdraw_wrongly(&mut held, 3)}");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
abc
|
||||
9 2.5 5
|
||||
108 true true
|
||||
contracts are checked
|
||||
@@ -0,0 +1,43 @@
|
||||
# Contracts that hold change nothing: a local given back by name reads as
|
||||
# itself, generic code checks each instance, a lambda's `ensures` covers its own
|
||||
# `return`, and `expects`, `asserts`, `ensures`, and `old` are names wherever no
|
||||
# statement begins with one.
|
||||
import std::io;
|
||||
|
||||
struct terms { var expects: bool; var ensures: i64; }
|
||||
|
||||
def asserts(n: i64): i64 { return n * 2; }
|
||||
|
||||
def old(n: i64): i64 { return n + 100; }
|
||||
|
||||
def named(): string {
|
||||
var s := string::from("abc", nothing {});
|
||||
ensures(r: r.length == 3 && s.length == 3);
|
||||
return s;
|
||||
}
|
||||
|
||||
def largest<T>(a: T, b: T): T where T: ordered {
|
||||
ensures(r: r >= a && r >= b);
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
def next_of(n: i64): i64 {
|
||||
val step := def(k: i64): i64 {
|
||||
ensures(r: r > k);
|
||||
return k + 1;
|
||||
};
|
||||
return step(n);
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
io::println(named().view());
|
||||
io::println($"{largest(3, 9)} {largest::<f64>(2.5, 1.5)} {next_of(4)}");
|
||||
val made := terms { expects = true, ensures = 3 };
|
||||
var expects := asserts(made.ensures) + old(1);
|
||||
ensures(old(expects) == 107 && expects == 108);
|
||||
expects++;
|
||||
io::println($"{expects} {made.expects} {contracts()}");
|
||||
if const contracts() {
|
||||
io::println("contracts are checked");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
2 counted
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: A pre-condition failed: A count of 0 is above zero, with count = 0.
|
||||
at counted (tests/programs/contracts_6.sakura:6:3)
|
||||
at main (tests/programs/contracts_6.sakura:12:10)
|
||||
@@ -0,0 +1,13 @@
|
||||
# What a contract says failed may be any view of text, made only where it
|
||||
# fails, and a full stop it ends with is the report's own.
|
||||
import std::io;
|
||||
|
||||
def counted(why: strview, count: i64): void {
|
||||
expects(count > 0, why);
|
||||
io::println($"{count} counted");
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
counted("A count is above zero.", 2);
|
||||
counted($"A count of {0} is above zero.", 0);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
negative zero small large
|
||||
1 3 5 7 9 fib 75025
|
||||
97120 98120
|
||||
@@ -0,0 +1,30 @@
|
||||
import std::io;
|
||||
def view(text: span<u8>): span<u8> { return text; }
|
||||
def classify(n: int): string {
|
||||
if n < 0 { return "negative"; }
|
||||
else if n == 0 { return "zero"; }
|
||||
else if n < 10 { return "small"; }
|
||||
return "large";
|
||||
}
|
||||
def fib(n: int): int { return n < 2 ? n : fib(n - 1) + fib(n - 2); }
|
||||
def main(): void {
|
||||
println($"{classify(-1)} {classify(0)} {classify(5)} {classify(50)}");
|
||||
var i := 0;
|
||||
while true {
|
||||
i++;
|
||||
if i % 2 == 0 { continue; }
|
||||
if i > 9 { break; }
|
||||
print($"{i} ");
|
||||
}
|
||||
println($"fib {fib(25)}");
|
||||
val text: span<u8> = "a,b,,c";
|
||||
for c in text {
|
||||
if c == ',' { continue; }
|
||||
if c == 'c' { break; }
|
||||
for d in view("xy") {
|
||||
if d == 'y' { break; }
|
||||
print($"{c as u8}{d as u8} ");
|
||||
}
|
||||
}
|
||||
println("");
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
vector 5 total 30 first 0 last 16
|
||||
owner 7
|
||||
function 42 pair
|
||||
maybe 16 result 12
|
||||
string core alone 10 CORE ALONE true
|
||||
lanes 22 4 7 8
|
||||
atomic 5 equal true
|
||||
@@ -0,0 +1,62 @@
|
||||
# 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.view();
|
||||
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.view();
|
||||
say($"string {text} {words.length} {words.upper()} {text.view() == 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 }}");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
--- exit 2
|
||||
tests/programs/decl_of_takes_a_type_a_declaration_makes_1.sakura:3:53: invalid: `decl_of` takes a type a declaration makes, as a struct, union, enum, or trait, and `&position` is none: `name_of` and `path_of` name any type. (In `path_of_decl<&position>`)
|
||||
@@ -0,0 +1,7 @@
|
||||
import std::io;
|
||||
struct position { var x: int; }
|
||||
def path_of_decl<T>(): strview { return decl_of::<T>().path; }
|
||||
def main(): void {
|
||||
println(path_of_decl::<position>());
|
||||
println(path_of_decl::<&position>());
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
shape of area 4, circle of area 3, shape of area 5
|
||||
18 4 9 =373
|
||||
@@ -0,0 +1,32 @@
|
||||
import std::io;
|
||||
trait Shape {
|
||||
def area(&this): f64;
|
||||
def name(&this): string { return "shape"; }
|
||||
def describe(&this): string { return $"{this.name()} of area {this.area()}"; }
|
||||
def grow(&mut this, by: f64): void { this.scale(by); this.scale(by); }
|
||||
def mix(&this, a: f64, label: span<u8>, other: &Shape): string { return $"{label}{this.area() + a + other.area()}"; }
|
||||
def scale(&mut this, by: f64): void;
|
||||
}
|
||||
struct square: Shape { var side: f64; }
|
||||
def square::area(&this): f64 { return this.side * this.side; }
|
||||
def square::scale(&mut this, by: f64): void { this.side *= by; }
|
||||
struct circle: Shape { var r: f64; }
|
||||
def circle::area(&this): f64 { return 3 * this.r * this.r; }
|
||||
def circle::name(&this): string { return "circle"; }
|
||||
def circle::scale(&mut this, by: f64): void { this.r *= by; }
|
||||
struct blob { var size: f64; }
|
||||
def blob::area(&this): f64 { return this.size; }
|
||||
def blob::scale(&mut this, by: f64): void { this.size += by; }
|
||||
def show(s: &Shape): string { return s.describe(); }
|
||||
def enlarge(s: &mut Shape): void { s.grow(2); }
|
||||
def main(): void {
|
||||
var sq := square{ 2 };
|
||||
var c := circle{ 1 };
|
||||
var b := blob{ 5 };
|
||||
println($"{sq.describe()}, {c.describe()}, {show(b)}");
|
||||
sq.grow(3);
|
||||
enlarge(c);
|
||||
enlarge(b);
|
||||
val label := "=";
|
||||
println($"{sq.side} {c.r} {b.size} {sq.mix(1, label, c)}");
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
* 4
|
||||
> 4
|
||||
@@ -0,0 +1,10 @@
|
||||
import std::io;
|
||||
trait shows { def show(this: &shows, prefix: span<u8> = "> "): string; }
|
||||
struct dot { val n: int; }
|
||||
def dot::show(this: &dot, prefix: span<u8> = "* "): string { return $"{prefix}{this.n}"; }
|
||||
def through(s: &shows): string { return s.show(); }
|
||||
def main(): void {
|
||||
val d := dot { n = 4 };
|
||||
println(d.show());
|
||||
println(through(&d));
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
before
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: Null reference was unwrapped.
|
||||
at main (tests/programs/dereferences_2.sakura:5:6)
|
||||
@@ -0,0 +1,6 @@
|
||||
import std::io;
|
||||
def main(): void {
|
||||
var p: *mut int;
|
||||
println("before");
|
||||
*p = 1;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
[red, green, blue] 9 paint{shade = blue, levels = [low, high]} green top bottom
|
||||
red:r:low:-3 green:g:mid:-2 blue:b:mid:-2
|
||||
none; int 7; text;
|
||||
[green, blue] [red, blue] false true true
|
||||
10 20
|
||||
@@ -0,0 +1,39 @@
|
||||
import std::io;
|
||||
enum color: u8 { red, green = 4, blue }
|
||||
enum level { low = -3, mid, high = 10 }
|
||||
enum wide: u64 { top = 18446744073709551615, bottom = 0 }
|
||||
struct nothing {}
|
||||
union value { var none: nothing; var number: i64; var text: span<u8>; }
|
||||
struct paint { var shade: color; var levels: span<level>; }
|
||||
def color::next(this: &color): color {
|
||||
return match this { red => color::green, green => color::blue, blue => color::red };
|
||||
}
|
||||
def name(v: value): string {
|
||||
return match v { number(n) => $"int {n}", text(s) => s, none => "none" };
|
||||
}
|
||||
def most<T>(a: T, b: T): T where T: ordered { return a > b ? a : b; }
|
||||
def main(): void {
|
||||
var c := color::red;
|
||||
val seen: color[3] = [c, c.next(), c.next().next()];
|
||||
var total: u32 = 0;
|
||||
for s in seen { total += s; }
|
||||
var ls: level[2] = [level::low, level::high];
|
||||
val p := paint{ shade = color::blue, levels = span::<level>::over((&ls) as *level, 2) };
|
||||
println($"{seen} {total} {p} {most(color::green, color::red)} {wide::top} {wide::bottom}");
|
||||
for n in [0, 4, 5] {
|
||||
val shade := n as color;
|
||||
val word := match shade { red => "r", green => "g", blue => "b" };
|
||||
val lv := match n { 0 => level::low, 4, 5 => level::mid, else => level::high };
|
||||
print($"{shade}:{word}:{lv}:{lv as i32} ");
|
||||
}
|
||||
println("");
|
||||
var values: value[3] = [value{}, value{ number = 7 }, value{ text = "text" }];
|
||||
for v in values { print($"{name(v)}; "); }
|
||||
println("");
|
||||
val bytes := [color::green, color::blue];
|
||||
var copy := bytes;
|
||||
copy[0] = color::red;
|
||||
println($"{bytes} {copy} {bytes == copy} {level::mid < level::high} {c == 0}");
|
||||
val pick := def(x: color): int { return match x { red => 1, else => 2 } * 10; };
|
||||
println($"{pick(color::red)} {pick(color::blue)}");
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
a 2 true c
|
||||
2 3 e 3 1
|
||||
@@ -0,0 +1,8 @@
|
||||
import std::io;
|
||||
enum word: u8 { a, b, c, first = word::a, last = word::c }
|
||||
enum next: u8 { start = word::last, d, e, stop = next::e }
|
||||
def name(w: word): int { return match w { first => 1, b => 2, last => 3 }; }
|
||||
def main(): void {
|
||||
println($"{word::first} {word::last as u8} {word::first == word::a} {2 as word}");
|
||||
println($"{next::start as u8} {next::d as u8} {next::stop} {name(word::c)} {name(word::a)}");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
true
|
||||
false
|
||||
false
|
||||
@@ -0,0 +1,8 @@
|
||||
import std::io;
|
||||
struct tag : equatable { var id: i64; var note: i64; }
|
||||
def tag::equals(&this, other: &tag): bool { return this.id == other.id; }
|
||||
def main(): void {
|
||||
println($"{tag{id=1,note=7} == tag{id=1,note=9}}");
|
||||
println($"{tag{id=1,note=7} != tag{id=1,note=9}}");
|
||||
println($"{tag{id=1,note=7} == tag{id=2,note=7}}");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true false true true false true false true false true false matched
|
||||
@@ -0,0 +1,37 @@
|
||||
import std::io;
|
||||
struct nothing {}
|
||||
struct point { var x: int; var y: f32; var tag: span<u8>; }
|
||||
def point::equals(&this, other: &point): bool {
|
||||
return this.x == other.x && this.y == other.y && this.tag == other.tag;
|
||||
}
|
||||
struct tree { var name: span<u8>; var kids: *tree; }
|
||||
def tree::equals(&this, other: &tree): bool { return this.name == other.name && this.kids == other.kids; }
|
||||
union value { var none: nothing; var whole: int; var text: span<u8>; var spot: point; }
|
||||
def value::equals(&this, other: &value): bool {
|
||||
match this {
|
||||
none => return other is none;
|
||||
whole(n) => { match other { whole(m) => return n == m; else => return false; } }
|
||||
text(t) => { match other { text(u) => return t == u; else => return false; } }
|
||||
spot(p) => { match other { spot(q) => return p == q; else => return false; } }
|
||||
}
|
||||
}
|
||||
def main(): void {
|
||||
val a := point{ x = 1, y = 0.5, tag = "a" };
|
||||
var b := a;
|
||||
print($"{a == b} ");
|
||||
b.tag = "b";
|
||||
val word: span<u8> = "x";
|
||||
val v := value{ text = word };
|
||||
val w := value{ spot = a };
|
||||
val x := value{ text = word };
|
||||
val three := value{ whole = 3 };
|
||||
val same := value{ spot = a };
|
||||
print($"{a == b} {a != b} {v == x} {v == three} {w == same} {w == v} ");
|
||||
var kid := tree{ name = "k", kids = null };
|
||||
val t := tree{ name = "r", kids = (&mut kid) as *mut tree };
|
||||
var u := t;
|
||||
print($"{t == u} ");
|
||||
u.name = "z";
|
||||
print($"{t == u} {[a, b] == [a, b]} {[a] == [b]} ");
|
||||
match w { spot(p) => println(p == a ? "matched" : "missed"); else => println("missed"); }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
3 -1 5 7 6 -1 close 101 leaving close 100 102 -1 8 -2 close 200 close 201 x 202 close 202 x 204 held 4 9 close 9 close 4
|
||||
@@ -0,0 +1,32 @@
|
||||
import std::io;
|
||||
enum why { empty }
|
||||
struct res : owning { var id: i64; }
|
||||
def res::close(&mut this): void { if this.id != 0 { print($"close {this.id} "); } }
|
||||
def mk(n: i64): res { return res { id = n }; }
|
||||
def parse(n: i64): result<i64, why> { if n == 0 { return why::empty; } return n; }
|
||||
def made(n: i64): result<res, why> { if n == 0 { return why::empty; } return mk(n); }
|
||||
def find(n: i64): maybe<i64> { if n > 1 { return n; } return nothing {}; }
|
||||
def pair(r: res, n: i64): i64 { return r.id + n; }
|
||||
def total(xs: span<i64>): result<i64, why> {
|
||||
var sum: i64 = 0;
|
||||
for x in xs { sum += parse(x) else (e) { return e; }; }
|
||||
return sum;
|
||||
}
|
||||
def early(n: i64): i64 { return pair(mk(100 + n), parse(n) else { print("leaving "); return -1; }); }
|
||||
def guard(n: i64): i64 {
|
||||
if parse(n) is not value(v) { return -2; }
|
||||
return v * 2;
|
||||
}
|
||||
def main(): void {
|
||||
print($"{parse(3) else 0} {parse(0) else -1} {find(5) else 0} {find(1) else 7} ");
|
||||
print($"{total([1, 2, 3]) else -1} {total([1, 0]) else -1} ");
|
||||
print($"{early(1)} {early(0)} {guard(4)} {guard(0)} ");
|
||||
for i in 0..3 {
|
||||
val x := pair(mk(200 + i), parse(i) else { continue; });
|
||||
print($"x {x} ");
|
||||
}
|
||||
val r := made(4) else { return; };
|
||||
val q := made(0) else mk(9);
|
||||
_ = parse(1);
|
||||
print($"held {r.id} {q.id} ");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
3
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: Division by zero.
|
||||
at divide (tests/programs/faults_2.sakura:2:44)
|
||||
at main (tests/programs/faults_2.sakura:5:20)
|
||||
@@ -0,0 +1,6 @@
|
||||
import std::io;
|
||||
def divide(a: int, b: int): int { return a / b; }
|
||||
def main(): void {
|
||||
println($"{divide(7, 2)}");
|
||||
println($"{divide(7, 0)}");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
--- exit 1
|
||||
sakura: runtime error: Null reference was unwrapped.
|
||||
at main (tests/programs/faults_3.sakura:5:31)
|
||||
@@ -0,0 +1,6 @@
|
||||
import std::io;
|
||||
struct node { var value: int; var next: *node; }
|
||||
def main(): void {
|
||||
val first := node{};
|
||||
val second := (*first.next).value;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
[1, 2, 3, 4] [1, 9, 3, 4] false true 4
|
||||
300 40 [10, 20, 30, 40]
|
||||
6 [[1, 2, 3], [40, 5, 6]] g
|
||||
[2, 3, 301, 5]
|
||||
xyz [x, y, z] 6
|
||||
@@ -0,0 +1,34 @@
|
||||
import std::io;
|
||||
const N: uint = 3;
|
||||
const TABLE: int[4] = [10, 20, 30, 40];
|
||||
struct grid { var cells: int[N]; var rows: int[3][2]; var name: span<u8>; }
|
||||
def total(g: &grid): int {
|
||||
var sum := 0;
|
||||
for x in g.cells { sum = sum + x; }
|
||||
return sum;
|
||||
}
|
||||
def folded(): int {
|
||||
var a: int[3] = [1, 2, 3];
|
||||
var t := 0;
|
||||
for x in a { t = t + x; }
|
||||
return t;
|
||||
}
|
||||
def main(): void {
|
||||
var a: int[4] = [1, 2, 3, 4];
|
||||
var b := a;
|
||||
b[1] = 9;
|
||||
println($"{a} {b} {a == b} {a == [1, 2, 3, 4]} {a.length}");
|
||||
var i: uint = 2;
|
||||
a[i] = a[i] * 100;
|
||||
println($"{a[i]} {TABLE[3]} {TABLE}");
|
||||
var g := grid { cells = [1, 2, 3], rows = [[1,2,3],[4,5,6]], name = "g" };
|
||||
g.rows[1][0] = 40;
|
||||
println($"{total(&g)} {g.rows} {g.name}");
|
||||
for &mut x in a { x = x + 1; }
|
||||
println($"{a}");
|
||||
var texts: span<u8>[3] = ["x", "y", "z"];
|
||||
var joined := string::new(nothing {});
|
||||
for t in texts { joined.append($"{t}"); }
|
||||
const k := folded();
|
||||
println($"{joined} {texts} {k}");
|
||||
}
|
||||
Loaded 100 of 706 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user