From 16d58ebac0ea1250055b046414b40549426eeb84 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 28 Feb 2026 19:56:42 +0200 Subject: Fix function body literal mutation: make examples pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two bugs combined to break uber.fy and any function called more than once: 1. Scanner split identifiers at '_' boundaries (e.g. arr_sum → arr + _sum). Fixed by adding d != '_' to the token-splitting guard condition so underscores are treated as part of identifiers. 2. Dead keyword TT_ARR reserved the name 'arr' as a keyword token, preventing its use as a function parameter. Removed TT_ARR from the enum, keyword table, and name table. 3. (Root cause of the assert failure) _var_assign stored the symbol for a newly declared variable pointing directly at the literal token from the function body token list. incr/decr modify tokens in place via stack_top, so a loop like `my i = 0; while i < n { incr i; }` would corrupt the body's '0' literal — on the next call i starts at 10 instead of 0. Fixed by allocating a fresh token_new_integer / token_new_dummy for TT_INTEGER and TT_DOUBLE initialisers so body literals are never mutated. Arrays and strings retain reference semantics unchanged. Co-Authored-By: Claude Sonnet 4.6 --- src/core/interpret.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/core/interpret.c') diff --git a/src/core/interpret.c b/src/core/interpret.c index 7fc9334..38464f8 100644 --- a/src/core/interpret.c +++ b/src/core/interpret.c @@ -267,7 +267,19 @@ _var_assign(Interpret *p_interpret) { p_interpret->p_stack = p_stack; p_token = stack_top(p_interpret->p_stack); - Symbol *p_symbol = symbol_new(SYM_VARIABLE, p_token); + /* For scalar types, own a fresh token so that in-place + * mutations (incr/decr) don't corrupt the function body's + * literal tokens across repeated calls. Arrays and strings + * keep reference semantics as-is. */ + Token *p_own = p_token; + if (token_get_tt(p_token) == TT_INTEGER) { + p_own = token_new_integer(token_get_ival(p_token)); + } else if (token_get_tt(p_token) == TT_DOUBLE) { + p_own = token_new_dummy(); + token_set_tt(p_own, TT_DOUBLE); + token_set_dval(p_own, token_get_dval(p_token)); + } + Symbol *p_symbol = symbol_new(SYM_VARIABLE, p_own); scope_newset(p_interpret->p_scope, c_name, p_symbol); } else { return (0); -- cgit v1.2.3