array literals
This commit is contained in:
@@ -33,29 +33,29 @@ OS.listdir:
|
||||
mov rdi, r14
|
||||
call opendir
|
||||
mov r14, rax
|
||||
.LBB5_1:
|
||||
.OS.listdir.1:
|
||||
mov rdi, r14
|
||||
call readdir
|
||||
test rax, rax
|
||||
je .LBB5_7
|
||||
je .OS.listdir.3
|
||||
cmp byte [rax+19], 46
|
||||
jne .LBB5_6
|
||||
jne .OS.listdir.2
|
||||
movzx ecx, byte [rax+20]
|
||||
test ecx, ecx
|
||||
je .LBB5_1
|
||||
je .OS.listdir.1
|
||||
cmp ecx, 46
|
||||
jne .LBB5_6
|
||||
jne .OS.listdir.2
|
||||
cmp byte [rax+21], 0
|
||||
je .LBB5_1
|
||||
.LBB5_6:
|
||||
je .OS.listdir.1
|
||||
.OS.listdir.2:
|
||||
add rax, 19
|
||||
mov rdi, rax
|
||||
call strdup
|
||||
mov rsi, rax
|
||||
mov rdi, rbx
|
||||
call Array.push
|
||||
jmp .LBB5_1
|
||||
.LBB5_7:
|
||||
jmp .OS.listdir.1
|
||||
.OS.listdir.3:
|
||||
mov rdi, r14
|
||||
call closedir
|
||||
mov rax, rbx
|
||||
@@ -64,11 +64,6 @@ OS.listdir:
|
||||
pop r14
|
||||
ret
|
||||
|
||||
Array.new:
|
||||
mov rdi, 1
|
||||
mov rsi, 24
|
||||
jmp calloc
|
||||
|
||||
Array.nth:
|
||||
mov rax, [rdi]
|
||||
mov rax, [rax + rsi*8]
|
||||
@@ -88,7 +83,7 @@ Array.push:
|
||||
mov rax, [rdi]
|
||||
mov rcx, [rdi + 16]
|
||||
cmp rcx, [rdi + 8]
|
||||
jne .no_realloc
|
||||
jne .Array.push.1
|
||||
lea rdx, [rcx + rcx]
|
||||
mov rsi, 4
|
||||
test rcx, rcx
|
||||
@@ -99,7 +94,7 @@ Array.push:
|
||||
call realloc
|
||||
mov [rbx], rax
|
||||
mov rcx, [rbx + 16]
|
||||
.no_realloc:
|
||||
.Array.push.1:
|
||||
mov [rax + rcx*8], r14
|
||||
inc qword [rbx + 16]
|
||||
add rsp, 8
|
||||
@@ -119,34 +114,3 @@ Array.free:
|
||||
mov rdi, rbx
|
||||
pop rbx
|
||||
jmp free
|
||||
|
||||
Math.isqrt:
|
||||
xor rax, rax
|
||||
mov rcx, 1
|
||||
mov rbx, rdi
|
||||
shl rcx, 62
|
||||
.isqrt.1:
|
||||
cmp rcx, 0
|
||||
je .isqrt.5
|
||||
cmp rcx, rbx
|
||||
jbe .isqrt.2
|
||||
shr rcx, 2
|
||||
jmp .isqrt.1
|
||||
.isqrt.2:
|
||||
cmp rcx, 0
|
||||
je .isqrt.5
|
||||
mov rdx, rax
|
||||
add rdx, rcx
|
||||
cmp rbx, rdx
|
||||
jb .isqrt.3
|
||||
sub rbx, rdx
|
||||
shr rax, 1
|
||||
add rax, rcx
|
||||
jmp .isqrt.4
|
||||
.isqrt.3:
|
||||
shr rax, 1
|
||||
.isqrt.4:
|
||||
shr rcx, 2
|
||||
jmp .isqrt.2
|
||||
.isqrt.5:
|
||||
ret
|
||||
@@ -58,6 +58,7 @@ macro_rules! emit {
|
||||
}
|
||||
|
||||
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
||||
// TODO: currently they are all just 8 byte values
|
||||
static TYPES: [&str; 6] = ["I64", "String", "Bool", "Ptr", "Char", "Array"];
|
||||
|
||||
pub struct CodegenX86_64 {
|
||||
@@ -452,6 +453,18 @@ extern gettimeofday
|
||||
|
||||
emit!(&mut self.output, " call {}", callee);
|
||||
}
|
||||
Expr::ArrayLiteral(exprs) => {
|
||||
emit!(&mut self.output, " call Array.new");
|
||||
emit!(&mut self.output, " mov r12, rax");
|
||||
|
||||
for expr in exprs {
|
||||
self.compile_expr(env, expr)?;
|
||||
emit!(&mut self.output, " mov rsi, rax");
|
||||
emit!(&mut self.output, " mov rdi, r12");
|
||||
emit!(&mut self.output, " call Array.push");
|
||||
}
|
||||
emit!(&mut self.output, " mov rax, r12");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -66,6 +66,7 @@ pub enum Expr {
|
||||
paren: Token,
|
||||
args: Vec<Expr>,
|
||||
},
|
||||
ArrayLiteral(Vec<Expr>),
|
||||
}
|
||||
|
||||
pub struct Parser {
|
||||
@@ -436,6 +437,19 @@ impl Parser {
|
||||
let expr = self.expression()?;
|
||||
self.consume(TokenType::RightParen, "expected ')' after expression")?;
|
||||
Ok(Expr::Grouping(Box::new(expr)))
|
||||
} else if self.match_token(&[TokenType::LeftBracket]) {
|
||||
let mut xs = vec![];
|
||||
if !self.check(&TokenType::RightBracket) {
|
||||
loop {
|
||||
xs.push(self.expression()?);
|
||||
if !self.match_token(&[TokenType::Comma]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.consume(TokenType::RightBracket, "expected ']' after values")?;
|
||||
|
||||
Ok(Expr::ArrayLiteral(xs))
|
||||
} else if self.match_token(&[TokenType::Identifier]) {
|
||||
Ok(Expr::Variable(self.previous().clone()))
|
||||
} else {
|
||||
|
||||
20
src/std.zr
20
src/std.zr
@@ -95,6 +95,21 @@ func Math.pow[b: I64, e: I64] : I64
|
||||
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]: I64
|
||||
if n <= 1
|
||||
return false
|
||||
@@ -108,4 +123,7 @@ func Math.is_prime[n: I64]: I64
|
||||
if n % i == 0 || n % (i + 2) == 0
|
||||
return false
|
||||
i = i + 6
|
||||
return true
|
||||
return true
|
||||
|
||||
func Array.new[] : Array
|
||||
return calloc(1, 24)
|
||||
Reference in New Issue
Block a user