rewrite array builtins in zern
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
func main[] : I64
|
func main[] : I64
|
||||||
|
// https://brainfuck.org/sierpinski.b
|
||||||
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||||
let src_len: I64 = strlen(src)
|
let src_len: I64 = strlen(src)
|
||||||
let i: I64 = 0
|
let i: I64 = 0
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
func quicksort[arr: Array] : I64
|
func quicksort[arr: Array] : Void
|
||||||
do_quicksort(arr, 0, Array.size(arr)-1)
|
do_quicksort(arr, 0, Array.size(arr)-1)
|
||||||
|
|
||||||
func do_quicksort[arr: Array, low: I64, high: I64] : I64
|
func do_quicksort[arr: Array, low: I64, high: I64] : Void
|
||||||
if low < high
|
if low < high
|
||||||
let i: I64 = partition(arr, low, high)
|
let i: I64 = partition(arr, low, high)
|
||||||
do_quicksort(arr, low, i - 1)
|
do_quicksort(arr, low, i - 1)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ func main[] : I64
|
|||||||
String.set(sa, 1, 0)
|
String.set(sa, 1, 0)
|
||||||
String.set(sa, 2, Bit.rshift(port, 8) && 255)
|
String.set(sa, 2, Bit.rshift(port, 8) && 255)
|
||||||
String.set(sa, 3, port && 255)
|
String.set(sa, 3, port && 255)
|
||||||
// 23.192.228.80
|
// 23.192.228.80 (example.com)
|
||||||
String.set(sa, 4, 23)
|
String.set(sa, 4, 23)
|
||||||
String.set(sa, 5, 192)
|
String.set(sa, 5, 192)
|
||||||
String.set(sa, 6, 228)
|
String.set(sa, 6, 228)
|
||||||
|
|||||||
@@ -150,6 +150,16 @@ _builtin_deref64:
|
|||||||
mov rax, qword [rdi]
|
mov rax, qword [rdi]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
section .text._builtin_set8
|
||||||
|
_builtin_set8:
|
||||||
|
mov [rdi], sil
|
||||||
|
ret
|
||||||
|
|
||||||
|
section .text._builtin_set64
|
||||||
|
_builtin_set64:
|
||||||
|
mov [rdi], rsi
|
||||||
|
ret
|
||||||
|
|
||||||
section .text._builtin_stdin
|
section .text._builtin_stdin
|
||||||
_builtin_stdin:
|
_builtin_stdin:
|
||||||
mov rax, [rel stdin]
|
mov rax, [rel stdin]
|
||||||
@@ -169,11 +179,6 @@ _builtin_rshift:
|
|||||||
sar rax, cl
|
sar rax, cl
|
||||||
ret
|
ret
|
||||||
|
|
||||||
section .text._builtin_string_set
|
|
||||||
_builtin_string_set:
|
|
||||||
mov [rdi + rsi], dl
|
|
||||||
ret
|
|
||||||
|
|
||||||
section .text._builtin_listdir
|
section .text._builtin_listdir
|
||||||
_builtin_listdir:
|
_builtin_listdir:
|
||||||
push r14
|
push r14
|
||||||
@@ -215,56 +220,6 @@ _builtin_listdir:
|
|||||||
pop rbx
|
pop rbx
|
||||||
pop r14
|
pop r14
|
||||||
ret
|
ret
|
||||||
|
|
||||||
section .text._builtin_array_set
|
|
||||||
_builtin_array_set:
|
|
||||||
mov rax, [rdi]
|
|
||||||
mov [rax + rsi*8], rdx
|
|
||||||
ret
|
|
||||||
|
|
||||||
section .text._builtin_array_push
|
|
||||||
_builtin_array_push:
|
|
||||||
push r14
|
|
||||||
push rbx
|
|
||||||
push rax
|
|
||||||
mov r14, rsi
|
|
||||||
mov rbx, rdi
|
|
||||||
mov rax, [rdi]
|
|
||||||
mov rcx, [rdi + 16]
|
|
||||||
cmp rcx, [rdi + 8]
|
|
||||||
jne ._builtin_array_push.1
|
|
||||||
lea rdx, [rcx + rcx]
|
|
||||||
mov rsi, 4
|
|
||||||
test rcx, rcx
|
|
||||||
cmovnz rsi, rdx
|
|
||||||
mov [rbx + 8], rsi
|
|
||||||
shl rsi, 3
|
|
||||||
mov rdi, rax
|
|
||||||
call realloc
|
|
||||||
mov [rbx], rax
|
|
||||||
mov rcx, [rbx + 16]
|
|
||||||
._builtin_array_push.1:
|
|
||||||
mov [rax + rcx*8], r14
|
|
||||||
inc qword [rbx + 16]
|
|
||||||
add rsp, 8
|
|
||||||
pop rbx
|
|
||||||
pop r14
|
|
||||||
ret
|
|
||||||
|
|
||||||
section .text._builtin_array_size
|
|
||||||
_builtin_array_size:
|
|
||||||
mov rax, [rdi + 16]
|
|
||||||
ret
|
|
||||||
|
|
||||||
section .text._builtin_array_free
|
|
||||||
_builtin_array_free:
|
|
||||||
push rbx
|
|
||||||
mov rbx, rdi
|
|
||||||
mov rdi, [rdi]
|
|
||||||
call free
|
|
||||||
mov rdi, rbx
|
|
||||||
pop rbx
|
|
||||||
jmp free
|
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ pub enum Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: currently they are all just 8 byte values
|
// TODO: currently they are all just 8 byte values
|
||||||
static TYPES: [&str; 6] = ["U8", "I64", "String", "Bool", "Ptr", "Array"];
|
static TYPES: [&str; 7] = ["Void", "U8", "I64", "String", "Bool", "Ptr", "Array"];
|
||||||
|
|
||||||
pub struct Parser {
|
pub struct Parser {
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
|
|||||||
41
src/std.zr
41
src/std.zr
@@ -1,18 +1,18 @@
|
|||||||
func panic[msg: String] : I64
|
func panic[msg: String] : Void
|
||||||
printf("PANIC: %s\n", msg)
|
printf("PANIC: %s\n", msg)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
func print[x: String] : I64
|
func print[x: String] : Void
|
||||||
printf("%s\n", x)
|
printf("%s\n", x)
|
||||||
|
|
||||||
func print_i64[x: I64] : I64
|
func print_i64[x: I64] : Void
|
||||||
printf("%ld\n", x)
|
printf("%ld\n", x)
|
||||||
|
|
||||||
func String.nth[s: String, n: I64] : U8
|
func String.nth[s: String, n: I64] : U8
|
||||||
return _builtin_deref8(s + n)
|
return _builtin_deref8(s + n)
|
||||||
|
|
||||||
func String.set[s: String, n: I64, c: U8] : I64
|
func String.set[s: String, n: I64, c: U8] : Void
|
||||||
_builtin_string_set(s, n, c)
|
_builtin_set8(s+n, c)
|
||||||
|
|
||||||
func String.is_whitespace[c: U8] : Bool
|
func String.is_whitespace[c: U8] : Bool
|
||||||
return c == ' ' || c == 10 || c == 13 || c == 9
|
return c == ' ' || c == 10 || c == 13 || c == 9
|
||||||
@@ -84,7 +84,7 @@ func IO.read_file[path: String]: String
|
|||||||
fclose(file)
|
fclose(file)
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func IO.write_file[path: String, content: String] : I64
|
func IO.write_file[path: String, content: String] : Void
|
||||||
let file: Ptr = fopen(path, "wb")
|
let file: Ptr = fopen(path, "wb")
|
||||||
if !file
|
if !file
|
||||||
panic("failed to open file")
|
panic("failed to open file")
|
||||||
@@ -176,14 +176,33 @@ func Math.urandom[]: I64
|
|||||||
func Array.new[] : Array
|
func Array.new[] : Array
|
||||||
return calloc(1, 24)
|
return calloc(1, 24)
|
||||||
|
|
||||||
func Array.set[xs: Array, n: I64, x: I64] : I64
|
func Array.set[xs: Array, n: I64, x: I64] : Void
|
||||||
_builtin_array_set(xs, n, x)
|
let data: Ptr = _builtin_deref64(xs)
|
||||||
|
_builtin_set64(data+n*8, x)
|
||||||
|
|
||||||
func Array.push[xs: Array, x: I64] : I64
|
func Array.push[xs: Array, x: I64] : Void
|
||||||
return _builtin_array_push(xs, x)
|
let data: Ptr = _builtin_deref64(xs)
|
||||||
|
let capacity: I64 = _builtin_deref64(xs+8)
|
||||||
|
let size: I64 = _builtin_deref64(xs+16)
|
||||||
|
|
||||||
|
if size == capacity
|
||||||
|
let new_capacity: I64 = 4
|
||||||
|
if capacity != 0
|
||||||
|
new_capacity = capacity * 2
|
||||||
|
let new_data: Ptr = realloc(data, new_capacity * 8)
|
||||||
|
_builtin_set64(xs, new_data)
|
||||||
|
_builtin_set64(xs+8, new_capacity)
|
||||||
|
data = new_data
|
||||||
|
|
||||||
|
_builtin_set64(data+size*8, x)
|
||||||
|
_builtin_set64(xs+16, size + 1)
|
||||||
|
|
||||||
func Array.size[xs: Array] : I64
|
func Array.size[xs: Array] : I64
|
||||||
return _builtin_array_size(xs)
|
return _builtin_deref64(xs+16)
|
||||||
|
|
||||||
|
func Array.free[xs: Array] : Void
|
||||||
|
free(_builtin_deref64(xs))
|
||||||
|
free(xs)
|
||||||
|
|
||||||
func OS.time[] : I64
|
func OS.time[] : I64
|
||||||
let tv: Ptr = malloc(16)
|
let tv: Ptr = malloc(16)
|
||||||
|
|||||||
2
test.zr
2
test.zr
@@ -1,4 +1,4 @@
|
|||||||
func run_test[x: String] : I64
|
func run_test[x: String] : Void
|
||||||
printf("\033[93mBuilding %s...\033[0m", x)
|
printf("\033[93mBuilding %s...\033[0m", x)
|
||||||
let cmd: String = String.concat("./target/release/zern examples/", x)
|
let cmd: String = String.concat("./target/release/zern examples/", x)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user