pipe, return

This commit is contained in:
2025-05-30 22:20:42 +02:00
parent 397f87c242
commit cfe35bcc9d
4 changed files with 61 additions and 11 deletions

View File

@@ -26,6 +26,10 @@ impl CodegenX86_64 {
}
impl Codegen for CodegenX86_64 {
fn get_output(&self) -> String {
self.output.clone()
}
fn emit_prologue(&mut self) -> Result<(), Box<dyn Error>> {
writeln!(
&mut self.output,
@@ -55,10 +59,6 @@ print:
Ok(())
}
fn get_output(&self) -> String {
self.output.clone()
}
fn compile_stmt(&mut self, env: &mut Env, stmt: Stmt) -> Result<(), Box<dyn Error>> {
match stmt {
Stmt::Expression(expr) => self.compile_expr(env, expr)?,
@@ -121,9 +121,15 @@ print:
self.compile_stmt(env, *body)?;
writeln!(&mut self.output, " mov rax, 0")?;
writeln!(&mut self.output, " mov rsp, rbp")?;
writeln!(&mut self.output, " pop rbp")?;
writeln!(&mut self.output, " ret")?;
}
Stmt::Return(expr) => {
self.compile_expr(env, expr)?;
writeln!(&mut self.output, " mov rsp, rbp")?;
writeln!(&mut self.output, " pop rbp")?;
writeln!(&mut self.output, " mov rax, 0")?;
writeln!(&mut self.output, " ret")?;
}
}
@@ -245,10 +251,12 @@ print:
};
// TODO
assert!(args.len() == 1);
assert!(args.len() <= 1);
if args.len() == 1 {
self.compile_expr(env, args.first().unwrap().clone())?;
writeln!(&mut self.output, " mov rdi, rax")?;
}
self.compile_expr(env, args.first().unwrap().clone())?;
writeln!(&mut self.output, " mov rdi, rax")?;
writeln!(&mut self.output, " call {}", callee)?;
}
}