refactor: apply project style guidelines
This commit is contained in:
+20
-8
@@ -19,15 +19,18 @@ CloseError :: enum {
|
||||
close_failed
|
||||
}
|
||||
|
||||
#! open a caller-owned handle; release it with close.
|
||||
open proc(io Io, path [;0]u8, mode FileMode) File ! OpenError {
|
||||
handle Handle :: try io.vtable.open(io.context, path, mode)
|
||||
return File{ io = io, handle = handle }
|
||||
}
|
||||
|
||||
#! close the owned handle, invalidating its borrowed readers and writers.
|
||||
close proc(file File) void ! CloseError {
|
||||
try file.io.vtable.close(file.io.context, file.handle)
|
||||
}
|
||||
|
||||
#! borrow the file handle; the reader must not outlive the open file.
|
||||
reader proc(file File) Reader {
|
||||
return Reader {
|
||||
context = file.io.context,
|
||||
@@ -36,6 +39,7 @@ reader proc(file File) Reader {
|
||||
}
|
||||
}
|
||||
|
||||
#! borrow the file handle; the writer must not outlive the open file.
|
||||
writer proc(file File) Writer {
|
||||
return Writer {
|
||||
context = file.io.context,
|
||||
@@ -44,7 +48,8 @@ writer proc(file File) Writer {
|
||||
}
|
||||
}
|
||||
|
||||
@hide system_read proc(_ ?@mut anyopaque, handle Handle, buffer []mut u8) usize ! ReadError {
|
||||
@hide:file
|
||||
system_read proc(_ ?@mut anyopaque, handle Handle, buffer []mut u8) usize ! ReadError {
|
||||
request := buffer.len
|
||||
maximum usize :: usize(maxval!(c_long))
|
||||
if (request > maximum) request = maximum
|
||||
@@ -60,7 +65,8 @@ writer proc(file File) Writer {
|
||||
}
|
||||
}
|
||||
|
||||
@hide system_write proc(_ ?@mut anyopaque, handle Handle, bytes []u8) usize ! WriteError {
|
||||
@hide:file
|
||||
system_write proc(_ ?@mut anyopaque, handle Handle, bytes []u8) usize ! WriteError {
|
||||
request := bytes.len
|
||||
maximum usize :: usize(maxval!(c_long))
|
||||
if (request > maximum) request = maximum
|
||||
@@ -76,7 +82,8 @@ writer proc(file File) Writer {
|
||||
}
|
||||
}
|
||||
|
||||
@hide system_open proc(_ ?@mut anyopaque, path [;0]u8, mode FileMode) Handle ! OpenError {
|
||||
@hide:file
|
||||
system_open proc(_ ?@mut anyopaque, path [;0]u8, mode FileMode) Handle ! OpenError {
|
||||
flags := c.O_RDONLY
|
||||
match mode {
|
||||
.read_only: flags = c.O_RDONLY
|
||||
@@ -90,23 +97,28 @@ writer proc(file File) Writer {
|
||||
}
|
||||
}
|
||||
|
||||
@hide system_close proc(_ ?@mut anyopaque, handle Handle) void ! CloseError {
|
||||
@hide:file
|
||||
system_close proc(_ ?@mut anyopaque, handle Handle) void ! CloseError {
|
||||
if (c.close(handle.file_desc) != 0) return .close_failed
|
||||
}
|
||||
|
||||
@hide system_stdin proc(_ ?@mut anyopaque) Handle {
|
||||
@hide:file
|
||||
system_stdin proc(_ ?@mut anyopaque) Handle {
|
||||
return Handle{ file_desc = c_int(Stream.stdin) }
|
||||
}
|
||||
|
||||
@hide system_stdout proc(_ ?@mut anyopaque) Handle {
|
||||
@hide:file
|
||||
system_stdout proc(_ ?@mut anyopaque) Handle {
|
||||
return Handle{ file_desc = c_int(Stream.stdout) }
|
||||
}
|
||||
|
||||
@hide system_stderr proc(_ ?@mut anyopaque) Handle {
|
||||
@hide:file
|
||||
system_stderr proc(_ ?@mut anyopaque) Handle {
|
||||
return Handle{ file_desc = c_int(Stream.stderr) }
|
||||
}
|
||||
|
||||
@hide system_vtable IoVTable :: IoVTable {
|
||||
@hide:file
|
||||
system_vtable IoVTable :: IoVTable {
|
||||
read = system_read,
|
||||
write = system_write,
|
||||
open = system_open,
|
||||
|
||||
+33
-14
@@ -1,4 +1,6 @@
|
||||
import "@ffi/c"
|
||||
import "@std/mem"
|
||||
# typeinfo! requires this import even without a qualified meta reference.
|
||||
import "@std/meta"
|
||||
|
||||
ReadError :: enum {
|
||||
@@ -23,6 +25,7 @@ Stream :: enum(c_int) {
|
||||
stderr = c.STDERR_FILENO
|
||||
}
|
||||
|
||||
#! context and vtable are borrowed and must outlive derived readers and writers.
|
||||
Io :: struct {
|
||||
context ?@mut anyopaque
|
||||
vtable @IoVTable
|
||||
@@ -38,12 +41,14 @@ IoVTable :: struct {
|
||||
stderr @proc(context ?@mut anyopaque) Handle
|
||||
}
|
||||
|
||||
#! borrows its context and handle; it does not close the handle.
|
||||
Reader :: struct {
|
||||
context ?@mut anyopaque
|
||||
handle Handle
|
||||
read @proc(context ?@mut anyopaque, handle Handle, buffer []mut u8) usize ! ReadError
|
||||
}
|
||||
|
||||
#! borrows its context and handle; it does not close the handle.
|
||||
Writer :: struct {
|
||||
context ?@mut anyopaque
|
||||
handle Handle
|
||||
@@ -101,7 +106,7 @@ stderr proc(io Io) Writer {
|
||||
}
|
||||
}
|
||||
|
||||
print proc(output Writer, $format []u8, $Args type, args Args) void ! WriteError {
|
||||
print proc($format []u8, $Args type, output Writer, args Args) void ! WriteError {
|
||||
inline for parse_format(format.len, format, Args) |token| {
|
||||
match token.kind {
|
||||
.unused: break
|
||||
@@ -120,14 +125,19 @@ print proc(output Writer, $format []u8, $Args type, args Args) void ! WriteError
|
||||
}
|
||||
|
||||
@hide:file
|
||||
write_integer_signed proc(output Writer, value i64, base u64, uppercase bool) void ! WriteError {
|
||||
write_integer_signed proc(
|
||||
output Writer,
|
||||
value i64,
|
||||
base u64,
|
||||
uppercase bool,
|
||||
) void ! WriteError {
|
||||
buffer [65]mut u8 := undefined
|
||||
end := buffer.len
|
||||
current := value
|
||||
|
||||
while true {
|
||||
digit_value i64 :: rem!(current, i64(base))
|
||||
digit := if (digit_value < 0)
|
||||
digit :: if (digit_value < 0)
|
||||
u8(-digit_value)
|
||||
else
|
||||
u8(digit_value)
|
||||
@@ -152,7 +162,12 @@ write_integer_signed proc(output Writer, value i64, base u64, uppercase bool) vo
|
||||
}
|
||||
|
||||
@hide:file
|
||||
write_integer_unsigned proc(output Writer, value u64, base u64, uppercase bool) void ! WriteError {
|
||||
write_integer_unsigned proc(
|
||||
output Writer,
|
||||
value u64,
|
||||
base u64,
|
||||
uppercase bool,
|
||||
) void ! WriteError {
|
||||
buffer [65]mut u8 := undefined
|
||||
end := buffer.len
|
||||
current := value
|
||||
@@ -201,9 +216,7 @@ FormatToken :: struct {
|
||||
@hide:file
|
||||
parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
|
||||
tokens [N]mut FormatToken := undefined
|
||||
for (usize(0))..format.len |index| {
|
||||
tokens[index] = FormatToken{ kind = .unused, start = 0, end = 0, field = "" }
|
||||
}
|
||||
mem.set(FormatToken, tokens[..], FormatToken{ kind = .unused, start = 0, end = 0, field = "" })
|
||||
|
||||
field_count := 0
|
||||
match typeinfo!(Args) {
|
||||
@@ -314,7 +327,7 @@ parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
|
||||
if literal_start < format.len {
|
||||
tokens[token_count] = FormatToken{
|
||||
kind = .literal,
|
||||
start = literal_start,
|
||||
start = literal_start,
|
||||
end = format.len,
|
||||
field = "",
|
||||
}
|
||||
@@ -351,7 +364,13 @@ scalar_or_distinct_type proc($T type) bool {
|
||||
}
|
||||
|
||||
@hide:file
|
||||
write_integer proc(output Writer, $T type, value T, base u64, uppercase bool) void ! WriteError {
|
||||
write_integer proc(
|
||||
$T type,
|
||||
output Writer,
|
||||
value T,
|
||||
base u64,
|
||||
uppercase bool,
|
||||
) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
.integer: if minval!(T) < 0 {
|
||||
try write_integer_signed(output, i64(value), base, uppercase)
|
||||
@@ -369,7 +388,7 @@ write_integer proc(output Writer, $T type, value T, base u64, uppercase bool) vo
|
||||
|
||||
# note: libc keeps float formatting small; replace it with a native shortest-roundtrip writer if locale independence matters.
|
||||
@hide:file
|
||||
write_float proc(output Writer, $T type, value T, scientific bool) void ! WriteError {
|
||||
write_float proc($T type, output Writer, value T, scientific bool) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
.float: {
|
||||
buffer [64]mut u8 := undefined
|
||||
@@ -395,7 +414,7 @@ write_float proc(output Writer, $T type, value T, scientific bool) void ! WriteE
|
||||
}
|
||||
|
||||
@hide:file
|
||||
write_decimal proc(output Writer, $T type, value T) void ! WriteError {
|
||||
write_decimal proc($T type, output Writer, value T) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
.integer: try write_integer(output, value, 10, false)
|
||||
.float: try write_float(output, value, false)
|
||||
@@ -409,13 +428,13 @@ write_decimal proc(output Writer, $T type, value T) void ! WriteError {
|
||||
}
|
||||
|
||||
@hide:file
|
||||
write_character proc(output Writer, $T type, value T) void ! WriteError {
|
||||
write_character proc($T type, output Writer, value T) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
.integer: {
|
||||
if minval!(T) < 0 or maxval!(T) > 255 {
|
||||
compile_error!("io.print '{c}' requires an unsigned integer that fits in u8")
|
||||
}
|
||||
buffer [1]u8 := [u8(value)]
|
||||
buffer [1]u8 :: [u8(value)]
|
||||
try write_all(output, buffer[..])
|
||||
}
|
||||
.distinct |backing|: if scalar_or_distinct_type(backing) {
|
||||
@@ -428,7 +447,7 @@ write_character proc(output Writer, $T type, value T) void ! WriteError {
|
||||
}
|
||||
|
||||
@hide:file
|
||||
write_default proc(output Writer, $T type, value T) void ! WriteError {
|
||||
write_default proc($T type, output Writer, value T) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
.bool: if value {
|
||||
try write_all(output, "true")
|
||||
|
||||
Reference in New Issue
Block a user