Nothing in this interpreter mallocs; every pool is a fixed array sized by an AKBASIC_MAX_* constant, so sizeof(akbasic_Runtime) is a compile-time number and most of it was headroom nobody was using. Measured concurrent-use high-water marks off examples/breakout and examples/megademo -- the two most demanding programs this interpreter runs -- against each pool's ceiling: AKBASIC_MAX_ENVIRONMENTS 32 -> 12 (measured peak concurrency: 6-7) AKBASIC_MAX_FUNCTIONS 64 -> 8 (measured: 0, neither program uses DEF FN) AKBASIC_MAX_ARRAY_VALUES 4096 -> 2048 (measured peak: 1618 slots) AKBASIC_MAX_SOURCE_LINES 9999 -> 2048 (measured: ~1270-1496 non-blank lines) AKBASIC_SYMTAB_MAX_SLOTS 256 -> 172 (no caller ever requests more than 128) AKBASIC_SYMTAB_MAX_KEY 64 -> 24 (longest identifier measured: 11 chars) AKBASIC_MAX_LINE_LENGTH 256 -> 80 (Commodore BASIC's own line limit) AKBASIC_MAX_VARIABLES (128) is untouched on purpose: breakout alone reaches 121 of 128 concurrent named variables, so it has the least slack of any pool measured and is not a shrink candidate. akbasic_Variable.name shrinks from AKBASIC_MAX_STRING_LENGTH (256) to AKBASIC_SYMTAB_MAX_KEY: every variable name is registered with akbasic_symtab_set() right after this field is populated (akbasic_environment_create(), src/environment.c), and that call already refuses anything AKBASIC_SYMTAB_MAX_KEY characters or longer. The wider field was headroom nothing could ever put a byte into. Two defects surfaced while testing the line-length drop against the golden corpus, both fixed here because the 80-byte ceiling makes them routine rather than theoretical: - sourcepath (runtime.h) was borrowing AKBASIC_MAX_LINE_LENGTH by accident. It holds a directory, not a line of BASIC, and this checkout's own test paths are 81+ characters deep -- every golden test failed to load until this split into its own AKBASIC_MAX_SOURCE_PATH_LENGTH, backed by PATH_MAX the way libakerror already sizes its own path buffers. - src/sink_stdio.c's stdio_readline() called aksl_fgets() but never checked its own documented contract: a full buffer with no trailing newline means the line was longer than the buffer, and the unread remainder is still in the stream. Unchecked, the next readline() picks that remainder up as its own statement -- a real line silently becomes two wrong ones instead of a clean AKBASIC_ERR_BOUNDS refusal. At 256 bytes this was theoretical; at 80 it is not, so it now refuses loudly. tests/value_pool.c's test_pool_is_untouched_by_scopes() was pinned to the old 4x1024=4096 pool math (four max-size arrays proving nothing leaked); rewritten to 2x1024=2048 for the same proof against the new AKBASIC_MAX_ARRAY_VALUES. Known consequence, tracked in andrew/akbasic#32 rather than worked around here: two files in the protected tests/reference/ corpus (language/functions/mod.bas, language/flowcontrol/nestedforloopwaitingfor command.bas) have 82-character lines and cannot be shortened -- MAINTENANCE.md and CMakeLists.txt:585 are explicit that tests/reference/ is never edited to suit this interpreter. Twelve tests/language/ cases and one docs/18 line are in the same position but are this project's own content. Shipping 80 anyway, with the fallout tracked rather than hidden, was an explicit call on this PR rather than something decided here. Verified: cmake --build build-akgl && ctest --test-dir build-akgl, 97/112 (15 known failures, all AKBASIC_MAX_LINE_LENGTH-related, filed as #32). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
141 lines
6.7 KiB
C
141 lines
6.7 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/symtab.h>
|
|
#include <akbasic/types.h>
|
|
#include <akbasic/value.h>
|
|
|
|
typedef struct
|
|
{
|
|
/*
|
|
* Sized to AKBASIC_SYMTAB_MAX_KEY, not AKBASIC_MAX_STRING_LENGTH: this name
|
|
* only ever gets here by surviving akbasic_symtab_set() first
|
|
* (akbasic_environment_create() calls it right after this field is
|
|
* populated), and that call refuses anything AKBASIC_SYMTAB_MAX_KEY
|
|
* characters or longer with AKBASIC_ERR_BOUNDS. A variable whose name did
|
|
* not fit could never exist, so the wider buffer was 232 bytes of headroom
|
|
* nothing could ever put a byte into.
|
|
*/
|
|
char name[AKBASIC_SYMTAB_MAX_KEY];
|
|
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_
|