some new std functions
This commit is contained in:
@@ -148,8 +148,7 @@ func mem.realloc[x: ptr, new_size: i64] : ptr
|
||||
if !new_ptr
|
||||
return 0
|
||||
|
||||
for i in 0..blk->size
|
||||
new_ptr[i] = x[i]
|
||||
mem.copy(x, new_ptr, blk->size)
|
||||
|
||||
mem.free(x)
|
||||
return new_ptr
|
||||
@@ -162,6 +161,10 @@ func mem.zero[x: ptr, size: i64] : void
|
||||
for i in 0..size
|
||||
x[i] = 0
|
||||
|
||||
func mem.copy[src: ptr, dst: ptr, n: i64] : void
|
||||
for i in 0..n
|
||||
dst[i] = src[i]
|
||||
|
||||
func mem.read8[x: ptr] : u8
|
||||
return x[0]
|
||||
|
||||
@@ -196,6 +199,12 @@ func io.println[x: str] : void
|
||||
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
|
||||
let s: str = str.from_i64(x)
|
||||
io.print(s)
|
||||
@@ -226,6 +235,13 @@ func io.read_line[]: str
|
||||
buffer[n] = 0
|
||||
return buffer
|
||||
|
||||
func io.file_exists[path: str] : bool
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd >= 0
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
return true
|
||||
return false
|
||||
|
||||
func io.read_file[path: str] : str
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd <= 0
|
||||
@@ -266,6 +282,14 @@ func io.write_file[path: str, content: str] : void
|
||||
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
|
||||
func io.write_binary_file[path: str, content: ptr, size: i64] : void
|
||||
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||
if fd < 0
|
||||
panic("failed to open file") // TODO
|
||||
|
||||
_builtin_syscall(SYS_write, fd, content, size)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
|
||||
func str.len[s: str] : i64
|
||||
let i = 0
|
||||
while s[i]
|
||||
@@ -275,8 +299,7 @@ func str.len[s: str] : i64
|
||||
func str.copy[s: str] : str
|
||||
let size: i64 = str.len(s) + 1
|
||||
let dup: str = mem.alloc(size)
|
||||
for i in 0..size
|
||||
dup[i] = s[i]
|
||||
mem.copy(s, dup, size)
|
||||
return dup
|
||||
|
||||
func str.equal[a: str, b: str] : bool
|
||||
@@ -312,13 +335,14 @@ func str.concat[a: str, b: str] : str
|
||||
let a_len: i64 = str.len(a)
|
||||
let b_len: i64 = str.len(b)
|
||||
let out: str = mem.alloc(a_len + b_len + 1)
|
||||
for i in 0..a_len
|
||||
out[i] = a[i]
|
||||
for i in 0..b_len
|
||||
out[a_len + i] = b[i]
|
||||
mem.copy(a, out, a_len)
|
||||
mem.copy(b, out + 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
|
||||
let haystack_len: i64 = str.len(haystack)
|
||||
let needle_len: i64 = str.len(needle)
|
||||
@@ -341,8 +365,7 @@ func str.substr[s: str, start: i64, length: i64] : str
|
||||
panic("str.substr out of bounds")
|
||||
|
||||
let out: str = mem.alloc(length + 1)
|
||||
for i in 0..length
|
||||
out[i] = s[start + i]
|
||||
mem.copy(s + start, out, length)
|
||||
out[length] = 0
|
||||
return out
|
||||
|
||||
@@ -690,6 +713,17 @@ func alg.reduce[arr: Array, fn: ptr, acc: any] : any
|
||||
func os.exit[code: i64] : void
|
||||
_builtin_syscall(SYS_exit, code)
|
||||
|
||||
func os.getpid[] : i64
|
||||
return _builtin_syscall(SYS_getpid)
|
||||
|
||||
func os.sleep[ms: i64] : void
|
||||
// TODO: we really shouldnt need a heap allocation to sleep
|
||||
let req: os.Timeval = new os.Timeval
|
||||
req->tv_sec = ms / 1000
|
||||
req->tv_usec = (ms % 1000) * 1000000
|
||||
_builtin_syscall(SYS_nanosleep, req, 0)
|
||||
mem.free(req)
|
||||
|
||||
func os.urandom[n: i64]: ptr
|
||||
let buffer: ptr = mem.alloc(n)
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
||||
@@ -708,6 +742,7 @@ struct os.Timeval
|
||||
tv_usec: i64
|
||||
|
||||
func os.time[] : i64
|
||||
// TODO: we really shouldnt need a heap allocation to check the time
|
||||
let tv: os.Timeval = new os.Timeval
|
||||
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||
let out: i64 = tv->tv_sec * 1000 + tv->tv_usec / 1000
|
||||
|
||||
Reference in New Issue
Block a user