generic types, any -> opaque

This commit is contained in:
2026-07-17 11:44:05 +02:00
parent af08d1888a
commit 1471c229c2
20 changed files with 284 additions and 167 deletions

View File

@@ -5,26 +5,24 @@ struct Array
size: i64
capacity: i64
func Array.new_preallocated_and_zeroed[size: i64] : Array
xs := new* Array
func Array.preallocate_and_zero[xs: Array<$>, size: i64] : void
if size > 0
xs->data = mem.alloc(size * 8)
mem.zero(xs->data, size * 8)
xs->size = size
xs->capacity = size
return xs
func Array.nth[xs: Array, n: i64] : any
func Array.nth[xs: Array<$>, n: i64] : $
if n < 0 || n >= xs->size
panic("Array.nth out of bounds")
return mem.read64(xs->data + n * 8)
func Array.set[xs: Array, n: i64, x: any] : void
func Array.set[xs: Array<$>, n: i64, x: $] : void
if n < 0 || n >= xs->size
panic("Array.set out of bounds")
mem.write64(xs->data + n * 8, x)
func Array.push[xs: Array, x: any] : void
func Array.push[xs: Array<$>, x: $] : void
if xs->size == xs->capacity
new_capacity := 4
if xs->capacity != 0
@@ -35,54 +33,56 @@ func Array.push[xs: Array, x: any] : void
mem.write64(xs->data + xs->size * 8, x)
xs->size += 1
func Array.free[xs: Array] : void
func Array.free[xs: Array<$>] : void
if xs->data != 0
xs->data->free()
(xs as ptr)->free()
func Array.free_with_items[xs: Array] : void
func Array.free_with_items[xs: Array<$>] : void
if xs->data != 0
for i in 0..xs->size
(xs->nth(i) as ptr)->free()
xs->data->free()
(xs as ptr)->free()
func Array.pop[xs: Array] : any
func Array.pop[xs: Array<$>] : $
if xs->size == 0
panic("Array.pop on empty array")
x : any = Array.last(xs)
x := Array.last(xs)
xs->size = xs->size - 1
return x
func Array.last[xs: Array] : any
func Array.last[xs: Array<$>] : $
if xs->size == 0
panic("Array.last on empty array")
return xs->nth(xs->size - 1)
func Array.slice[xs: Array, start: i64, length: i64] : Array
func Array.slice[xs: Array<$>, start: i64, length: i64] : Array<$>
if start < 0 || length < 0 || start + length > xs->size
panic("Array.slice out of bounds")
new_array := Array.new_preallocated_and_zeroed(length)
new_array := [] as Array<$>
new_array->preallocate_and_zero(length)
mem.copy(xs->data + start * 8, new_array->data, length * 8)
return new_array
func Array.concat[a: Array, b: Array] : Array
new_array := Array.new_preallocated_and_zeroed(a->size + b->size)
func Array.concat[a: Array<$>, b: Array<$>] : Array<$>
new_array := [] as Array<$>
new_array->preallocate_and_zero(a->size + b->size)
mem.copy(a->data, new_array->data, a->size * 8)
mem.copy(b->data, new_array->data + a->size * 8, b->size * 8)
return new_array
func Array.quicksort[arr: Array] : void
func Array.quicksort[arr: Array<$>] : void
arr->_do_quicksort(0, arr->size - 1)
func Array._do_quicksort[arr: Array, low: i64, high: i64] : void
func Array._do_quicksort[arr: Array<$>, low: i64, high: i64] : void
if low < high
i := arr->_partition(low, high)
arr->_do_quicksort(low, i - 1)
arr->_do_quicksort(i + 1, high)
func Array._partition[arr: Array, low: i64, high: i64] : i64
func Array._partition[arr: Array<$>, low: i64, high: i64] : i64
pivot : i64 = arr->nth(high)
i := low - 1
for j in (low)..high
@@ -96,41 +96,42 @@ func Array._partition[arr: Array, low: i64, high: i64] : i64
arr->set(high, temp)
return i + 1
func Array.contains_str[arr: Array, s: str] : bool
func Array.contains_str[arr: Array<str>, s: str] : bool
for i in 0..arr->size
if (arr->nth(i) as str)->equal(s)
return true
return false
func Array.count[arr: Array, item: any] : i64
func Array.count[arr: Array<$>, item: $] : i64
count := 0
for i in 0..arr->size
if arr->nth(i) == item
count += 1
return count
func Array.map[arr: Array, fn: ptr] : Array
out := Array.new_preallocated_and_zeroed(arr->size)
func Array.map[arr: Array<$>, fn: ptr] : Array<$>
out := [] as Array<$>
out->preallocate_and_zero(arr->size)
for i in 0..arr->size
out->set(i, fn(arr->nth(i)))
return out
func Array.filter[arr: Array, fn: ptr] : Array
out := []
func Array.filter[arr: Array<$>, fn: ptr] : Array<$>
out := [] as Array<$>
for i in 0..arr->size
if fn(arr->nth(i))
if fn(arr->nth(i)) as bool
out->push(arr->nth(i))
return out
func Array.reduce[arr: Array, fn: ptr, acc: any] : any
func Array.reduce[arr: Array<$>, fn: ptr, acc: $] : $
for i in 0..arr->size
acc = fn(acc, arr->nth(i))
return acc
func str.split[haystack: str, needle: str]: Array
func str.split[haystack: str, needle: str]: Array<str>
haystack_len := haystack->len()
needle_len := needle->len()
result := []
result := [] as Array<str>
if !needle_len
if !haystack_len
@@ -162,19 +163,20 @@ func str.split[haystack: str, needle: str]: Array
const HashMap._TABLE_SIZE = 100
struct HashMap
table: Array
table: Array<HashMap._Node>
struct HashMap._Node
key: str
value: any
value: $
next: HashMap._Node
func HashMap.new[] : HashMap
map := new* HashMap
map->table = Array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE)
map->table = [] as Array<HashMap._Node>
map->table->preallocate_and_zero(HashMap._TABLE_SIZE)
return map
func HashMap.insert[map: HashMap, key: str, value: any] : void
func HashMap.insert[map: HashMap<$>, key: str, value: $] : void
index := HashMap._djb2(key)
current : HashMap._Node = map->table->nth(index)
@@ -190,7 +192,7 @@ func HashMap.insert[map: HashMap, key: str, value: any] : void
new_node->next = map->table->nth(index)
map->table->set(index, new_node)
func HashMap.get[map: HashMap, key: str] : any, bool
func HashMap.get[map: HashMap<$>, key: str] : $, bool
index := HashMap._djb2(key)
current : HashMap._Node = map->table->nth(index)
@@ -199,9 +201,9 @@ func HashMap.get[map: HashMap, key: str] : any, bool
return current->value, true
current = current->next
return 0 as any, false
return 0 as $, false
func HashMap.delete[map: HashMap, key: str] : void
func HashMap.delete[map: HashMap<$>, key: str] : void
index := HashMap._djb2(key)
current : HashMap._Node = map->table->nth(index)
prev := 0 as HashMap._Node
@@ -220,8 +222,8 @@ func HashMap.delete[map: HashMap, key: str] : void
prev = current
current = current->next
func HashMap.keys[map: HashMap] : Array
out := []
func HashMap.keys[map: HashMap<$>] : Array<str>
out := [] as Array<str>
for i in 0..HashMap._TABLE_SIZE
current : HashMap._Node = map->table->nth(i)
while current as ptr
@@ -229,7 +231,7 @@ func HashMap.keys[map: HashMap] : Array
current = current->next
return out
func HashMap.free_with_keys[map: HashMap] : void
func HashMap.free_with_keys[map: HashMap<$>] : void
for i in 0..HashMap._TABLE_SIZE
current : HashMap._Node = map->table->nth(i)
while current as ptr