Files
brolang/examples/programs/io/main.bro
T

148 lines
3.5 KiB
Plaintext

io :: import "@std/io"
process :: import "@std/process"
hide read_ok func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! io.ReadError {
if buffer.len == 0 {
return 0
}
buffer[0] = 'o'
if buffer.len == 1 {
return 1
}
buffer[1] = 'k'
return 2
}
hide read_too_much func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! io.ReadError {
return buffer.len + 1
}
hide read_eof func(_ ?*mut anyopaque, _ io.ReadStream, _ []mut u8) usize ! io.ReadError {
return 0
}
hide write_short func(_ ?*mut anyopaque, _ io.WriteStream, bytes []u8) usize ! io.WriteError {
if bytes.len > 2 {
return 2
}
return bytes.len
}
hide write_none func(_ ?*mut anyopaque, _ io.WriteStream, _ []u8) usize ! io.WriteError {
return 0
}
hide write_too_much func(_ ?*mut anyopaque, _ io.WriteStream, bytes []u8) usize ! io.WriteError {
return bytes.len + 1
}
hide ok_vtable io.IoVTable :: io.IoVTable {
read = read_ok,
write = write_short,
}
hide read_bad_vtable io.IoVTable :: io.IoVTable {
read = read_too_much,
write = write_short,
}
hide eof_vtable io.IoVTable :: io.IoVTable {
read = read_eof,
write = write_short,
}
hide write_none_vtable io.IoVTable :: io.IoVTable {
read = read_ok,
write = write_none,
}
hide write_bad_vtable io.IoVTable :: io.IoVTable {
read = read_ok,
write = write_too_much,
}
reader_for func(vtable @io.IoVTable) io.Reader {
return io.Reader {
impl = io.Io {context = none, vtable = vtable},
stream = .stdin,
}
}
writer_for func(vtable @io.IoVTable) io.Writer {
return io.Writer {
impl = io.Io {context = none, vtable = vtable},
stream = .stdout,
}
}
rejects_bad_read func() bool {
buffer [1]mut u8 = [0]
_ = io.read(reader_for(&read_bad_vtable), buffer[..]) catch |err| {
return err == .read_failed
}
return false
}
rejects_no_progress func() bool {
io.print(writer_for(&write_none_vtable), "{s}", {"x",}) catch |err| {
return err == .no_progress
}
return false
}
rejects_bad_write func() bool {
_ = io.write(writer_for(&write_bad_vtable), "x") catch |err| {
return err == .write_failed
}
return false
}
main func(init process.Init) i32 {
system io.Io :: init.io
buffer [2]mut u8 = [0, 0]
count usize :: io.read(reader_for(&ok_vtable), buffer[..]) catch 0
if count != 2 or buffer[0] != 'o' or buffer[1] != 'k' {
return 1
}
eof usize :: io.read(reader_for(&eof_vtable), buffer[..]) catch 1
empty_read usize :: io.read(reader_for(&read_bad_vtable), buffer[0..0]) catch 1
empty_write usize :: io.write(writer_for(&write_bad_vtable), "") catch 1
if eof != 0 or empty_read != 0 or empty_write != 0 {
return 5
}
io.print(writer_for(&ok_vtable), "{s}{d}", {"partial", 37}) catch |_| {
return 2
}
if !rejects_bad_read() or !rejects_no_progress() or !rejects_bad_write() {
return 3
}
io.print(io.Writer {
impl = system,
stream = .stdout,
}, "io-ok {d} {{bro}}\n", {37,}) catch |_| {
return 4
}
io.print(io.Writer {
impl = system,
stream = .stdout,
}, "bounds={d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d}\n", {
minval!(i8), maxval!(u8),
minval!(i16), maxval!(u16),
minval!(i32), maxval!(u32),
minval!(i64), maxval!(u64),
minval!(isize), maxval!(usize),
minval!(c_char), maxval!(c_char),
minval!(c_schar), maxval!(c_uchar),
minval!(c_short), maxval!(c_ushort),
minval!(c_int), maxval!(c_uint),
minval!(c_long), maxval!(c_ulong),
minval!(c_longlong), maxval!(c_ulonglong),
0, 0,
1, 1,
-1, 1,
}) catch |_| {
return 6
}
return 0
}