From 0bda3fb60d63c661070b7f647e2f811b75e2dd70 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Fri, 29 Nov 2024 01:17:12 -0700 Subject: [PATCH] Function invocations working, stack unwinding correctly --- output.stationeers | 14 ++++++++------ src/compiler/mod.rs | 2 +- tests/file.stlg | 8 ++++++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/output.stationeers b/output.stationeers index 8120724..e96b76f 100644 --- a/output.stationeers +++ b/output.stationeers @@ -7,12 +7,14 @@ push 1 push 2 push 3 j 1 -sub sp sp 3 +sub sp sp 0 +pop ra +j ra +push 14 +j 4 +sub sp sp 0 pop ra j ra main: -push 19 -push 1 -push 2 -push 3 -j 4 +push 20 +j 12 diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 943da9d..17609b1 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -172,7 +172,7 @@ impl<'a> Compiler<'a> { let return_addr = self.current_line + to_write.lines().count() + 2; self.write_output(format!("push {return_addr}"))?; self.output.write(to_write.as_bytes())?; - self.current_line = return_addr; + self.current_line = return_addr - 1; self.write_output(format!("j {function_line}"))?; diff --git a/tests/file.stlg b/tests/file.stlg index a7c6395..d1a863f 100644 --- a/tests/file.stlg +++ b/tests/file.stlg @@ -2,8 +2,12 @@ fn test(a, b, c) { }; -fn test2(x, y, z) { +fn test2() { test(1, 2, 3); }; -test2(1, 2, 3); \ No newline at end of file +fn test3() { + test2(); +}; + +test3(); \ No newline at end of file