This commit is contained in:
koniifer 2025-03-11 20:35:24 +00:00
parent 1eb3f8f0a5
commit d69cd9799c
4 changed files with 49 additions and 34 deletions

2
build
View file

@ -8,7 +8,7 @@ readonly LILY_TEST_DIR="${LILY_TEST_DIR:-$LILY_SCRIPT_DIR/test}"
readonly LILY_BUILD_DIR="${LILY_BUILD_DIR:-$LILY_SCRIPT_DIR/out}"
readonly HBLANG_GIT="https://git.ablecorp.us/mlokis/hblang"
readonly HBLANG_COMMIT="bd36e598b5caa29510fd4461b16675f53b0d7277"
readonly HBLANG_COMMIT="ab3fb8422c15fec753e429c588759c5adfddddf9"
readonly HBC_FLAGS="--path-projection lily $LILY_SRC_DIR/lib.hb ${HBC_FLAGS:-}"
readonly HBC_BINARY="hbc"

26
main.hb
View file

@ -1,30 +1,14 @@
lily.{fmt, log, mem, alloc, target} := @use("lily")
// b: []u8 = idk
b: []u8 = idk
main := fn(): void {
arena := alloc.Arena.new()
defer arena.deinit()
// i := 0
// loop if i == 5 break else {
// // 24 = @size_of(AllocationHeader)
// if arena.alloc(u8, target.page_len() - 24) == null die
// i += 1
// }
b = arena.alloc(u8, 1).?
// b = arena.alloc_zeroed(u8, 1000).?
// mem.bytes(mem.reverse("Hello, World!")[1..]).take(5).for_each(fn(x: u8): void {
// len := fmt.fmt_int(b, x, 16)
// log.info(b[0..len])
// mem.set(b.ptr, 0, len)
// })
c := alloc.Vec(uint, alloc.Arena).new(&arena)
defer c.deinit()
loop if c.len() == 100000 break else {
c.push(1)
}
iter := mem.bytes(mem.reverse("Hello, World!")[1..]).take(5)
str := iter.collect_vec(&arena)
log.info(str.slice)
}

View file

@ -26,30 +26,31 @@ Arena := struct {
Self := @CurrentScope()
$new := fn(): @CurrentScope() {
$new := fn(): Self {
return .(null)
}
// todo: handle alignment
alloc := fn(self: ^Self, $T: type, count: uint): ?[]T {
size := mem.size(T, count)
header: ^AllocationHeader = idk
header: ^AllocationHeader = @bit_cast(self.allocation)
if self.allocation == null {
new_header := AllocationHeader.new(size)
// todo: handle cleanly
if new_header == null die
self.allocation = new_header
header = @bit_cast(new_header)
} else {
header = @bit_cast(self.allocation)
}
loop {
lily.log.debug("arena.hb:45: if i dont print this it crashes")
if header.len + size <= header.cap {
header.len += size
break
}
if header.next == null {
header.next = AllocationHeader.new(size)
// todo: handle cleanly
if header.next == null die
}
header = @bit_cast(header.next)
}
@ -77,7 +78,7 @@ Arena := struct {
allocation: ^AllocationHeader = @bit_cast(self.allocation)
loop {
next := allocation.next
target.dealloc(@bit_cast(allocation), allocation.cap)
target.dealloc(@bit_cast(allocation), allocation.cap + @size_of(AllocationHeader))
if next == null break
allocation = @bit_cast(next)
}

View file

@ -1,3 +1,5 @@
lily.{Type, alloc: .{Vec}} := @use("lib.hb")
Next := fn(T: type): type return struct {
.finished: bool;
.val: T
@ -11,29 +13,57 @@ Iterator := fn(T: type): type return struct {
IterNext := @TypeOf(T.next(idk))
IterVal := @TypeOf(T.next(idk).val)
$next := fn(self: ^@CurrentScope()): IterNext {
Self := @CurrentScope()
$next := fn(self: ^Self): IterNext {
return self.inner.next()
}
$map := fn(self: ^@CurrentScope(), $func: type): Iterator(Map(T, func)) {
$map := fn(self: ^Self, $func: type): Iterator(Map(T, func)) {
return .(.(self))
}
$enumerate := fn(self: ^@CurrentScope()): Iterator(Enumerate(T)) {
$enumerate := fn(self: ^Self): Iterator(Enumerate(T)) {
return .(.(self, 0))
}
$take := fn(self: ^@CurrentScope(), end: uint): Iterator(Take(T)) {
$take := fn(self: ^Self, end: uint): Iterator(Take(T)) {
return .(.(self, 0, end))
}
for_each := fn(self: ^@CurrentScope(), $func: type): void {
for_each := fn(self: ^Self, $func: type): void {
loop {
x := self.next()
if x.finished break
_ = func(x.val)
}
}
collect := fn(self: ^Self, $A: type): ?A {
$if Type(A).kind() != .Array {
@error("collecting", Self, "into type", A, "unsupported for now")
}
$if @ChildOf(A) != IterVal {
@error("cannot collect of iterator of type", IterVal, "into type", A)
}
cont: A = idk
i := 0
loop {
defer i += 1
x := self.next()
if i == @len_of(A) & x.finished return cont
if i == @len_of(A) | x.finished return null
cont[i] = x.val
}
}
// ! broken
collect_vec := fn(self: ^Self, allocator: @Any()): Vec(IterVal, @ChildOf(@TypeOf(allocator))) {
vec := Vec(IterVal, @ChildOf(@TypeOf(allocator))).new(allocator)
loop {
x := self.next()
if x.finished return vec
vec.push(x.val)
}
}
}
Map := fn(T: type, func: type): type return struct {
.iter: Iterator(T)
.iter: ^Iterator(T)
IterNext := @TypeOf(func(idk))
$next := fn(self: ^@CurrentScope()): Next(IterNext) {