From 33e86c66ce739913d453808d3fecd6670a0e9fe1 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sun, 30 Jan 2022 01:10:35 -0500 Subject: Functions, yay! We now support function calls! We don't have support for forward declaring functions right now though, so no mutual recursion is possible. The arguments are passed via the stack instead of through registers (unlike the x86_64 calling convention, I think). We'll probably need some sort of primitives built into the language for syscalls eventually because of this. Return types are also not checked, and right now it's possible to have a function that doesn't return anything even when the caller expects it to, error checking and reporting definitely needs to be improved. --- examples/functions.cup | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 examples/functions.cup (limited to 'examples') diff --git a/examples/functions.cup b/examples/functions.cup new file mode 100644 index 0000000..89858c6 --- /dev/null +++ b/examples/functions.cup @@ -0,0 +1,13 @@ +fn rec_sum(n: int, accum: int): int { + if (n == 0) + return accum; + return rec_sum(n - 1, accum + n); +} + +fn sum(n: int): int { + return rec_sum(n, 0); +} + +fn main() { + return sum(10); +} \ No newline at end of file -- cgit v1.2.3