summaryrefslogtreecommitdiff
path: root/src/core/functions.c
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-02-19 10:13:51 +0200
committerPaul Buetow <paul@buetow.org>2026-02-19 10:13:51 +0200
commit2e81f599a0323e2025883bc1375bf438d1406733 (patch)
tree8083216541adfb688e47d1c2dfbf8010ef34f23a /src/core/functions.c
parent3ad7831662b8f4c6ffd4666169b8edebc1391e36 (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'src/core/functions.c')
-rw-r--r--src/core/functions.c33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/core/functions.c b/src/core/functions.c
index 792065a..76b4eed 100644
--- a/src/core/functions.c
+++ b/src/core/functions.c
@@ -971,9 +971,38 @@ function_process_self_defined(Interpret *p_interpret, Token *p_token_ident) {
break;
case SYM_FUNCTION:
{
- List *p_list_token = symbol_get_val(p_symbol);
+ FuncDef *p_funcdef = (FuncDef*) symbol_get_val(p_symbol);
scope_up(p_interpret->p_scope);
- interpret_subprocess(p_interpret, p_list_token);
+
+ /* Bind each named parameter to its argument value.
+ * Args were pushed left-to-right so the last arg is on top (LIFO).
+ * Reverse-fill a temp array so index 0 = first param = first arg. */
+ if (p_funcdef->i_nparams > 0) {
+ Token **pp_args = malloc(
+ sizeof(Token*) * p_funcdef->i_nparams);
+
+ for (int i = p_funcdef->i_nparams - 1; i >= 0; --i)
+ pp_args[i] = stack_pop(p_interpret->p_stack);
+
+ ListIterator *p_iter = listiterator_new(p_funcdef->p_params);
+
+ for (int i = 0; i < p_funcdef->i_nparams; ++i) {
+ char *c_name = (char*) listiterator_next(p_iter);
+ Symbol *p_sym = symbol_new(SYM_VARIABLE, pp_args[i]);
+ scope_newset(p_interpret->p_scope, c_name, p_sym);
+ }
+
+ listiterator_delete(p_iter);
+ free(pp_args);
+ }
+
+ interpret_subprocess(p_interpret, p_funcdef->p_body);
+
+ /* Consume CONTROL_RET here; return value(s) are already on the stack
+ * (merged by interpret_subprocess when CONTROL_RET was active). */
+ if (p_interpret->ct == CONTROL_RET)
+ p_interpret->ct = CONTROL_NONE;
+
scope_down(p_interpret->p_scope);
}
NO_DEFAULT;