rewrite listdir and stdin builtins in zern

This commit is contained in:
2025-07-08 17:10:39 +02:00
parent 8722e226a9
commit dfec298823
3 changed files with 26 additions and 60 deletions

View File

@@ -7,7 +7,7 @@ func main[] : I64
let s: I64 = 1
let j: I64 = 0
while j < 13
s = s * U8.parse_i64(String.nth(n, i + j))
s = s * (String.nth(n, i + j) - '0')
j = j + 1
if s > out
out = s

View File

@@ -114,7 +114,7 @@ extern strcat
extern strcpy
extern strdup
extern strncpy
extern fgets
extern syscall
extern fopen
extern fseek
extern ftell
@@ -160,11 +160,6 @@ _builtin_set64:
mov [rdi], rsi
ret
section .text._builtin_stdin
_builtin_stdin:
mov rax, [rel stdin]
ret
section .text._builtin_lshift
_builtin_lshift:
mov rcx, rsi
@@ -178,50 +173,6 @@ _builtin_rshift:
mov rax, rdi
sar rax, cl
ret
section .text._builtin_listdir
_builtin_listdir:
push r14
push rbx
push rax
mov r14, rdi
call Array.new
mov rbx, rax
mov rdi, r14
call opendir
mov r14, rax
._builtin_listdir.1:
mov rdi, r14
call readdir
test rax, rax
je ._builtin_listdir.3
cmp byte [rax+19], 46
jne ._builtin_listdir.2
movzx ecx, byte [rax+20]
test ecx, ecx
je ._builtin_listdir.1
cmp ecx, 46
jne ._builtin_listdir.2
cmp byte [rax+21], 0
je ._builtin_listdir.1
._builtin_listdir.2:
add rax, 19
mov rdi, rax
call strdup
mov rsi, rax
mov rdi, rbx
mov [rsp], rbx
call Array.push
mov rbx, [rsp]
jmp ._builtin_listdir.1
._builtin_listdir.3:
mov rdi, r14
call closedir
mov rax, rbx
add rsp, 8
pop rbx
pop r14
ret
"
);
Ok(())

View File

@@ -60,12 +60,11 @@ func String.rev[s: String] : String
String.set(out, len, 0)
return out
func IO.stdin[]: Ptr
return _builtin_stdin()
func IO.read_line[]: String
let buffer: String = malloc(1024)
fgets(buffer, 1024, IO.stdin())
let sys_read: I64 = 0
let stdin: I64 = 0
syscall(sys_read, stdin, buffer, 1024)
return buffer
func IO.read_file[path: String]: String
@@ -92,9 +91,6 @@ func IO.write_file[path: String, content: String] : Void
fwrite(content, 1, strlen(content), file)
fclose(file)
func U8.parse_i64[c: U8]: I64
return c - '0'
func I64.to_string[n: I64] : String
let x: String = malloc(21)
sprintf(x, "%ld", n)
@@ -213,7 +209,26 @@ func OS.time[] : I64
return seconds * 1000 + microseconds / 1000
func OS.listdir[path: String] : Array
return _builtin_listdir(path)
let dir: Ptr = opendir(path)
let files: Array = []
while true
let entry: Ptr = readdir(dir)
if entry == 0
break
let skip: Bool = false
if String.nth(entry, 19) == '.'
if String.nth(entry, 20) == 0
skip = true
else if String.nth(entry, 20) == '.'
if String.nth(entry, 21) == 0
skip = true
if !skip
Array.push(files, strdup(entry + 19))
closedir(dir)
return files
func Bit.lshift[a: I64, b: I64] : I64
return _builtin_lshift(a, b)
@@ -242,7 +257,7 @@ func Crypto.from_hex_digit[d: U8] : I64
return d - 'a' + 10
if d >= 'A' && d <= 'F'
return d - 'A' + 10
return U8.parse_i64(d)
return d - '0'
func Crypto.hex_decode[s: String] : String
let s_len: I64 = strlen(s)