fix argument evaluation

This commit is contained in:
2025-06-03 13:01:37 +02:00
parent 178ad8b9c0
commit 946ab52a19

View File

@@ -533,19 +533,22 @@ Math.isqrt:
paren, paren,
args, args,
} => { } => {
// TODO: in function calls like a(1, b(2, 3)) the first argument will get overwritten when calling b
let callee = match *callee { let callee = match *callee {
Expr::Variable(name) => name.lexeme, Expr::Variable(name) => name.lexeme,
_ => return error!(&paren.loc, "tried to call a non-constant expression"), _ => return error!(&paren.loc, "tried to call a non-constant expression"),
}; };
for (i, arg) in args.iter().enumerate() { for arg in &args {
self.compile_expr(env, arg.clone())?; self.compile_expr(env, arg.clone())?;
emit!(&mut self.output, " push rax");
}
for i in (0..args.len()).rev() {
let reg = match REGISTERS.get(i) { let reg = match REGISTERS.get(i) {
Some(x) => x, Some(x) => x,
None => return error!(&paren.loc, "only up to 6 args allowed"), None => return error!(&paren.loc, "only up to 6 args allowed"),
}; };
emit!(&mut self.output, " mov {}, rax", reg,); emit!(&mut self.output, " pop {}", reg);
} }
emit!(&mut self.output, " call {}", callee); emit!(&mut self.output, " call {}", callee);