Files
akbasic/include/akbasic/variable.h
Tachikoma 8b9c788ad7 Stop a scalar created inside a scope costing value-pool slots
A scalar now lives in the variable record (`akbasic_Variable::inlinevalue`)
rather than drawing from the value pool, so a `GOSUB` local, a `FOR` counter and
a `DEF` parameter cost nothing at all.

The pool is a bump allocator with no free, and its comment justified that with
"nothing in BASIC destroys a variable". Scope exit does: it marks the variable
slot unused, `new_variable()` memsets the slot it hands back -- clearing
`values` -- and `variable_init()` therefore took *fresh* slots for a variable
whose old ones were still counted. Every scope that created a local leaked, with
no diagnostic until the pool ran dry on whichever line happened to be unlucky.

Six thousand `GOSUB`s creating one local used to die on the 4091st at `LOC# = 1`
with "Array of 1 elements does not fit in the 0 remaining value slots". They now
run. A `DEF` called eight thousand times used to die between the four and five
thousandth -- the leaking slot was the call scope's parameter, which is a scalar
-- and both forms now run. A game creating one name per tick was dead in half a
minute; the Breakout in examples/ was, after twenty-five seconds.

**A `@` name is the one exclusion, and it is the whole of it.** A structure or a
pointer to one keeps pool storage, because a pointer into a record outlives the
scope that DIMmed it -- docs/16-structures.md says nothing is reclaimed and
`prev_environment()` relies on it. The name suffix is the right test rather than
`structtype`, which the DIM path sets *after* calling `variable_init()`. A local
array therefore still leaks, deliberately, and is now the narrow rule the
tutorial teaches.

`SWAP` needed the other half: it copies whole variable records, so the `values`
pointer that came over named the other variable's inline slot -- which by then
held this variable's own old value -- and SWAP silently did nothing. Caught by
tests/language/housekeeping/verbs.bas, which is the golden corpus earning its
keep.

tests/value_pool.c is the new coverage. It asserts the mechanism as well as the
consequence: a later change that moved arrays inline too would pass every
behavioural case and quietly break the pointer guarantee. The sharpest case
takes the pool's whole 4096 slots in four arrays after two hundred scope
entries, so one leaked slot has nowhere to go.

Chapter 17 Step 3 taught "declare every name at the top" and no longer needs to.
It now teaches what is still true -- a name first seen inside a subroutine dies
at RETURN, so a routine cannot answer its caller through one -- and its
demonstration is the array case, which still fails.

TODO.md section 6 item 30 and section 9 item 1, both struck.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:47:49 -04:00

131 lines
6.2 KiB
C

/**
* @file variable.h
* @brief Declares a named, strongly-typed, optionally multi-dimensional slot.
*
* Ported from basicvariable.go. Type comes from the identifier's last character:
* `$` string, `#` integer, `%` float. Note that `%` meaning *float* inverts the
* Commodore convention where `%` is integer; the reference does it that way and
* its README documents it, so it is kept.
*/
#ifndef _AKBASIC_VARIABLE_H_
#define _AKBASIC_VARIABLE_H_
#include <akerror.h>
#include <akbasic/types.h>
#include <akbasic/value.h>
typedef struct
{
char name[AKBASIC_MAX_STRING_LENGTH];
akbasic_Type valuetype;
akbasic_Value *values; /** The pool, or `inlinevalue` for a scalar */
int valuecount;
/*
* A scalar's storage, so that creating one costs the value pool nothing.
*
* The pool is a bump allocator with no free, and a scope exit hands the
* variable *slot* back while its storage stays counted against the pool --
* so a name first created inside a GOSUB used to spend slots on every call
* and 4096 creations ended the run. Keeping the one value here instead
* means a local, a FOR counter and a DEF parameter are all free.
*
* akbasic_variable_init() decides which of the two `values` points at, and
* a `@` name is the one exclusion: see the comment there.
*/
akbasic_Value inlinevalue;
int64_t dimensions[AKBASIC_MAX_ARRAY_DEPTH];
int dimensioncount;
bool mutable_;
bool used; /** Pool bookkeeping */
/*
* What DIM ... AS recorded. A structure variable's `values` run is one
* instance laid out by the type's offsets, exactly as an array's run is its
* elements -- so these two are what tell the difference between the two
* kinds of run.
*/
int structtype; /** type index, -1 when not a structure */
bool ispointer; /** declared PTR TO rather than a value */
void *hostbase; /** the host's struct, for a binding */
} akbasic_Variable;
/**
* @brief Give a variable storage and a type.
* @param obj Object to initialize, inspect, or modify.
* @param pool Backing store the elements are drawn from.
* @param sizes Dimension sizes; every one must be positive.
* @param sizecount Number of dimensions.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `obj` or `pool` is NULL.
* @throws AKBASIC_ERR_VALUE When the name is empty or a dimension is not positive.
* @throws AKBASIC_ERR_BOUNDS When the array is larger than the pool can serve.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_init(akbasic_Variable *obj, akbasic_ValuePool *pool, int64_t *sizes, int sizecount);
/**
* @brief Mark the variable undefined and mutable without touching its storage.
* @param obj Object to initialize, inspect, or modify.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `obj` is NULL.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_zero(akbasic_Variable *obj);
/**
* @brief Resolve a subscript list to the value it addresses.
* @param obj Object to initialize, inspect, or modify.
* @param subscripts Index per dimension.
* @param subscriptcount Number of indices supplied; must equal the dimension count.
* @param dest Output destination populated by the function.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKBASIC_ERR_BOUNDS When the count is wrong or an index is out of range.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_get_subscript(akbasic_Variable *obj, int64_t *subscripts, int subscriptcount, akbasic_Value **dest);
/**
* @brief Copy a value into the slot a subscript list addresses.
* @param obj Object to initialize, inspect, or modify.
* @param value Source value; cloned into the slot, not aliased.
* @param subscripts Index per dimension.
* @param subscriptcount Number of indices supplied; must equal the dimension count.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `value` is NULL.
* @throws AKBASIC_ERR_BOUNDS When the count is wrong or an index is out of range.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_set_subscript(akbasic_Variable *obj, akbasic_Value *value, int64_t *subscripts, int subscriptcount);
/**
* @brief Store an integer into the slot a subscript list addresses.
* @param obj Object to initialize, inspect, or modify.
* @param value Value to store.
* @param subscripts Index per dimension.
* @param subscriptcount Number of indices supplied; must equal the dimension count.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKBASIC_ERR_BOUNDS When the count is wrong or an index is out of range.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_set_integer(akbasic_Variable *obj, int64_t value, int64_t *subscripts, int subscriptcount);
/**
* @brief Store a float into the slot a subscript list addresses.
* @param obj Object to initialize, inspect, or modify.
* @param value Value to store.
* @param subscripts Index per dimension.
* @param subscriptcount Number of indices supplied; must equal the dimension count.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKBASIC_ERR_BOUNDS When the count is wrong or an index is out of range.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_set_float(akbasic_Variable *obj, double value, int64_t *subscripts, int subscriptcount);
/**
* @brief Store a string into the slot a subscript list addresses.
* @param obj Object to initialize, inspect, or modify.
* @param value Value to store.
* @param subscripts Index per dimension.
* @param subscriptcount Number of indices supplied; must equal the dimension count.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `value` is NULL.
* @throws AKBASIC_ERR_VALUE When the string exceeds the length limit.
* @throws AKBASIC_ERR_BOUNDS When the count is wrong or an index is out of range.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_variable_set_string(akbasic_Variable *obj, const char *value, int64_t *subscripts, int subscriptcount);
#endif // _AKBASIC_VARIABLE_H_