Files
zern/src/std.zr
2025-11-22 21:06:00 +01:00

528 lines
13 KiB
Plaintext

func dbg.panic[msg: String] : Void
io.print("PANIC: ")
io.println(msg)
os.exit(1)
func mem.alloc[x: I64] : Ptr
return c.malloc(x)
func mem.free[x: Ptr] : Void
c.free(x)
func mem.zero[x: I64, size: I64] : Void
for i in 0..size
mem.write8(x + i, 0)
func mem.read8[x: Ptr] : U8
return _builtin_read8(x)
func mem.read16[x: Ptr] : I64
return _builtin_read16(x)
func mem.read32[x: Ptr] : I64
return _builtin_read32(x)
func mem.read64[x: Ptr] : I64
return _builtin_read64(x)
func mem.write8[x: Ptr, d: U8] : Void
_builtin_set8(x, d)
func mem.write64[x: Ptr, d: I64] : Void
_builtin_set64(x, d)
func io.print[x: String] : Void
_builtin_syscall(1, 1, x, str.len(x))
func io.println[x: String] : Void
io.print(x)
io.print("\n")
func io.print_sized[x: String, size: I64] : Void
_builtin_syscall(1, 1, x, size)
func io.print_char[x: U8] : Void
let s: String = mem.alloc(1)
str.set(s, 0, x)
_builtin_syscall(1, 1, s, 1)
mem.free(s)
func io.print_i64[x: I64] : Void
let s: String = str.from_i64(x)
io.print(s)
mem.free(s)
func io.println_i64[x: I64] : Void
let s: String = str.from_i64(x)
io.println(s)
mem.free(s)
func io.read_char[] : U8
let s: String = mem.alloc(1)
str.set(s, 0, 0)
_builtin_syscall(0, 0, s, 1)
let c: U8 = s[0]
mem.free(s)
return c
func io.read_stdin[]: String
let buffer: String = mem.alloc(1025)
let n: I64 = _builtin_syscall(0, 0, buffer, 1024)
if n < 0
return ""
str.set(buffer, n, 0)
return buffer
func io.read_file[path: String]: String
let fd: I64 = _builtin_syscall(2, path, 0, 0) // open
if fd <= 0
dbg.panic("failed to open file")
let size: I64 = _builtin_syscall(8, fd, 0, 2) // lseek to the end
_builtin_syscall(8, fd, 0, 0) // lseek back to start
let buffer: String = mem.alloc(size + 1)
let n: I64 = _builtin_syscall(0, fd, buffer, size) // read
str.set(buffer, n, 0)
_builtin_syscall(3, fd) // close
return buffer
func io.write_file[path: String, content: String] : Void
let fd: Ptr = _builtin_syscall(2, path, 0x241, 0o644) // open
if fd < 0
dbg.panic("failed to open file")
_builtin_syscall(1, fd, content, str.len(content)) // write
_builtin_syscall(3, fd) // close
func str.len[s: String] : I64
let i: I64 = 0
while mem.read8(s + i)
i = i + 1
return i
func str.copy[s: String] : String
let size: I64 = str.len(s) + 1
let dup: String = mem.alloc(size)
for i in 0..size
str.set(dup, i, s[i])
return dup
func str.set[s: String, n: I64, c: U8] : Void
mem.write8(s + n, c)
func str.equal[a: String, b: String] : Bool
let i: I64 = 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 == 10 | x == 13 | x == 9
func str.concat[a: String, b: String] : String
let a_len: I64 = str.len(a)
let b_len: I64 = str.len(b)
let out: String = mem.alloc(a_len + b_len + 1)
for i in 0..a_len
str.set(out, i, a[i])
for i in 0..b_len
str.set(out, a_len + i, b[i])
str.set(out, a_len + b_len, 0)
return out
func str.find[s: String, c: U8] : I64
let s_len: I64 = str.len(s)
for i in 0..s_len
if s[i] == c
return i
return -1
func str.substr[s: String, start: I64, length: I64] : String
if start < 0 | length < 0 | start + length > str.len(s)
dbg.panic("str.substr out of bounds")
let out: String = mem.alloc(length + 1)
for i in 0..length
str.set(out, i, s[start + i])
str.set(out, length, 0)
return out
func str.trim[s: String] : String
let start: I64 = 0
let end: I64 = str.len(s) - 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: String, needle: String]: Array
let haystack_len: I64 = str.len(haystack)
let needle_len: I64 = str.len(needle)
let result: Array = []
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
let start: I64 = 0
let i: I64 = 0
while i < haystack_len
if i <= haystack_len - needle_len
let match: Bool = 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: String] : String
let len: I64 = str.len(s)
let out: String = mem.alloc(len + 1)
for i in 0..len
str.set(out, i, s[len - i - 1])
str.set(out, len, 0)
return out
// not sure this covers all wacky edge cases
func str.from_i64[n: I64] : String
if n == 0
let s: String = mem.alloc(2)
str.set(s, 0, '0')
str.set(s, 1, 0)
return s
let neg: Bool = n < 0
if neg
n = -n
let buf: String = mem.alloc(21)
let i: I64 = 0
while n > 0
let d: U8 = n % 10
str.set(buf, i, '0' + d)
n = n / 10
i = i + 1
if neg
str.set(buf, i, '-')
i = i + 1
str.set(buf, i, 0)
let s: String = str.reverse(buf)
mem.free(buf)
return s
func str.parse_i64[s: String] : I64
let len: I64 = str.len(s)
let i: I64 = 0
let sign: I64 = 1
if i < len & s[i] == '-'
sign = -1
i = i + 1
let num: I64 = 0
while i < len
let d: U8 = s[i]
if d < '0' | d > '9'
break
num = num * 10 + (d - '0')
i = i + 1
return num * sign
func str.hex_encode[s: String] : String
let hex_chars: String = "0123456789abcdef"
let s_len: I64 = str.len(s)
let j: I64 = 0
let out: String = mem.alloc(s_len * 2 + 1)
for i in 0..s_len
let high: U8 = (s[i] >> 4) & 15
let low: U8 = s[i] & 15
str.set(out, j, hex_chars[high])
str.set(out, j + 1, hex_chars[low])
j = j + 2
str.set(out, j, 0)
return out
func str._from_hex_digit[d: U8] : I64
if d >= 'a' & d <= 'f'
return d - 'a' + 10
if d >= 'A' & d <= 'F'
return d - 'A' + 10
return d - '0'
func str.hex_decode[s: String] : String
let s_len: I64 = str.len(s)
let i: I64 = 0
let j: I64 = 0
let out: String = mem.alloc(s_len / 2 + 1)
while i < s_len
str.set(out, j, str._from_hex_digit(s[i]) * 16 + str._from_hex_digit(s[i + 1]))
i = i + 2
j = j + 1
str.set(out, j, 0)
return out
func math.gcd[a: I64, b: I64] : I64
while b != 0
let tmp: I64 = 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 < 0
return -n
return n
func math.pow[b: I64, e: I64] : I64
let out: I64 = 1
for i in 0..e
out = out * b
return out
func math.lcm[a: I64, b: I64] : I64
return (a * b) / math.gcd(a, b)
func math.isqrt[n: I64] : I64
if n < 0
return -1
if n == 0 | n == 1
return n
let guess: I64 = n
let next_guess: I64 = (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
let i: I64 = 5
while i * i <= n
if n % i == 0 | n % (i + 2) == 0
return false
i = i + 6
return true
func array.new[] : Array
let arr: Ptr = mem.alloc(24)
mem.zero(arr, 24)
return arr
func array.nth[xs: Array, n: I64] : I64
// this probably should be implemented in the codegen
if n < 0 | n >= array.size(xs)
dbg.panic("array.nth out of bounds")
let data: Ptr = mem.read64(xs)
return mem.read64(data + n * 8)
func array.set[xs: Array, n: I64, x: I64] : Void
let data: Ptr = mem.read64(xs)
mem.write64(data + n * 8, x)
func array.push[xs: Array, x: I64] : Void
let data: Ptr = mem.read64(xs)
let capacity: I64 = mem.read64(xs + 8)
let size: I64 = mem.read64(xs + 16)
if size == capacity
let new_capacity: I64 = 4
if capacity != 0
new_capacity = capacity * 2
let new_data: Ptr = c.realloc(data, new_capacity * 8)
mem.write64(xs, new_data)
mem.write64(xs + 8, new_capacity)
data = new_data
mem.write64(data + size * 8, x)
mem.write64(xs + 16, size + 1)
func array.size[xs: Array] : I64
return mem.read64(xs + 16)
func array.free[xs: Array] : Void
let data: Ptr = mem.read64(xs)
if data != 0
mem.free(data)
mem.free(xs)
func alg.quicksort[arr: Array] : Void
alg._do_quicksort(arr, 0, array.size(arr) - 1)
func alg._do_quicksort[arr: Array, low: I64, high: I64] : Void
if low < high
let i: I64 = 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
let pivot: I64 = array.nth(arr, high)
let i: I64 = low - 1
for j in (low)..high
if array.nth(arr, j) <= pivot
i = i + 1
let temp: I64 = array.nth(arr ,i)
array.set(arr, i, array.nth(arr, j))
array.set(arr, j, temp)
let 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 os.exit[code: I64] : Void
_builtin_syscall(60, code)
func os.urandom[]: I64
let buffer: Ptr = mem.alloc(8)
let fd: I64 = _builtin_syscall(2, "/dev/urandom", 0, 0) // open
_builtin_syscall(0, fd, buffer, 8) // read
_builtin_syscall(3, fd) // close
let n: I64 = mem.read64(buffer)
mem.free(buffer)
return n
func os.time[] : I64
let tv: Ptr = mem.alloc(16)
_builtin_syscall(96, tv, 0) // gettimeofday
let seconds: I64 = mem.read64(tv)
let microseconds: I64 = mem.read64(tv + 8)
mem.free(tv)
return seconds * 1000 + microseconds / 1000
func os.listdir[path: String] : Array
let fd: I64 = _builtin_syscall(2, path, 0, 0) // open
if fd < 0
return []
let files: Array = []
let buf: Ptr = mem.alloc(1024)
while true
let n: I64 = _builtin_syscall(217, fd, buf, 1024) // getdents64
if n <= 0
break
let pos: I64 = 0
while pos < n
let len: I64 = mem.read16(buf + pos + 16)
let name: String = buf + pos + 19
if name[0]
let skip: Bool = 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.copy(name))
pos = pos + len
mem.free(buf)
_builtin_syscall(3, fd) // close
return files
func net.listen[port: I64] : I64
let s: I64 = _builtin_syscall(41, 2, 1, 0) // socket
if s < 0
return -1
let sa: Ptr = mem.alloc(16)
mem.zero(sa, 16)
mem.write8(sa + 0, 2)
mem.write8(sa + 1, 0)
mem.write8(sa + 2, (port >> 8) & 255)
mem.write8(sa + 3, port & 255)
if _builtin_syscall(49, s, sa, 16) < 0 // bind
_builtin_syscall(3, s) // close
return -1
mem.free(sa)
if _builtin_syscall(50, s, 1) < 0 // listen
_builtin_syscall(3, s) // close
return -1
return s
func net.connect[host: String, port: I64] : I64
let he: Ptr = c.gethostbyname(host)
if he == 0
return -1
let ip_ptr: Ptr = mem.read64(mem.read64(he + 24))
let s: I64 = _builtin_syscall(41, 2, 1, 0) // socket
if s < 0
return -1
let sa: Ptr = mem.alloc(16)
mem.zero(sa, 16)
mem.write8(sa + 0, 2)
mem.write8(sa + 2, (port >> 8) & 255)
mem.write8(sa + 3, port & 255)
mem.write8(sa + 4, ip_ptr[0])
mem.write8(sa + 5, ip_ptr[1])
mem.write8(sa + 6, ip_ptr[2])
mem.write8(sa + 7, ip_ptr[3])
if _builtin_syscall(42, s, sa, 16) < 0 // connect
mem.free(sa)
_builtin_syscall(3, s) // close
return -1
mem.free(sa)
return s
func net.accept[s: I64] : I64
return _builtin_syscall(43, s, 0, 0)
func net.send[s: I64, data: String, size: I64] : Void
_builtin_syscall(44, s, data, size, 0, 0, 0)
func net.read[s: I64, buffer: Ptr, size: I64] : I64
return _builtin_syscall(0, s, buffer, size)
func net.close[s: I64] : Void
_builtin_syscall(3, s)