1
0
Fork 0
forked from AbleOS/ableos

more and less broken

This commit is contained in:
koniifer 2024-12-17 22:30:22 +00:00 committed by peony
parent 6ddc199b6a
commit c0a4c88c41
5 changed files with 181 additions and 165 deletions

24
Cargo.lock generated
View file

@ -213,35 +213,25 @@ 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#e769fa8dbad4d69ccab9e13112e20be4b8bd91bf"
[[package]]
name = "hbbytecode"
version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#248bdf003aa991f4bad18ddcb084532d1bcb78d5"
[[package]] [[package]]
name = "hblang" name = "hblang"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#e769fa8dbad4d69ccab9e13112e20be4b8bd91bf"
dependencies = [ dependencies = [
"hashbrown", "hashbrown",
"hbbytecode 0.1.0", "hbbytecode",
"hbvm 0.1.0", "hbvm",
"log", "log",
] ]
[[package]] [[package]]
name = "hbvm" name = "hbvm"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#e769fa8dbad4d69ccab9e13112e20be4b8bd91bf"
dependencies = [ dependencies = [
"hbbytecode 0.1.0", "hbbytecode",
]
[[package]]
name = "hbvm"
version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#248bdf003aa991f4bad18ddcb084532d1bcb78d5"
dependencies = [
"hbbytecode 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)",
] ]
[[package]] [[package]]
@ -401,7 +391,7 @@ dependencies = [
"crossbeam-queue", "crossbeam-queue",
"derive_more", "derive_more",
"hashbrown", "hashbrown",
"hbvm 0.1.0 (git+https://git.ablecorp.us/AbleOS/holey-bytes.git)", "hbvm",
"ktest_macro", "ktest_macro",
"limine", "limine",
"log", "log",

View file

@ -14,8 +14,7 @@ fatfs = { version = "0.3", default-features = false, features = [
"alloc", "alloc",
] } ] }
toml = "0.8" toml = "0.8"
#hblang.git = "https://git.ablecorp.us/AbleOS/holey-bytes.git" hblang.git = "https://git.ablecorp.us/AbleOS/holey-bytes.git"
hblang.path = "../../holey-bytes/lang/"
log = "0.4" log = "0.4"
raw-cpuid = "11" raw-cpuid = "11"
ureq = { version = "2", default-features = false, features = ["tls"] } ureq = { version = "2", default-features = false, features = ["tls"] }

View file

