Files
zern/src/std/std.zr

1122 lines
29 KiB
Plaintext

func panic[msg: str] : void
io.print("PANIC: ")
io.println(msg)
// for gdb backtrace
_builtin_syscall(SYS_kill, os.getpid(), 6)
os.exit(1)
const MEM_BLOCK_SIZE = 32
struct mem.Block
size: i64
free: bool
next: mem.Block
prev: mem.Block
func mem.align[x: i64] : i64
return (x + 7) & -8
func mem._split_block[blk: mem.Block, needed: i64] : void
if blk->size >= needed + MEM_BLOCK_SIZE + 8
new_blk := (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block
new_blk->size = blk->size - needed - MEM_BLOCK_SIZE
new_blk->free = true
new_blk->next = blk->next
new_blk->prev = blk
if blk->next as ptr
blk->next->prev = new_blk
else
mem.write64(_builtin_heap_tail(), new_blk)
blk->size = needed
blk->next = new_blk
func mem._request_space[size: i64] : mem.Block
needed := size + MEM_BLOCK_SIZE
alloc_size := (needed + 4095) & -4096
blk := _builtin_syscall(SYS_mmap, 0, alloc_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) as mem.Block
if (blk as ptr as i64) == -1
panic("mem._request_space: failed to mmap")
blk->size = alloc_size - MEM_BLOCK_SIZE
blk->free = false
blk->next = 0 as mem.Block
blk->prev = 0 as mem.Block
tail := mem.read64(_builtin_heap_tail()) as mem.Block
if tail as ptr
tail->next = blk
blk->prev = tail
mem.write64(_builtin_heap_tail(), blk)
return blk
func mem.alloc[size: i64] : ptr
if size <= 0
panic("mem.alloc: called with nonpositive size")
size = mem.align(size)
cur := mem.read64(_builtin_heap_head()) as mem.Block
while cur as ptr
if cur->free && cur->size >= size
mem._split_block(cur, size)
cur->free = false
return cur as ptr + MEM_BLOCK_SIZE
cur = cur->next
blk := mem._request_space(size)
if !mem.read64(_builtin_heap_head())
mem.write64(_builtin_heap_head(), blk)
mem._split_block(blk, size)
return blk as ptr + MEM_BLOCK_SIZE
func mem.free[x: any] : void
if x == 0
return 0
blk := (x as ptr - MEM_BLOCK_SIZE) as mem.Block
if blk->free
return 0
blk->free = true
next := blk->next
if next as ptr && next->free
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
if expected_next as ptr == next as ptr
blk->size = blk->size + MEM_BLOCK_SIZE + next->size
blk->next = next->next
if next->next as ptr
next->next->prev = blk
if mem.read64(_builtin_heap_tail()) == next as ptr
mem.write64(_builtin_heap_tail(), blk)
prev := blk->prev
if prev as ptr && prev->free
expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block
if expected_blk as ptr == blk as ptr
prev->size = prev->size + MEM_BLOCK_SIZE + blk->size
prev->next = blk->next
if blk->next as ptr
blk->next->prev = prev
if mem.read64(_builtin_heap_tail()) == blk as ptr
mem.write64(_builtin_heap_tail(), prev)
blk = prev
block_total := blk->size + MEM_BLOCK_SIZE
if (blk as i64 & 4095) == 0 && (block_total & 4095) == 0
if blk->prev as ptr
blk->prev->next = blk->next
else
mem.write64(_builtin_heap_head(), blk->next)
if blk->next as ptr
blk->next->prev = blk->prev
else
mem.write64(_builtin_heap_tail(), blk->prev)
_builtin_syscall(SYS_munmap, blk, block_total)
func mem.realloc[x: ptr, new_size: i64] : ptr
if !x
return mem.alloc(new_size)
if new_size < 0
panic("mem.realloc: called with negative new_size")
if new_size == 0
mem.free(x)
return 0 as ptr
new_size = mem.align(new_size)
blk := (x - MEM_BLOCK_SIZE) as mem.Block
if blk->size >= new_size
mem._split_block(blk, new_size)
return x
next := blk->next
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
if next as ptr && next->free && expected_next as ptr == next as ptr
combined := blk->size + MEM_BLOCK_SIZE + next->size
if combined >= new_size
blk->size = combined
blk->next = next->next
if next->next as ptr
next->next->prev = blk
if mem.read64(_builtin_heap_tail()) == next as ptr
mem.write64(_builtin_heap_tail(), blk)
mem._split_block(blk, new_size)
return x
new_ptr := mem.alloc(new_size)
mem.copy(x, new_ptr, math.min(blk->size, new_size))
mem.free(x)
return new_ptr
func mem.zero_and_free[x: ptr, size: i64] : void
mem.zero(x, size)
mem.free(x)
func mem.zero[x: ptr, size: i64] : void
i := 0
while i + 8 <= size
mem.write64(x + i, 0)
i = i + 8
while i < size
x[i] = 0 as u8
i = i + 1
func mem.copy[src: ptr, dst: ptr, n: i64] : void
if dst > src && dst < src + n
i := n
while i - 8 >= 0
i = i - 8
mem.write64(dst + i, mem.read64(src + i))
while i > 0
i = i - 1
dst[i] = src[i]
else
i := 0
while i + 8 <= n
mem.write64(dst + i, mem.read64(src + i))
i = i + 8
while i < n
dst[i] = src[i]
i = i + 1
func mem.read8[x: ptr] : u8
return x[0]
func mem.read16[x: ptr] : i64
return x[0] as i64 | (x[1] << 8)
func mem.read16be[x: ptr] : i64
return (x[0] << 8) as i64 | x[1]
func mem.read32[x: ptr] : i64
return x[0] as i64 | (x[1] << 8) | (x[2] << 16) | (x[3] << 24)
func mem.read32be[x: ptr] : i64
return (x[0] as i64 << 24) | (x[1] << 16) | (x[2] << 8) | x[3]
func mem.read64[x: ptr] : i64
return _builtin_read64(x)
func mem.write32[x: ptr, d: i64] : void
x[0] = d & 0xff
x[1] = (d >> 8) & 0xff
x[2] = (d >> 16) & 0xff
x[3] = (d >> 24) & 0xff
func mem.write16[x: ptr, d: i64] : void
x[0] = d & 0xff
x[1] = (d >> 8) & 0xff
func mem.write16be[x: ptr, d: i64] : void
x[0] = (d >> 8) & 0xff
x[1] = d & 0xff
func mem.write64[x: ptr, d: any] : void
_builtin_set64(x, d)
struct Blob
data: ptr
size: i64
func Blob.alloc[size: i64] : Blob
buffer := new* Blob
buffer->size = size
buffer->data = mem.alloc(size)
return buffer
func Blob.free[buf: Blob] : void
mem.free(buf->data)
mem.free(buf)
func io.printf[..] : void
s := _var_arg(0) as ptr
i := 1
while s[0]
if s[0] == '%'
s = s + 1
if s[0] == 'd'
io.print_i64(_var_arg(i) as i64)
i = i + 1
else if s[0] == 'x'
io.print_i64_hex(_var_arg(i) as i64)
i = i + 1
else if s[0] == 's'
io.print(_var_arg(i) as str)
i = i + 1
else if s[0] == 'c'
io.print_char(_var_arg(i) as u8)
i = i + 1
else if s[0] == '%'
io.print_char('%')
else if s[0] == 0
break
else
panic("io.printf: unrecognized format")
else
io.print_char(s[0])
s = s + 1
func io.snprintf[..] : i64
buf := _var_arg(0) as ptr
size := _var_arg(1) as i64
if size <= 0
return 0
s := _var_arg(2) as ptr
i := 3
n := 0
while s[0]
if s[0] == '%'
s = s + 1
if s[0] == 'd'
tmp := _stackalloc(21)
str.from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str)
remaining := size - n - 1
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
n = n + tmp_len
i = i + 1
else if s[0] == 'x'
tmp := _stackalloc(17)
str.hex_from_i64_buf(tmp, _var_arg(i) as i64)
tmp_len := str.len(tmp as str)
remaining := size - n - 1
mem.copy(tmp, buf + n, math.min(tmp_len, remaining))
n = n + tmp_len
i = i + 1
else if s[0] == 's'
tmp_str := _var_arg(i) as str
tmp_len := str.len(tmp_str)
remaining := size - n - 1
mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining))
n = n + tmp_len
i = i + 1
else if s[0] == 'c'
if n < size - 1
buf[n] = _var_arg(i) as u8
n = n + 1
i = i + 1
else if s[0] == '%'
if n < size - 1
buf[n] = '%'
n = n + 1
else if s[0] == 0
break
else
panic("io.snprintf: unrecognized format")
else
if n < size - 1
buf[n] = s[0]
n = n + 1
s = s + 1
if n < size
buf[n] = 0
else if size > 0
buf[size - 1] = 0
return n
func io.print_sized[x: ptr, size: i64] : void
_builtin_syscall(SYS_write, 1, x, size)
func io.print[x: str] : void
io.print_sized(x as ptr, str.len(x))
func io.println[x: str] : void
io.print(x)
io.print("\n")
func io.print_char[x: u8] : void
io.print_sized(^x, 1)
func io.print_bool[x: bool] : void
if x
io.print("true")
else
io.print("false")
func io.print_i64[x: i64] : void
s := _stackalloc(21)
str.from_i64_buf(s, x)
io.print(s as str)
func io.print_i64_hex[x: i64] : void
s := _stackalloc(17)
str.hex_from_i64_buf(s, x)
io.print(s as str)
func io.println_i64[x: i64] : void
s := _stackalloc(21)
str.from_i64_buf(s, x)
io.println(s as str)
func io.read_char[] : u8
c := 0 as u8
_builtin_syscall(SYS_read, 0, ^c, 1)
return c
func io.read_line[] : str
b := new str.Builder
while true
c := io.read_char()
if c == '\n'
break
str.Builder.append_char(b, c)
s := str.Builder.build(b)
mem.free(b->data)
return s
func io.file_exists[path: str] : bool
return _builtin_syscall(SYS_faccessat, -100, path, 0, 0) == 0
func io.is_a_directory[path: str] : bool
st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now
rc := _builtin_syscall(SYS_newfstatat, -100, path, st, 0)
if rc != 0
return false
return (mem.read32(st + 24) & S_IFMT) == S_IFDIR
func io.read_text_file[path: str] : str, bool
fd := _builtin_syscall(SYS_openat, -100, path, O_RDONLY, 0)
if fd < 0
return 0 as str, false
size := _builtin_syscall(SYS_lseek, fd, 0, SEEK_END)
_builtin_syscall(SYS_lseek, fd, 0, SEEK_SET)
buffer := mem.alloc(size + 1)
n := _builtin_syscall(SYS_read, fd, buffer, size)
_builtin_syscall(SYS_close, fd)
if n != size
mem.free(buffer)
return 0 as str, false
buffer[n] = 0
return buffer as str, true
func io.read_binary_file[path: str] : Blob, bool
fd := _builtin_syscall(SYS_openat, -100, path, 0, 0)
if fd < 0
return 0 as Blob, false
buf := new* Blob
buf->size = _builtin_syscall(SYS_lseek, fd, 0, 2)
_builtin_syscall(SYS_lseek, fd, 0, 0)
buf->data = mem.alloc(buf->size)
n := _builtin_syscall(SYS_read, fd, buf->data, buf->size)
_builtin_syscall(SYS_close, fd)
if n != buf->size
Blob.free(buf)
return 0 as Blob, false
return buf, true
func io.write_file[path: str, content: str] : bool
return io.write_binary_file(path, content as ptr, str.len(content))
func io.write_binary_file[path: str, content: ptr, size: i64] : bool
fd := _builtin_syscall(SYS_openat, -100, path, O_WRONLY|O_CREAT|O_TRUNC, 0o644)
if fd < 0
return false
n := _builtin_syscall(SYS_write, fd, content, size)
_builtin_syscall(SYS_close, fd)
if n != size
return false
return true
func str.len[s: str] : i64
i := 0
while s[i]
i = i + 1
return i
func str.make_copy[s: str] : str
size := str.len(s) + 1
dup := mem.alloc(size) as str
mem.copy(s as ptr, dup as ptr, size)
return dup
func str.equal[a: str, b: str] : bool
i := 0
while a[i] != 0 && b[i] != 0
if a[i] != b[i]
return false
i = i + 1
return a[i] == b[i]
func str.is_whitespace[x: u8] : bool
return x == ' ' || x == '\n' || x == '\t' || x == '\r'
func str.is_digit[x: u8] : bool
return x >= '0' && x <= '9'
func str.is_hex_digit[x: u8] : bool
return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')
func str.is_lowercase[x: u8] : bool
return x >= 'a' && x <= 'z'
func str.is_uppercase[x: u8] : bool
return x >= 'A' && x <= 'Z'
func str.is_letter[x: u8] : bool
return str.is_uppercase(x) || str.is_lowercase(x)
func str.is_alphanumeric[x: u8] : bool
return str.is_letter(x) || str.is_digit(x)
func str.concat[a: str, b: str] : str
a_len := str.len(a)
b_len := str.len(b)
out := mem.alloc(a_len + b_len + 1) as str
mem.copy(a as ptr, out as ptr, a_len)
mem.copy(b as ptr, out as ptr + a_len, b_len)
out[a_len + b_len] = 0
return out
func str.contains[haystack: str, needle: str] : bool
return str.find(haystack, needle) != -1
func str.find[haystack: str, needle: str] : i64
haystack_len := str.len(haystack)
needle_len := str.len(needle)
if needle_len == 0
return 0
if needle_len > haystack_len
return -1
for i in 0..(haystack_len - needle_len + 1)
if haystack[i] != needle[0]
continue
if needle_len == 1
return i
match := true
for j in 1..needle_len
if haystack[i + j] != needle[j]
match = false
break
if match
return i
return -1
func str.substr[s: str, start: i64, length: i64] : str
if start < 0 || length < 0 || start + length > str.len(s)
panic("str.substr out of bounds")
out := mem.alloc(length + 1) as str
mem.copy(s as ptr + start, out as ptr, length)
out[length] = 0
return out
func str.trim[s: str] : str
len := str.len(s)
if len == 0
out := mem.alloc(1) as str
out[0] = 0
return out
start := 0
end := len - 1
while start <= end && str.is_whitespace(s[start])
start = start + 1
while end >= start && str.is_whitespace(s[end])
end = end - 1
return str.substr(s, start, end - start + 1)
func str.split[haystack: str, needle: str]: Array
haystack_len := str.len(haystack)
needle_len := str.len(needle)
result := []
if !needle_len
if !haystack_len
return result
else
for i in 0..haystack_len
array.push(result, str.substr(haystack, i, 1))
return result
start := 0
i := 0
while i < haystack_len
if i <= haystack_len - needle_len
match := true
for j in 0..needle_len
if haystack[i + j] != needle[j]
match = false
break
if match
array.push(result, str.substr(haystack, start, i - start))
start = i + needle_len
i = i + needle_len
continue
i = i + 1
array.push(result, str.substr(haystack, start, haystack_len - start))
return result
func str.reverse[s: str] : str
len := str.len(s)
out := mem.alloc(len + 1) as str
for i in 0..len
out[i] = s[len - i - 1]
out[len] = 0
return out
func str.from_i64[n: i64] : str
out := mem.alloc(21)
str.from_i64_buf(out, n)
return out as str
func str.from_i64_buf[buf: ptr, n: i64] : void
if n == 0
buf[0] = '0'
buf[1] = 0
return 0
neg : bool = n < 0
if neg
// MIN_I64 will fail but i so dont care
n = -n
tmp := _stackalloc(21)
end := 20
tmp[end] = 0
end = end - 1
for i in 0..19
if n == 0
break
tmp[end] = '0' + (n % 10)
n = n / 10
end = end - 1
if neg
tmp[end] = '-'
end = end - 1
len := 19 - end
for i in 0..len
buf[i] = tmp[end + 1 + i]
buf[len] = 0
func str.hex_from_i64[n: i64] : str
out := mem.alloc(17)
str.hex_from_i64_buf(out, n)
return out as str
func str.hex_from_i64_buf[buf: ptr, n: i64] : void
hex_chars := "0123456789abcdef"
if n == 0
buf[0] = '0'
buf[1] = 0
return 0
mask := (1 << 60) - 1
tmp := _stackalloc(17)
len := 0
for i in 0..16
if n == 0
break
tmp[len] = hex_chars[n & 15]
n = (n >> 4) & mask
len = len + 1
for j in 0..len
buf[j] = tmp[len - 1 - j]
buf[len] = 0
func str.from_char[c: u8] : str
s := mem.alloc(2) as str
s[0] = c
s[1] = 0
return s
func str.parse_i64[s: str] : i64
len := str.len(s)
i := 0
sign := 1
if i < len && s[i] == '-'
sign = -1
i = i + 1
num := 0
while i < len
d := s[i]
if d < '0' || d > '9'
break
num = num * 10 + (d - '0')
i = i + 1
return num * sign
func str.hex_encode[s: str, s_len: i64] : str
hex_chars := "0123456789abcdef"
out := mem.alloc(s_len * 2 + 1) as str
for i in 0..s_len
b := s[i]
out[i * 2] = hex_chars[(b >> 4) & 15]
out[i * 2 + 1] = hex_chars[b & 15]
out[s_len * 2] = 0
return out
func str._hex_digit_to_int[d: u8] : u8
if d >= '0' && d <= '9'
return d - '0'
lower : u8 = d | 32
if lower >= 'a' && lower <= 'f'
return lower - 'a' + 10
panic("invalid hex digit passed to str._hex_digit_to_int")
func str.hex_decode[s: str] : str
s_len := str.len(s)
if s_len % 2 != 0
panic("invalid hex string passed to str.hex_decode")
out_len := s_len / 2
out := mem.alloc(out_len + 1) as str
for i in 0..out_len
high := str._hex_digit_to_int(s[i * 2])
low := str._hex_digit_to_int(s[i * 2 + 1])
out[i] = (high << 4) | low
out[out_len] = 0
return out
struct str.Builder
data: ptr
size: i64
capacity: i64
func str.Builder._grow[b: str.Builder, needed: i64] : void
if b->size + needed > b->capacity
new_capacity := 64
if b->capacity != 0
new_capacity = b->capacity * 2
while new_capacity < b->size + needed
new_capacity = new_capacity * 2
b->data = mem.realloc(b->data, new_capacity)
b->capacity = new_capacity
func str.Builder.append_char[b: str.Builder, c: u8] : void
str.Builder._grow(b, 1)
b->data[b->size] = c
b->size = b->size + 1
func str.Builder.append[b: str.Builder, s: str] : void
len := str.len(s)
str.Builder._grow(b, len)
mem.copy(s as ptr, b->data + b->size, len)
b->size = b->size + len
func str.Builder.build[b: str.Builder] : str
s := mem.alloc(b->size + 1) as str
mem.copy(b->data, s as ptr, b->size)
s[b->size] = 0
return s
func math.gcd[a: i64, b: i64] : i64
a = math.abs(a)
b = math.abs(b)
while b != 0
tmp := b
b = a % b
a = tmp
return a
func math.min[a: i64, b: i64] : i64
if a < b
return a
return b
func math.max[a: i64, b: i64] : i64
if a > b
return a
return b
func math.abs[n: i64] : i64
if n == -9223372036854775808
panic("MIN_I64 passed to math.abs")
if n < 0
return -n
return n
func math.sign[n: i64] : i64
if n < 0
return -1
else if n > 0
return 1
else
return 0
func math.pow[b: i64, e: i64] : i64
if e < 0
panic("negative exponent passed to math.pow")
out := 1
while e > 0
if e & 1
out = out * b
b = b * b
e = e >> 1
return out
func math.lcm[a: i64, b: i64] : i64
return (a / math.gcd(a, b)) * b
func math.isqrt[n: i64] : i64
if n < 0
panic("negative number passed to math.isqrt")
if n == 0 || n == 1
return n
guess := n
next_guess := (guess + n / guess) / 2
while next_guess < guess
guess = next_guess
next_guess = (guess + n / guess) / 2
return guess
func math.is_prime[n: i64]: bool
if n <= 1
return false
if n == 2 || n == 3
return true
if n % 2 == 0 || n % 3 == 0
return false
i := 5
while i * i <= n
if n % i == 0 || n % (i + 2) == 0
return false
i = i + 6
return true
struct Array
data: ptr
size: i64
capacity: i64
func array.new_preallocated_and_zeroed[size: i64] : Array
xs := new* Array
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
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
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
if xs->size == xs->capacity
new_capacity := 4
if xs->capacity != 0
new_capacity = xs->capacity * 2
xs->data = mem.realloc(xs->data, new_capacity * 8)
xs->capacity = new_capacity
mem.write64(xs->data + xs->size * 8, x)
xs->size = xs->size + 1
func array.free[xs: Array] : void
if xs->data != 0
mem.free(xs->data)
mem.free(xs)
func array.pop[xs: Array] : any
if xs->size == 0
panic("array.pop on empty array")
x : any = array.nth(xs, xs->size - 1)
xs->size = xs->size - 1
return x
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)
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)
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
const HashMap._TABLE_SIZE = 100
struct HashMap
table: Array
struct HashMap._Node
key: str
value: any
next: HashMap._Node
func HashMap.new[] : HashMap
map := new* HashMap
map->table = array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE)
return map
func HashMap.insert[map: HashMap, key: str, value: any] : void
index := HashMap._djb2(key)
current : HashMap._Node = array.nth(map->table, index)
while current as ptr
if str.equal(current->key, key)
current->value = value
return 0
current = current->next
new_node := new* HashMap._Node
new_node->key = str.make_copy(key)
new_node->value = value
new_node->next = array.nth(map->table, index)
array.set(map->table, index, new_node)
func HashMap.get[map: HashMap, key: str] : any, bool
index := HashMap._djb2(key)
current : HashMap._Node = array.nth(map->table, index)
while current as ptr
if str.equal(current->key, key)
return current->value, true
current = current->next
return 0 as any, false
func HashMap.delete[map: HashMap, key: str] : void
index := HashMap._djb2(key)
current : HashMap._Node = array.nth(map->table, index)
prev := 0 as HashMap._Node
while current as ptr
if str.equal(current->key, key)
if prev as ptr
prev->next = current->next
else
array.set(map->table, index, current->next)
mem.free(current->key)
mem.free(current)
return 0
prev = current
current = current->next
func HashMap._djb2[key: str] : i64
hash := 5381
key_len := str.len(key)
for i in 0..key_len
hash = ((hash << 5) + hash) + key[i]
hash = (hash & 0x7fffffffffffffff) // prevent negative
return hash % HashMap._TABLE_SIZE
func HashMap.free[map: HashMap] : void
for i in 0..HashMap._TABLE_SIZE
current : HashMap._Node = array.nth(map->table, i)
while current as ptr
tmp := current
current = current->next
mem.free(tmp->key)
mem.free(tmp)
array.free(map->table)
mem.free(map)
func alg.quicksort[arr: Array] : void
alg._do_quicksort(arr, 0, arr->size - 1)
func alg._do_quicksort[arr: Array, low: i64, high: i64] : void
if low < high
i := alg._partition(arr, low, high)
alg._do_quicksort(arr, low, i - 1)
alg._do_quicksort(arr, i + 1, high)
func alg._partition[arr: Array, low: i64, high: i64] : i64
pivot : i64 = array.nth(arr, high)
i := low - 1
for j in (low)..high
if array.nth(arr, j) as i64 <= pivot
i = i + 1
temp : i64 = array.nth(arr ,i)
array.set(arr, i, array.nth(arr, j))
array.set(arr, j, temp)
temp : i64 = array.nth(arr, i + 1)
array.set(arr, i + 1, array.nth(arr, high))
array.set(arr, high, temp)
return i + 1
func alg.count[arr: Array, item: any] : i64
count := 0
for i in 0..arr->size
if array.nth(arr, i) == item
count = count + 1
return count
func alg.map[arr: Array, fn: fnptr] : Array
out := array.new_preallocated_and_zeroed(arr->size)
for i in 0..arr->size
array.set(out, i, fn(array.nth(arr, i)))
return out
func alg.filter[arr: Array, fn: fnptr] : Array
out := []
for i in 0..arr->size
if fn(array.nth(arr, i))
array.push(out, array.nth(arr, i))
return out
func alg.reduce[arr: Array, fn: fnptr, acc: any] : any
for i in 0..arr->size
acc = fn(acc, array.nth(arr, i))
return acc
func os.exit[code: i64] : void
_builtin_syscall(SYS_exit, code)
func os.getpid[] : i64
return _builtin_syscall(SYS_getpid)
struct os.Timespec
tv_sec: i64
tv_nsec: i64
func os.sleep[ms: i64] : void
if ms < 0
panic("negative time passed to os.sleep")
req := new os.Timespec
req->tv_sec = ms / 1000
req->tv_nsec = (ms % 1000) * 1000000
_builtin_syscall(SYS_nanosleep, req, 0)
func os.urandom_buf[buf: ptr, n: i64] : void
pos := 0
while pos < n
ret := _builtin_syscall(SYS_getrandom, buf + pos, n - pos, 0)
if ret <= 0
panic("os.urandom_buf: syscall failed")
pos = pos + ret
func os.urandom[n: i64]: ptr
buf := mem.alloc(n)
os.urandom_buf(buf, n)
return buf
func os.urandom_i64[] : i64
n := 0
os.urandom_buf(^n, 8)
return n
struct os.Timeval
tv_sec: i64
tv_usec: i64
func os.time[] : i64
tv := new os.Timeval
_builtin_syscall(SYS_gettimeofday, tv, 0)
out := tv->tv_sec * 1000 + tv->tv_usec / 1000
return out
// voodoo magic
func os.run_shell_command[command: str] : i64, bool
pid := _builtin_syscall(SYS_fork)
if pid < 0
return -1, false
if pid == 0
argv := ["sh", "-c", command, 0] // gets freed after child process exits
_builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ())
os.exit(1)
else
status := 0
wp := _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
if wp < 0
return -1, false
st := status & 0xffffffff
if (st & 0x7f) == 0
return (st >> 8) & 0xff, true
else
return -(st & 0x7f), true
func os.list_directory[path: str] : Array, bool
fd := _builtin_syscall(SYS_openat, -100, path, O_RDONLY, 0)
if fd < 0
return 0 as Array, false
files := []
buf := _stackalloc(1024)
while true
n := _builtin_syscall(SYS_getdents64, fd, buf, 1024)
if n < 0
for i in 0..files->size
mem.free(array.nth(files, i))
array.free(files)
_builtin_syscall(SYS_close, fd)
return 0 as Array, false
else if n == 0
break
pos := 0
while pos < n
len := mem.read16(buf + pos + 16)
name : ptr = buf + pos + 19
if name[0]
skip := false
// skip if name is exactly '.' or '..'
if name[0] == '.'
if name[1] == 0
skip = true
else if name[1] == '.'
if name[2] == 0
skip = true
if !skip
array.push(files, str.make_copy(name as str))
pos = pos + len
_builtin_syscall(SYS_close, fd)
return files, true