Files
akbasic/include/akbasic/symtab.h
Tachikoma 17af2d406c
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m29s
akbasic CI Build / coverage (push) Failing after 3m40s
akbasic CI Build / sanitizers (push) Failing after 4m37s
akbasic CI Build / mutation_test (push) Failing after 3m35s
akbasic CI Build / akgl_build (push) Failing after 7m20s
Cut akbasic_Runtime's static footprint from 10.75 MiB to 2.40 MiB
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>
2026-08-03 21:44:16 -04:00

96 lines
3.6 KiB
C

/**
* @file symtab.h
* @brief Declares a fixed-capacity open-addressed string-keyed table.
*
* The Go reference used five maps. Three of them -- an environment's variables,
* functions and labels -- become this: one implementation, capacity supplied by
* the caller, keyed by aksl_strhash_djb2 with linear probing. A full table is an
* error, not a resize.
*/
#ifndef _AKBASIC_SYMTAB_H_
#define _AKBASIC_SYMTAB_H_
#include <stdbool.h>
#include <stdint.h>
#include <akerror.h>
#include <akbasic/types.h>
/*
* Slots are sized for the largest table any caller needs, so one slot array type
* serves variables, functions and labels. Capacity is a member; the table is
* kept below a 75% load factor by construction because `capacity` counts slots
* and the caller's logical maximum is smaller.
*
* 172 rather than the old 256: no caller passes akbasic_symtab_init() anything
* larger than AKBASIC_MAX_VARIABLES (128), and 172 keeps that under the 75%
* load factor the comment above promises (128 / 0.75 = 170.7).
*
* AKBASIC_SYMTAB_MAX_KEY 24 rather than 64: the longest identifier across
* examples/breakout and examples/megademo -- variables, DIMmed arrays, labels
* and DEF FN names alike -- is 11 characters (TITLESCREEN, CLEARPOWERS).
*/
#define AKBASIC_SYMTAB_MAX_SLOTS 172
#define AKBASIC_SYMTAB_MAX_KEY 24
typedef struct
{
bool used;
char key[AKBASIC_SYMTAB_MAX_KEY];
void *value;
int64_t ivalue;
} akbasic_SymbolSlot;
typedef struct
{
int capacity;
int count;
akbasic_SymbolSlot slots[AKBASIC_SYMTAB_MAX_SLOTS];
} akbasic_SymbolTable;
/**
* @brief Symbol table initialize.
* @param obj Object to initialize, inspect, or modify.
* @param capacity Number of slots to use; must be positive and no larger than AKBASIC_SYMTAB_MAX_SLOTS.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `obj` is NULL.
* @throws AKBASIC_ERR_BOUNDS When `capacity` is out of range.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_symtab_init(akbasic_SymbolTable *obj, int capacity);
/**
* @brief Store a pointer and an integer against a key, replacing any existing entry.
* @param obj Object to initialize, inspect, or modify.
* @param key Lookup key; copied into the slot.
* @param value Pointer payload, may be NULL.
* @param ivalue Integer payload, used by the label table.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `obj` or `key` is NULL.
* @throws AKBASIC_ERR_BOUNDS When the key is too long or the table is full.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_symtab_set(akbasic_SymbolTable *obj, const char *key, void *value, int64_t ivalue);
/**
* @brief Look a key up.
* @param obj Object to initialize, inspect, or modify.
* @param key Lookup key.
* @param value Output destination for the pointer payload; may be NULL.
* @param ivalue Output destination for the integer payload; may be NULL.
* @return `NULL` on success, otherwise an error context owned by the caller.
* @throws AKERR_NULLPOINTER When `obj` or `key` is NULL.
* @throws AKERR_KEY When the key is not present.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_symtab_get(akbasic_SymbolTable *obj, const char *key, void **value, int64_t *ivalue);
/**
* @brief Drop every entry.
* @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_symtab_clear(akbasic_SymbolTable *obj);
#endif // _AKBASIC_SYMTAB_H_