@ -1,8 +1,7 @@
.{Kind, string, unsigned_int, signed_int, float, integer, memory, panic} := @use("stn") .{Kind, string, signed_int, float, integer, memory, panic} := @use("stn")
format_int := fn($T: type, v: T, str: ^u8, radix: T): uint { fmt_int := fn(v: @Any(), str: ^u8, radix: @TypeOf(v)): uint {
if integer(T) { is_negative := signed_int(@TypeOf(v)) & v < 0
is_negative := signed_int(T) & v < 0
if is_negative v = -v if is_negative v = -v
prefix_len := 0 prefix_len := 0
@ -44,12 +43,9 @@ format_int := fn($T: type, v: T, str: ^u8, radix: T): uint {
@inline(string.reverse, str + prefix_len) @inline(string.reverse, str + prefix_len)
return prefix_len + i return prefix_len + i
} else {
panic("Type is not an integer\0")
}
} }
format_bool := fn(v: bool, str: ^u8): uint { fmt_bool := fn(v: bool, str: ^u8): uint {
if v { if v {
*@as(^[u8; 4], @bitcast(str)) = *@bitcast("true\0") *@as(^[u8; 4], @bitcast(str)) = *@bitcast("true\0")
return 4 return 4
@ -59,8 +55,7 @@ format_bool := fn(v: bool, str: ^u8): uint {
} }
} }
format_float := fn($T: type, v: T, str: ^u8, precision: uint, radix: int): uint { fmt_float := fn(v: @Any(), str: ^u8, precision: uint, radix: int): uint {
if float(T) {
is_negative := v < 0 is_negative := v < 0
i := 0 i := 0
@ -105,7 +100,7 @@ format_float := fn($T: type, v: T, str: ^u8, precision: uint, radix: int): uint
i += 1 i += 1
p := precision p := precision
tolerance := @as(T, 0.00000001) tolerance := @as(@TypeOf(v), 0.00000001)
loop if p <= 0 | fractional_part < tolerance break else { loop if p <= 0 | fractional_part < tolerance break else {
fractional_part *= @itf(radix) fractional_part *= @itf(radix)
digit := @fti(fractional_part) digit := @fti(fractional_part)
@ -120,36 +115,20 @@ format_float := fn($T: type, v: T, str: ^u8, precision: uint, radix: int): uint
} }
return prefix_len + i return prefix_len + i
} else {
panic("Type is not a floating point\0")
}
} }
format_inner := fn($T: type, v: T, str: ^u8, opts: FormatOptions): uint { fmt_container := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint {
match @as(Kind, @bitcast(@kindof(T))) { T2 := @TypeOf(v)
.Pointer => return @inline(format_int, uint, @bitcast(v), str, 16),
.Builtin => {
if integer(T) {
return @inline(format_int, T, v, str, @intcast(opts.radix))
} else if T == bool {
return @inline(format_bool, v, str)
} else if float(T) {
return @inline(format_float, T, v, str, opts.decimal_digits, @intcast(opts.radix))
}
},
.Struct => {
// name := ;
i := 0; i := 0;
*@as(^[u8; @len(@nameof(T))], @bitcast(str)) = *@bitcast(@nameof(T)) *@as(^[u8; @len(@nameof(T2))], @bitcast(str)) = *@bitcast(@nameof(T2))
len := @len(@nameof(T)); len := @len(@nameof(T2));
*@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(".(\0") *@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(".(\0")
len += 2 len += 2
$loop { $loop {
v_sub := v[i] v_sub := v[i]
TSub := @TypeOf(v_sub) len += @inline(fmt_inner, v_sub, str + len, opts)
len += @inline(format_inner, TSub, v_sub, str + len, opts)
i += 1 i += 1
if i == @len(T) break else { if i == @len(T2) break else {
*@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(", \0") *@as(^[u8; 2], @bitcast(str + len)) = *@bitcast(", \0")
len += 2 len += 2
} }
@ -157,35 +136,58 @@ format_inner := fn($T: type, v: T, str: ^u8, opts: FormatOptions): uint {
*@as(^[u8; 1], @bitcast(str + len)) = *@bitcast(")\0") *@as(^[u8; 1], @bitcast(str + len)) = *@bitcast(")\0")
len += 1 len += 1
return len return len
}
fmt_nullable := fn(v: @Any(), str: ^u8, opts: FormatOptions): uint {
if v == null {
*@as(^[u8; 4], @bitcast(str)) = *@bitcast("null\0")
return 4
} else {
return @inline(fmt_inner, @unwrap(v), str, opts)
}
}
fmt_inner := 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),
.Builtin => {
if integer(T) {
return @inline(fmt_int, v, str, @intcast(opts.radix))
} else if T == bool {
return @inline(fmt_bool, v, str)
} else if float(T) {
return @inline(fmt_float, v, str, opts.decimal_digits, @intcast(opts.radix))
}
}, },
_ => panic("unsupported format type\0"), .Opt => return @inline(fmt_nullable, 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"),
} }
} }
/* TODO: /* TODO:
* Custom formatters using struct methods (T._fmt(self, str): uint), * Custom formatters using struct methods (T._fmt(self, str): uint),
* Format struct names "Name.(x, y, z)"
* Format struct fields "Name.{a: x, b: y, c: z}" * Format struct fields "Name.{a: x, b: y, c: z}"
* Optionally tabulate * Optionally tabulate
* Add more FormatOption fields * Add more FormatOption fields
* Support scientific notation for floating point * Support scientific notation for floating point
* Support format string (impossible right now) * Support format string
* Support nullables (impossible right now)
* Support pointers
*/ */
FormatOptions := struct { FormatOptions := struct {
decimal_digits: uint = 1 << 32, decimal_digits: uint = 2,
radix: uint = 10, radix: uint = 10,
} }
/* SAFETY: /* SAFETY:
* Assumes the buffer is wide enough for the formatted text and a null char * Assumes the buffer is wide enough for the formatted text and a null char
*/ */
format := fn($T: type, v: T, str: ^u8): ^u8 return @inline(format_args, T, v, str, .{}) format := fn(v: @Any(), str: ^u8): ^u8 return @inline(format_args, v, str, .{})
format_args := fn($T: type, v: T, str: ^u8, opts: FormatOptions): ^u8 { format_args := fn(v: @Any(), str: ^u8, opts: FormatOptions): ^u8 {
@inline(string.clear, str) @inline(string.clear, str)
_ = @inline(format_inner, T, v, str, opts) _ = @inline(fmt_inner, v, str, opts)
return str return str
} }

View file

@ -24,7 +24,23 @@ panic := fn(message: ?^u8): never {
die die
} }
Kind := enum {Builtin, Struct, Enum, Union, Pointer, Slice, Opt, Function, Template, Global, Const, Module} Kind := enum {
Builtin,
Struct,
Enum,
Union,
Pointer,
Slice,
Opt,
Function,
Template,
Global,
Const,
Module,
$of := fn($T: type): Self {
return @bitcast(@kindof(T))
}
}
$unsigned_int := fn($T: type): bool { $unsigned_int := fn($T: type): bool {
return T == uint | T == u8 | T == u16 | T == u32 return T == uint | T == u8 | T == u16 | T == u32

View file

@ -12,16 +12,25 @@ SubThingy := struct {
b: bool, b: bool,
} }
a := enum {
Bababa,
}
opaque := fn(): ?u32 {
return null
}
test := fn(): uint { test := fn(): uint {
buffer := memory.request_page(1) buffer := memory.request_page(1)
log.info(format(Thingy, .(-100, -100, .(-math.PI, true)), buffer)) log.info(format(Thingy.(-100, -100, .(-math.PI, true)), buffer))
log.info(format(SubThingy, .(-math.E, false), buffer)) log.info(format(SubThingy.(-math.E, false), buffer))
log.info(format_args(Color, .{r: 255, g: 254, b: 253, a: 252}, buffer, .{ log.info(format_args(Color.{r: 255, g: 254, b: 253, a: 252}, buffer, .{
radix: 16, radix: 16,
})) }))
log.info(format_args(f64, math.LN_2, buffer, .{radix: 16})) log.info(format_args(math.LN_2, buffer, .{radix: 16, decimal_digits: 1 << 32}))
log.info(format([u8; 3], .(1, 2, 3), buffer)) log.info(format([u8].(1, 2, 3), buffer))
log.info(format(^SubThingy, &.(0.0, true), buffer)) log.info(format(&SubThingy.(0.0, true), buffer))
log.info(format(opaque(), buffer))
return 0 return 0
} }