Honour a subscript in SSHAPE and GSHAPE
`shape_variable()` took the identifier off the leaf and looked the variable up without ever evaluating the subscript, and both verbs then addressed element zero with a literal. So `SSHAPE SH$(2), ...` wrote the handle into `SH$(0)` and `GSHAPE SH$(2)` stamped whatever was in `SH$(0)`. Ordinary assignment and `PRINT` honour the subscript, which is what made this expensive: a program keeping several saved shapes in an array got every one of them resolving to the same element, silently, and the only symptom was that every stamp came out as the last shape captured. The Breakout in examples/ keeps its six brick stamps in six separate scalars for exactly this reason. `SPRSAV` was the counter-example and is the model -- it evaluates its argument and handles an array element correctly. The subscript resolution itself is now shared: `collect_subscripts()` comes out of src/environment.c as `akbasic_environment_collect_subscripts()`, so a verb taking a variable by name resolves a subscript the same way assignment does rather than each verb deciding for itself. tests/graphics_verbs.c covers TODO.md's reduction -- which used to print "[SHAPE:0] []" and now prints "[] [SHAPE:0]" -- and the case a program actually wants: two shapes captured into two elements, each stamped back through its own, asserted against the device log so a fix that merely made the strings look right would not pass. Chapter 18's trap 4 becomes history, and Chapter 6 says an array works. TODO.md section 9 item 6, struck. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -197,6 +197,25 @@ akerr_ErrorContext AKERR_NOIGNORE *akbasic_environment_stop_waiting(akbasic_Envi
|
||||
*/
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_environment_get(akbasic_Environment *obj, const char *varname, akbasic_Variable **dest);
|
||||
|
||||
/**
|
||||
* @brief Evaluate an lvalue's subscript list, or yield {0} when it has none.
|
||||
*
|
||||
* A bare identifier addresses element zero, because every variable here is
|
||||
* really a one-element array. Shared so that a verb taking a variable by name
|
||||
* -- `SSHAPE` and `GSHAPE` do -- resolves a subscript the same way assignment
|
||||
* does, rather than reading the leaf and quietly using element zero for
|
||||
* everything.
|
||||
*
|
||||
* @param obj The environment the subscript expressions are evaluated in.
|
||||
* @param lval The identifier leaf, whose `.expr` carries any subscript list.
|
||||
* @param subscripts Output: one index per dimension, AKBASIC_MAX_ARRAY_DEPTH wide.
|
||||
* @param count Output: how many were written; at least one.
|
||||
* @return `NULL` on success, otherwise an error context owned by the caller.
|
||||
* @throws AKBASIC_ERR_TYPE When a subscript does not evaluate to an integer.
|
||||
* @throws AKBASIC_ERR_BOUNDS When there are more than AKBASIC_MAX_ARRAY_DEPTH.
|
||||
*/
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_environment_collect_subscripts(akbasic_Environment *obj, akbasic_ASTLeaf *lval, int64_t *subscripts, int *count);
|
||||
|
||||
/**
|
||||
* @brief Find a variable in exactly this scope, creating it here if it is absent.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user