logger work
This commit is contained in:
parent
e8f1bce4d6
commit
d0bd3714ee
14
Cargo.lock
generated
14
Cargo.lock
generated
|
@ -73,9 +73,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.2.4"
|
version = "1.2.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf"
|
checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
|
@ -213,12 +213,12 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hbbytecode"
|
name = "hbbytecode"
|
||||||
version = "0.1.0"
|
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]]
|
[[package]]
|
||||||
name = "hblang"
|
name = "hblang"
|
||||||
version = "0.1.0"
|
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 = [
|
dependencies = [
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
"hbbytecode",
|
"hbbytecode",
|
||||||
|
@ -229,7 +229,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hbvm"
|
name = "hbvm"
|
||||||
version = "0.1.0"
|
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 = [
|
dependencies = [
|
||||||
"hbbytecode",
|
"hbbytecode",
|
||||||
]
|
]
|
||||||
|
@ -421,9 +421,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.168"
|
version = "0.2.169"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
|
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "limine"
|
name = "limine"
|
||||||
|
|
|
@ -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 {
|
fmt_int := fn(v: @Any(), str: ^u8, radix: @TypeOf(v)): uint {
|
||||||
is_negative := signed_int(@TypeOf(v)) & v < 0
|
is_negative := signed_int(@TypeOf(v)) & v < 0
|
||||||
|
@ -126,7 +126,7 @@ fmt_container := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint {
|
||||||
len += 2
|
len += 2
|
||||||
$loop {
|
$loop {
|
||||||
v_sub := v[i]
|
v_sub := v[i]
|
||||||
len += @inline(fmt_inner, v_sub, str + len, opts)
|
len += @inline(format, v_sub, str + len, opts)
|
||||||
i += 1
|
i += 1
|
||||||
if i == @len(T2) break else {
|
if i == @len(T2) break else {
|
||||||
*@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(", \0")
|
*@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")
|
*@as(^[u8; 4], @bitcast(str)) = *@bitcast("null\0")
|
||||||
return 4
|
return 4
|
||||||
} else {
|
} 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)
|
T := @TypeOf(v)
|
||||||
match Kind.of(T) {
|
match Kind.of(T) {
|
||||||
.Pointer => return @inline(fmt_int, @as(uint, @bitcast(v)), str, 16),
|
.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),
|
.Opt => return @inline(fmt_nullable, v, str, opts),
|
||||||
|
.Enum => return @inline(fmt_enum, v, str, opts),
|
||||||
.Struct => return @inline(fmt_container, v, str, opts),
|
.Struct => return @inline(fmt_container, v, str, opts),
|
||||||
.Slice => return @inline(fmt_container, v, str, opts),
|
.Slice => return @inline(fmt_container, v, str, opts),
|
||||||
_ => @error("Type: \"\0", T, "\" is not supported.\0"),
|
_ => @error("Type: \"\0", T, "\" is not supported.\0"),
|
||||||
|
@ -179,15 +197,6 @@ fmt_inner := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint {
|
||||||
FormatOptions := struct {
|
FormatOptions := struct {
|
||||||
decimal_digits: uint = 2,
|
decimal_digits: uint = 2,
|
||||||
radix: uint = 10,
|
radix: uint = 10,
|
||||||
}
|
// temporarily here, will change later maybe
|
||||||
|
log: LogLevel = .Info,
|
||||||
/* 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
|
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
acs := @use("acs.hb")
|
acs := @use("acs.hb")
|
||||||
allocators := @use("alloc/lib.hb")
|
allocators := @use("alloc/lib.hb")
|
||||||
formatters := @use("formatters.hb")
|
fmt := @use("fmt.hb")
|
||||||
hashers := @use("hash/lib.hb")
|
hashers := @use("hash/lib.hb")
|
||||||
string := @use("string.hb")
|
string := @use("string.hb")
|
||||||
log := @use("log.hb")
|
log := @use("log.hb")
|
||||||
|
@ -58,6 +58,14 @@ $float := fn($T: type): bool {
|
||||||
return T == f32 | T == f64
|
return T == f32 | T == f64
|
||||||
}
|
}
|
||||||
|
|
||||||
$float_bytes := fn($T: type): type {
|
$usize := fn($T: type): type {
|
||||||
if T == f64 return uint else if T == f32 return u32
|
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
|
||||||
}
|
}
|
|
@ -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))
|
return @eca(3, 1, LogMsg.(level, message, @inline(string.length, message)), @sizeof(LogMsg))
|
||||||
}
|
}
|
||||||
|
|
||||||
error := fn(message: ^u8): void return @inline(log, 0, message)
|
error := fn(message: ^u8): void return @inline(log, LogLevel.Error, message)
|
||||||
warn := fn(message: ^u8): void return @inline(log, 1, message)
|
warn := fn(message: ^u8): void return @inline(log, LogLevel.Warn, message)
|
||||||
info := fn(message: ^u8): void return @inline(log, 2, message)
|
info := fn(message: ^u8): void return @inline(log, LogLevel.Info, message)
|
||||||
debug := fn(message: ^u8): void return @inline(log, 3, message)
|
debug := fn(message: ^u8): void return @inline(log, LogLevel.Debug, message)
|
||||||
trace := fn(message: ^u8): void return @inline(log, 4, 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)
|
||||||
|
}
|
|
@ -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
|
$PI := 3.14159265358979323846264338327950288
|
||||||
$LN_2 := 0.693147180559945309417232121458176568
|
$LN_2 := 0.693147180559945309417232121458176568
|
||||||
|
@ -8,7 +8,7 @@ $abs := fn($T: type, x: T): T {
|
||||||
if integer(T) {
|
if integer(T) {
|
||||||
return (x ^ x >> @sizeof(T) * 8 - 1) - (x >> @sizeof(T) * 8 - 1)
|
return (x ^ x >> @sizeof(T) * 8 - 1) - (x >> @sizeof(T) * 8 - 1)
|
||||||
} else if float(T) {
|
} 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
|
// 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 {
|
$max := fn($T: type, a: T, b: T): T {
|
||||||
if integer(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) {
|
} else if float(T) {
|
||||||
return @itf(a > b) * a + @itf(a <= b) * b
|
return @itf(a > b) * a + @itf(a <= b) * b
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,8 @@ $sign := fn($T: type, x: T): i8 {
|
||||||
if signed_int(T) {
|
if signed_int(T) {
|
||||||
return @intcast(x >> @sizeof(T) * 8 - 1) | x != 0
|
return @intcast(x >> @sizeof(T) * 8 - 1) | x != 0
|
||||||
} else if float(T) {
|
} else if float(T) {
|
||||||
sign_bit := @as(float_bytes(T), @bitcast(x)) >> @sizeof(T) * 8 - 1
|
sign_bit := @as(usize(T), @bitcast(x)) >> @sizeof(T) * 8 - 1
|
||||||
return (1 - 2 * @intcast(sign_bit)) * (x != 0)
|
return 1 - (@intcast(sign_bit) << 1) & x != 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log := fn($T: type, base: T, x: T): T {
|
log := fn($T: type, base: T, x: T): T {
|
||||||
|
@ -156,7 +156,7 @@ ln := fn($T: type, x: T): T {
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
loop if i == 10 break else {
|
loop if i == 10 break else {
|
||||||
exp_val := exp(T, guess)
|
exp_val := @inline(exp, T, guess)
|
||||||
f := exp_val - x
|
f := exp_val - x
|
||||||
f_prime := exp_val
|
f_prime := exp_val
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,5 @@ serial_driver := @use("./tests/serial_driver.hb")
|
||||||
|
|
||||||
main := fn(): uint {
|
main := fn(): uint {
|
||||||
// return serial_driver.test()
|
// return serial_driver.test()
|
||||||
return stn.formatters.test()
|
return stn.fmt.test()
|
||||||
}
|
}
|
27
sysdata/programs/test/src/tests/stn/fmt.hb
Normal file
27
sysdata/programs/test/src/tests/stn/fmt.hb
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -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
|
|
||||||
}
|
|
|
@ -3,4 +3,4 @@ allocators := @use("./allocators.hb")
|
||||||
sleep := @use("./sleep.hb")
|
sleep := @use("./sleep.hb")
|
||||||
dt := @use("./dt.hb")
|
dt := @use("./dt.hb")
|
||||||
process := @use("./process.hb")
|
process := @use("./process.hb")
|
||||||
formatters := @use("./formatters.hb")
|
fmt := @use("./fmt.hb")
|
Loading…
Reference in a new issue