diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-28 16:04:22 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-28 16:04:22 +0200 |
| commit | 946fac7d813f6283c18087a46b9e2ee084852c64 (patch) | |
| tree | e00bc09a5205a6699cd40cbb48e9f47e9bcdc9dc | |
| parent | 075bc33d15b4d11fa03f381c3e03437d9f759c22 (diff) | |
Decouple functions.c from interpreter array-LHS private state [Coupling]
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>
| -rw-r--r-- | docs/stats.txt | 2 | ||||
| -rwxr-xr-x | fype | bin | 288064 -> 288168 bytes | |||
| -rw-r--r-- | src/build.h | 2 | ||||
| -rw-r--r-- | src/core/functions.c | 38 | ||||
| -rw-r--r-- | src/core/functions.h | 4 | ||||
| -rw-r--r-- | src/core/interpret.c | 12 | ||||
| -rw-r--r-- | tags | 2 |
7 files changed, 38 insertions, 22 deletions
diff --git a/docs/stats.txt b/docs/stats.txt index fdb7486..bf4e4cc 100644 --- a/docs/stats.txt +++ b/docs/stats.txt @@ -1,6 +1,6 @@ make[1]: Entering directory '/home/paul/git/fype' ===> Num of C source files : 46 -===> Num of C source lines : 8686 +===> Num of C source lines : 8702 ===> Num of Fype source examples : 19 ===> Num of Fype source lines : 883 make[1]: Leaving directory '/home/paul/git/fype' Binary files differdiff --git a/src/build.h b/src/build.h index ab16e44..fce9d6f 100644 --- a/src/build.h +++ b/src/build.h @@ -36,7 +36,7 @@ #ifndef BUILD_H #define BUILD_H -#define BUILDNR 9696 +#define BUILDNR 9698 #define OS_LINUX #endif diff --git a/src/core/functions.c b/src/core/functions.c index d4a2b51..4d1cc4d 100644 --- a/src/core/functions.c +++ b/src/core/functions.c @@ -486,14 +486,16 @@ _resolve_composite_op(TokenType tt_op, TokenType tt_op2) { /* Perform variable or array-element assignment. * If p_interpret->p_token_array_lhs is set, writes into that array slot; * otherwise writes into the named variable in p_interpret->p_token_temp. */ +/* Perform variable or array-element assignment. + * p_array_lhs and i_lhs_idx are passed explicitly so this function does + * not need to reach into the Interpret struct for array-LHS state. */ static void -_op_assign(Interpret *p_interpret, Token *p_token_store) { +_op_assign(Interpret *p_interpret, Token *p_token_store, + Token *p_array_lhs, int i_lhs_idx) { + /* Array element assignment: arr[i] = val */ - if (p_interpret->p_token_array_lhs != NULL) { - array_set(p_interpret->p_token_array_lhs->p_array, - p_interpret->i_array_lhs_index, - p_token_store); - p_interpret->p_token_array_lhs = NULL; + if (p_array_lhs != NULL) { + array_set(p_array_lhs->p_array, i_lhs_idx, p_token_store); return; } @@ -924,9 +926,18 @@ _op_rshift(Token *p_store, Token *p_next, TokenType tt_highest) { * 2. Handle assignment early — it does not need type conversion. * 3. Coerce both operands to their highest shared type. * 4. Call the matching _op_*() function for the resolved operator. */ +/* Dispatch to the correct per-operator handler. + * p_array_lhs / i_lhs_idx carry array-element assignment context when + * the LHS is arr[i]; both are NULL/0 for plain variable assignment or + * any non-assignment operator. + * 1. Resolve composite two-token operators (!=, ==, <=, >=, <<, >>). + * 2. Handle assignment early — it does not need type conversion. + * 3. Coerce both operands to their highest shared type. + * 4. Call the matching _op_*() function for the resolved operator. */ void _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, - Token *p_token_op2, Token *p_token_next) { + Token *p_token_op2, Token *p_token_next, + Token *p_array_lhs, int i_lhs_idx) { TokenType tt_op = token_get_tt(p_token_op); TokenType tt_op2 = p_token_op2 == NULL @@ -953,7 +964,7 @@ _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, if (p_token_op2 != NULL) tt_op = _resolve_composite_op(tt_op, tt_op2); else if (tt_op == TT_ASSIGN) { - _op_assign(p_interpret, p_token_store); + _op_assign(p_interpret, p_token_store, p_array_lhs, i_lhs_idx); return; } @@ -994,17 +1005,22 @@ _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, token_delete(p_token_next); } +/* Drive p_stack_args through the given operator, consuming i_args operands. + * p_array_lhs / i_lhs_idx are forwarded to _process() so that array-element + * assignment (arr[i] = val) can be dispatched without touching Interpret + * internals. Pass NULL / 0 for normal (non-array-LHS) expressions. */ void function_process(Interpret *p_interpret, Token *p_token_op, - Token *p_token_op2, Stack *p_stack_args, int i_args) { + Token *p_token_op2, Stack *p_stack_args, int i_args, + Token *p_array_lhs, int i_lhs_idx) { Token *p_token_store = token_new_copy(stack_pop(p_stack_args)); - for (int i = 0; i < i_args -1 && !stack_empty(p_stack_args); ++i) { + for (int i = 0; i < i_args - 1 && !stack_empty(p_stack_args); ++i) { Token *p_token_next = stack_pop(p_stack_args); _process(p_interpret, p_token_store, p_token_op, - p_token_op2, p_token_next); + p_token_op2, p_token_next, p_array_lhs, i_lhs_idx); } stack_push(p_stack_args, p_token_store); diff --git a/src/core/functions.h b/src/core/functions.h index e20355b..ac45d5e 100644 --- a/src/core/functions.h +++ b/src/core/functions.h @@ -50,9 +50,11 @@ Functions* functions_new(); void functions_delete(Functions *p_functions); void functions_init(Functions *p_functions); +/* p_array_lhs / i_lhs_idx carry array-element LHS context for arr[i] = val; + * pass NULL / 0 for plain assignment or non-assignment operators. */ void function_process(Interpret *p_interp, Token *p_token_op, Token *p_token_op2, Stack *p_stack_args, - int i_args); + int i_args, Token *p_array_lhs, int i_lhs_idx); _Bool function_is_buildin(Token *p_token_ident); void function_process_buildin(Interpret *p_interpret, Token *p_token_ident, diff --git a/src/core/interpret.c b/src/core/interpret.c index b5ec443..085f716 100644 --- a/src/core/interpret.c +++ b/src/core/interpret.c @@ -818,7 +818,7 @@ _compare(Interpret *p_interpret) { _INTERPRET_ERROR("Expected sum", p_interpret->p_token); function_process(p_interpret, p_token_op, p_token_op2, - p_interpret->p_stack, 2); + p_interpret->p_stack, 2, NULL, 0); } break; /* case */ @@ -875,7 +875,7 @@ _sum(Interpret *p_interpret) { _INTERPRET_ERROR("Expected product", p_token_op); function_process(p_interpret, p_token_op, p_token_op2, - p_interpret->p_stack, 2); + p_interpret->p_stack, 2, NULL, 0); } break; /* case */ @@ -913,7 +913,7 @@ _product(Interpret *p_interpret) { _INTERPRET_ERROR("Expected product2", p_token); function_process(p_interpret, p_token, NULL, - p_interpret->p_stack, 2); + p_interpret->p_stack, 2, NULL, 0); } break; /* case */ @@ -955,12 +955,10 @@ _product2(Interpret *p_interpret) { if (!_expression_(p_interpret)) _INTERPRET_ERROR("Expected expression", p_token); - /* Restore array LHS so function_process can assign it */ - p_interpret->p_token_array_lhs = p_lhs; - p_interpret->i_array_lhs_index = i_lhs_idx; + /* Pass array LHS explicitly; no longer stored in p_interpret */ p_interpret->p_token_temp = p_token_temp; function_process(p_interpret, p_token, NULL, - p_interpret->p_stack, 2); + p_interpret->p_stack, 2, p_lhs, i_lhs_idx); p_interpret->p_token_temp = NULL; } else { @@ -102,7 +102,7 @@ _next ./src/core/interpret.c /^_next(Interpret *p_interpret) {$/;" f typeref:typ _next_tt ./src/core/interpret.c /^_next_tt(Interpret *p_interpret) {$/;" f typeref:typename:TokenType _op_add ./src/core/functions.c /^_op_add(Token *p_store, Token *p_next, TokenType tt_highest) {$/;" f typeref:typename:void file: _op_and ./src/core/functions.c /^_op_and(Token *p_store, Token *p_next, TokenType tt_highest) {$/;" f typeref:typename:void file: -_op_assign ./src/core/functions.c /^_op_assign(Interpret *p_interpret, Token *p_token_store) {$/;" f typeref:typename:void file: +_op_assign ./src/core/functions.c /^_op_assign(Interpret *p_interpret, Token *p_token_store,$/;" f typeref:typename:void file: _op_div ./src/core/functions.c /^_op_div(Token *p_store, Token *p_next, TokenType tt_highest) {$/;" f typeref:typename:void file: _op_eq ./src/core/functions.c /^_op_eq(Token *p_store, Token *p_next, TokenType tt_highest) {$/;" f typeref:typename:void file: _op_ge ./src/core/functions.c /^_op_ge(Token *p_store, Token *p_next, TokenType tt_highest) {$/;" f typeref:typename:void file: |
