summaryrefslogtreecommitdiff
path: root/src/core/functions.c
AgeCommit message (Collapse)Author
2026-02-28modify headersHEADmasterPaul Buetow
2026-02-28Decouple functions.c from interpreter array-LHS private state [Coupling]Paul Buetow
functions.c::_op_assign() was reading p_interpret->p_token_array_lhs and p_interpret->i_array_lhs_index directly — a hidden cross-module coupling where the functions module reached into interpreter-private state. Pass the array-LHS context as explicit parameters instead: - _op_assign() gains Token *p_array_lhs, int i_lhs_idx params - _process() propagates them to _op_assign() - function_process() propagates them from the call site In interpret.c the "restore" lines that set p_interpret->p_token_array_lhs and p_interpret->i_array_lhs_index before calling function_process() are removed; instead p_lhs and i_lhs_idx are passed directly. The three non-array-LHS call sites pass NULL / 0. _op_assign() no longer touches Interpret internals for the array assignment path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-28Refactor _process() by extracting per-operator handler functions [SRP]Paul Buetow
_process() was a 510-line function with a nested switch(operator) x switch(type) structure. Extracted 17 static helper functions: - _resolve_composite_op(): maps two-token operator pairs (!=, ==, <=, >=, <<, >>) to their canonical single TokenType - _op_assign(): handles variable and array-element assignment - _op_add(), _op_sub(), _op_mult(), _op_div(): arithmetic operators - _op_eq(), _op_neq(), _op_lt(), _op_gt(), _op_le(), _op_ge(): comparison operators (result always TT_INTEGER) - _op_and(), _op_or(), _op_xor(), _op_lshift(), _op_rshift(): bitwise operators (doubles/strings coerced to int) _process() is now a ~70-line dispatcher. Assignment is guarded by `else if` so it only fires when p_token_op2 == NULL, preserving the original semantics exactly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-28Replace built-in strcmp chains with dispatch table in functions.cPaul Buetow
Extract each of the 21 built-in function handlers (assert, decr, double, end, exit, fork, gc, incr, ind, integer, len, ln, neg, no, not, put, refs, say, scope, string, yes) into individual static _builtin_XXX() functions, each focused on a single responsibility. Add a null-terminated BuiltinEntry g_builtins[] dispatch table mapping name strings to handler function pointers. Rewrite function_is_buildin() and function_process_buildin() to iterate the table instead of maintaining parallel 21-branch strcmp chains. This satisfies OCP (adding a built-in only requires a new table row), SRP (each handler owns exactly one built-in's logic), and DRY (name lookup done in one place). The array special-case (len/ind on TT_ARRAY vs element-wise iteration) is preserved inline before the table dispatch. Also add missing NO_DEFAULT; to _builtin_say's switch for style consistency with all other handlers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20Add README.md and array slicing supportPaul Buetow
2026-02-19Implement function named arguments, ret, and multiple return valuesPaul Buetow
- 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>
2021-11-02move to codebergPaul Buetow
2021-04-29update URIsPaul Buetow
2008-12-15modified headersPaul Buetow
2008-12-15say foo[1] worksPaul Buetow
2008-11-09changed the src headers.Paul Buetow
2008-11-09make headers stylePaul Buetow
2008-11-09splitted function.c into function.c and functions.cPaul Buetow