From d0bd3714eee7dd3a4472932575c9b91473eddd51 Mon Sep 17 00:00:00 2001 From: koniifer Date: Thu, 19 Dec 2024 17:19:57 +0000 Subject: [PATCH] logger work --- Cargo.lock | 14 +++---- .../stn/src/{formatters.hb => fmt.hb} | 41 +++++++++++-------- sysdata/libraries/stn/src/lib.hb | 14 +++++-- sysdata/libraries/stn/src/log.hb | 39 ++++++++++++++---- sysdata/libraries/stn/src/math.hb | 12 +++--- sysdata/programs/test/src/main.hb | 2 +- sysdata/programs/test/src/tests/stn/fmt.hb | 27 ++++++++++++ .../programs/test/src/tests/stn/formatters.hb | 29 ------------- sysdata/programs/test/src/tests/stn/lib.hb | 2 +- 9 files changed, 109 insertions(+), 71 deletions(-) rename sysdata/libraries/stn/src/{formatters.hb => fmt.hb} (81%) create mode 100644 sysdata/programs/test/src/tests/stn/fmt.hb delete mode 100644 sysdata/programs/test/src/tests/stn/formatters.hb diff --git a/Cargo.lock b/Cargo.lock index 1b847ebf..eac0453b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,9 +73,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cc" -version = "1.2.4" +version = "1.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf" +checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e" dependencies = [ "shlex", ] @@ -213,12 +213,12 @@ dependencies = [ [[package]] name = "hbbytecode" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#f05c61a99e87f312992419dc10c8ce62a3469449" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#cfd3eac0a832b61abe57a047aeb14ff8290dd8e6" [[package]] name = "hblang" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#f05c61a99e87f312992419dc10c8ce62a3469449" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#cfd3eac0a832b61abe57a047aeb14ff8290dd8e6" dependencies = [ "hashbrown", "hbbytecode", @@ -229,7 +229,7 @@ dependencies = [ [[package]] name = "hbvm" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#f05c61a99e87f312992419dc10c8ce62a3469449" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#cfd3eac0a832b61abe57a047aeb14ff8290dd8e6" dependencies = [ "hbbytecode", ] @@ -421,9 +421,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.168" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "limine" diff --git a/sysdata/libraries/stn/src/formatters.hb b/sysdata/libraries/stn/src/fmt.hb similarity index 81% rename from sysdata/libraries/stn/src/formatters.hb rename to sysdata/libraries/stn/src/fmt.hb index cca0c344..eaa6b8c6 100644 --- a/sysdata/libraries/stn/src/formatters.hb +++ b/sysdata/libraries/stn/src/fmt.hb @@ -1,4 +1,4 @@ -.{Kind, string, signed_int, float, integer, memory, panic} := @use("stn") +.{Kind, usize, string, signed_int, float, integer, memory, panic, log: .{LogLevel}} := @use("stn") fmt_int := fn(v: @Any(), str: ^u8, radix: @TypeOf(v)): uint { is_negative := signed_int(@TypeOf(v)) & v < 0 @@ -126,7 +126,7 @@ fmt_container := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { len += 2 $loop { v_sub := v[i] - len += @inline(fmt_inner, v_sub, str + len, opts) + len += @inline(format, v_sub, str + len, opts) i += 1 if i == @len(T2) break else { *@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(", \0") @@ -143,11 +143,28 @@ fmt_nullable := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { *@as(^[u8; 4], @bitcast(str)) = *@bitcast("null\0") return 4 } else { - return @inline(fmt_inner, @as(@ChildOf(@TypeOf(v)), v), str, opts) + return @inline(format, @as(@ChildOf(@TypeOf(v)), v), str, opts) } } -fmt_inner := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { +fmt_enum := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { + T := @TypeOf(v) + // len := @len(@nameof(T)); + // *@as(^[u8; @len(@nameof(T))], @bitcast(str)) = *@bitcast(@nameof(T)); + // *@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(".(\0") + // len += 2 + // len += @inline(fmt_int, @as(usize(T), @bitcast(v)), str + len, 10); + // *@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(".)\0") + // return len + 2 + return fmt_int(@as(usize(T), @bitcast(v)), str, 10) +} + +/* SAFETY: + * Assumes the buffer is wide enough for the formatted text and a null char + * Does not clear the buffer for you +*/ + +format := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { T := @TypeOf(v) match Kind.of(T) { .Pointer => return @inline(fmt_int, @as(uint, @bitcast(v)), str, 16), @@ -161,6 +178,7 @@ fmt_inner := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { } }, .Opt => return @inline(fmt_nullable, v, str, opts), + .Enum => return @inline(fmt_enum, v, str, opts), .Struct => return @inline(fmt_container, v, str, opts), .Slice => return @inline(fmt_container, v, str, opts), _ => @error("Type: \"\0", T, "\" is not supported.\0"), @@ -179,15 +197,6 @@ fmt_inner := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint { FormatOptions := struct { decimal_digits: uint = 2, radix: uint = 10, -} - -/* SAFETY: - * Assumes the buffer is wide enough for the formatted text and a null char -*/ -format := fn(v: @Any(), str: ^u8): ^u8 return @inline(format_args, v, str, .{}) - -format_args := fn(v: @Any(), str: ^u8, opts: FormatOptions): ^u8 { - @inline(string.clear, str) - _ = @inline(fmt_inner, v, str, opts) - return str -} + // temporarily here, will change later maybe + log: LogLevel = .Info, +} \ No newline at end of file diff --git a/sysdata/libraries/stn/src/lib.hb b/sysdata/libraries/stn/src/lib.hb index 259d73e8..c0d85bea 100644 --- a/sysdata/libraries/stn/src/lib.hb +++ b/sysdata/libraries/stn/src/lib.hb @@ -1,6 +1,6 @@ acs := @use("acs.hb") allocators := @use("alloc/lib.hb") -formatters := @use("formatters.hb") +fmt := @use("fmt.hb") hashers := @use("hash/lib.hb") string := @use("string.hb") log := @use("log.hb") @@ -58,6 +58,14 @@ $float := fn($T: type): bool { return T == f32 | T == f64 } -$float_bytes := fn($T: type): type { - if T == f64 return uint else if T == f32 return u32 +$usize := fn($T: type): type { + if @sizeof(T) == 1 return u8 else if @sizeof(T) == 2 return u16 else if @sizeof(T) == 4 return u32 else return uint +} + +$bits := fn($T: type): usize(T) { + return @sizeof(T) << 3 +} + +$bitmask := fn($T: type): usize(T) { + return -1 } \ No newline at end of file diff --git a/sysdata/libraries/stn/src/log.hb b/sysdata/libraries/stn/src/log.hb index bd7d6256..67a95b41 100644 --- a/sysdata/libraries/stn/src/log.hb +++ b/sysdata/libraries/stn/src/log.hb @@ -1,13 +1,36 @@ -string := @use("string.hb") +.{string, fmt, memory} := @use("stn") -LogMsg := packed struct {level: u8, string: ^u8, strlen: uint} +LogMsg := packed struct {level: LogLevel, string: ^u8, strlen: uint} -log := fn(level: u8, message: ^u8): void { +LogLevel := enum { + Error, + Warn, + Info, + Debug, + Trace, +} + +log := fn(level: LogLevel, message: ^u8): void { return @eca(3, 1, LogMsg.(level, message, @inline(string.length, message)), @sizeof(LogMsg)) } -error := fn(message: ^u8): void return @inline(log, 0, message) -warn := fn(message: ^u8): void return @inline(log, 1, message) -info := fn(message: ^u8): void return @inline(log, 2, message) -debug := fn(message: ^u8): void return @inline(log, 3, message) -trace := fn(message: ^u8): void return @inline(log, 4, message) \ No newline at end of file +error := fn(message: ^u8): void return @inline(log, LogLevel.Error, message) +warn := fn(message: ^u8): void return @inline(log, LogLevel.Warn, message) +info := fn(message: ^u8): void return @inline(log, LogLevel.Info, message) +debug := fn(message: ^u8): void return @inline(log, LogLevel.Debug, message) +trace := fn(message: ^u8): void return @inline(log, LogLevel.Trace, message) + +print_buffer := memory.dangling(u8) + +print := fn(v: @Any(), opts: fmt.FormatOptions): void { + if @TypeOf(v) == ^u8 { + info(v) + return + } + if print_buffer == memory.dangling(u8) { + print_buffer = memory.request_page(1) + } + len := @inline(fmt.format, v, print_buffer, opts) + @eca(3, 1, LogMsg.(opts.log, print_buffer, len), @sizeof(LogMsg)) + memory.set(u8, &0, print_buffer, memory.PAGE_SIZE) +} \ No newline at end of file diff --git a/sysdata/libraries/stn/src/math.hb b/sysdata/libraries/stn/src/math.hb index 55fca824..085c3db1 100644 --- a/sysdata/libraries/stn/src/math.hb +++ b/sysdata/libraries/stn/src/math.hb @@ -1,4 +1,4 @@ -.{unsigned_int, signed_int, integer, float, float_bytes} := @use("stn") +.{unsigned_int, signed_int, integer, float, usize, bitmask} := @use("stn") $PI := 3.14159265358979323846264338327950288 $LN_2 := 0.693147180559945309417232121458176568 @@ -8,7 +8,7 @@ $abs := fn($T: type, x: T): T { if integer(T) { return (x ^ x >> @sizeof(T) * 8 - 1) - (x >> @sizeof(T) * 8 - 1) } else if float(T) { - return @bitcast(@as(float_bytes(T), @bitcast(x)) & (1 << @sizeof(T) * 8) - 1) + return @bitcast(@as(usize(T), @bitcast(x)) & bitmask(T) >> 1) } } // todo: better float min, max @@ -21,7 +21,7 @@ $min := fn($T: type, a: T, b: T): T { } $max := fn($T: type, a: T, b: T): T { if integer(T) { - return a - (a - b & a - b >> @sizeof(T) - 1) + return a - (a - b & a - b >> @sizeof(T) * 8 - 1) } else if float(T) { return @itf(a > b) * a + @itf(a <= b) * b } @@ -30,8 +30,8 @@ $sign := fn($T: type, x: T): i8 { if signed_int(T) { return @intcast(x >> @sizeof(T) * 8 - 1) | x != 0 } else if float(T) { - sign_bit := @as(float_bytes(T), @bitcast(x)) >> @sizeof(T) * 8 - 1 - return (1 - 2 * @intcast(sign_bit)) * (x != 0) + sign_bit := @as(usize(T), @bitcast(x)) >> @sizeof(T) * 8 - 1 + return 1 - (@intcast(sign_bit) << 1) & x != 0 } } log := fn($T: type, base: T, x: T): T { @@ -156,7 +156,7 @@ ln := fn($T: type, x: T): T { i := 0 loop if i == 10 break else { - exp_val := exp(T, guess) + exp_val := @inline(exp, T, guess) f := exp_val - x f_prime := exp_val diff --git a/sysdata/programs/test/src/main.hb b/sysdata/programs/test/src/main.hb index f4421ba9..f1bff3c9 100644 --- a/sysdata/programs/test/src/main.hb +++ b/sysdata/programs/test/src/main.hb @@ -3,5 +3,5 @@ serial_driver := @use("./tests/serial_driver.hb") main := fn(): uint { // return serial_driver.test() - return stn.formatters.test() + return stn.fmt.test() } \ No newline at end of file diff --git a/sysdata/programs/test/src/tests/stn/fmt.hb b/sysdata/programs/test/src/tests/stn/fmt.hb new file mode 100644 index 00000000..cfb29379 --- /dev/null +++ b/sysdata/programs/test/src/tests/stn/fmt.hb @@ -0,0 +1,27 @@ +.{log, math} := @use("stn"); +.{Color} := @use("lib:render") + +Thingy := struct { + a: uint, + b: int, + c: SubThingy, +} + +SubThingy := struct { + a: f32, + b: bool, +} + +test := fn(): uint { + log.print("Hello, World!\0", .{log: .Error}) + log.print(Thingy.(-100, -100, .(-math.PI, true)), .{}) + log.print(SubThingy.(-math.E, false), .{}) + log.print(Color.{r: 255, g: 254, b: 253, a: 252}, .{radix: 2}) + log.print(@as(f64, math.LN_2), .{radix: 16, decimal_digits: 1 << 32}) + log.print([u8].(1, 2, 3), .{}) + log.print(&SubThingy.(0.0, true), .{}) + log.print(@as(?u32, null), .{}) + log.print(@as(?u32, 200), .{}) + + return 0 +} \ No newline at end of file diff --git a/sysdata/programs/test/src/tests/stn/formatters.hb b/sysdata/programs/test/src/tests/stn/formatters.hb deleted file mode 100644 index 2fce6e65..00000000 --- a/sysdata/programs/test/src/tests/stn/formatters.hb +++ /dev/null @@ -1,29 +0,0 @@ -.{formatters: .{format, format_args, DEFAULT_OPTS}, log, memory, math} := @use("stn"); -.{Color} := @use("lib:render") - -Thingy := struct { - a: uint, - b: int, - c: SubThingy, -} - -SubThingy := struct { - a: f32, - b: bool, -} - -test := fn(): uint { - buffer := memory.request_page(1) - log.info(format(Thingy.(-100, -100, .(-math.PI, true)), buffer)) - log.info(format(SubThingy.(-math.E, false), buffer)) - log.info(format_args(Color.{r: 255, g: 254, b: 253, a: 252}, buffer, .{ - radix: 16, - })) - log.info(format_args(math.LN_2, buffer, .{radix: 16, decimal_digits: 1 << 32})) - log.info(format([u8].(1, 2, 3), buffer)) - log.info(format(&SubThingy.(0.0, true), buffer)) - log.info(format(@as(?u32, null), buffer)) - log.info(format(@as(?u32, 200), buffer)) - - return 0 -} \ No newline at end of file diff --git a/sysdata/programs/test/src/tests/stn/lib.hb b/sysdata/programs/test/src/tests/stn/lib.hb index 8aaa8ca8..f9b1bcc1 100644 --- a/sysdata/programs/test/src/tests/stn/lib.hb +++ b/sysdata/programs/test/src/tests/stn/lib.hb @@ -3,4 +3,4 @@ allocators := @use("./allocators.hb") sleep := @use("./sleep.hb") dt := @use("./dt.hb") process := @use("./process.hb") -formatters := @use("./formatters.hb") \ No newline at end of file +fmt := @use("./fmt.hb") \ No newline at end of file