From 2e81f599a0323e2025883bc1375bf438d1406733 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 19 Feb 2026 10:13:51 +0200 Subject: Implement function named arguments, ret, and multiple return values - Add FuncDef struct to symbol.h/symbol.c: holds body token list, strdup'd param names, and param count; replaces the bare List* that SYM_FUNCTION used to store; funcdef_delete frees param strings and body list (tokens are GC-managed) - Add CONTROL_RET to ControlType so ret can propagate cleanly through the interpreter's control-flow stack - Update _func_decl to parse optional (p1, p2, ...) param list and store a FuncDef* instead of a raw List* - Implement case TT_RET in _control: clears intermediate stack values, evaluates comma-separated return expressions, sets CONTROL_RET - Propagate CONTROL_RET in all loop bodies (while/until, loop, do) without clearing it; while/until rescues return values from the temporary condition stack before it is destroyed - Update _term call site to support parenthesised func(arg1, arg2) syntax alongside the existing no-parens style for backward compat - Modify interpret_subprocess to merge the sub's stack into the parent when CONTROL_RET is active so return values survive teardown - Update function_process_self_defined SYM_FUNCTION case to pop and bind named args, run the body, then clear CONTROL_RET - Add -D_POSIX_C_SOURCE=200809L to Makefile CFLAGS for strdup under -std=c99 -pedantic - Add examples/func_args_ret.fy covering zero-arg, single-arg, two-arg, conditional ret, multiple return values, and old-style no-parens backward compat Co-Authored-By: Claude Sonnet 4.6 --- examples/all-examples.txt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ examples/func_args_ret.fy | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 examples/func_args_ret.fy (limited to 'examples') diff --git a/examples/all-examples.txt b/examples/all-examples.txt index 966fc52..6e02967 100644 --- a/examples/all-examples.txt +++ b/examples/all-examples.txt @@ -202,6 +202,52 @@ ifnot pid { say "I am the child process"; } +# Test: function named arguments, explicit ret, and multiple return values + +# zero-arg function with explicit return +func answer() { + ret 42; +} +assert 42 == say answer(); + +# single-arg function — factorial with a while loop and ret +func factorial(n) { + my result = 1; + while n > 1 { + result = result * n; + decr n; + } + ret result; +} +assert 120 == say factorial(5); + +# two-arg function +func add(a, b) { + ret a + b; +} +assert 8 == say add(3, 5); + +# conditional return inside if +func absval(n) { + if n < 0 { ret neg n; } + ret n; +} +assert 5 == say absval(5); +assert 5 == say absval(neg 5); + +# multiple return values — both land on the caller's stack +func minmax(a, b) { + if a < b { ret a, b; } + ret b, a; +} +say minmax(3, 7); + +# old-style zero-arg function without parens still works +func greet { + say "hello"; +} +greet; + #* * Examples of how to use functions *# diff --git a/examples/func_args_ret.fy b/examples/func_args_ret.fy new file mode 100644 index 0000000..54088e8 --- /dev/null +++ b/examples/func_args_ret.fy @@ -0,0 +1,45 @@ +# Test: function named arguments, explicit ret, and multiple return values + +# zero-arg function with explicit return +func answer() { + ret 42; +} +assert 42 == say answer(); + +# single-arg function — factorial with a while loop and ret +func factorial(n) { + my result = 1; + while n > 1 { + result = result * n; + decr n; + } + ret result; +} +assert 120 == say factorial(5); + +# two-arg function +func add(a, b) { + ret a + b; +} +assert 8 == say add(3, 5); + +# conditional return inside if +func absval(n) { + if n < 0 { ret neg n; } + ret n; +} +assert 5 == say absval(5); +assert 5 == say absval(neg 5); + +# multiple return values — both land on the caller's stack +func minmax(a, b) { + if a < b { ret a, b; } + ret b, a; +} +say minmax(3, 7); + +# old-style zero-arg function without parens still works +func greet { + say "hello"; +} +greet; -- cgit v1.2.3