Port the BASIC interpreter from Go to C
Reproduces deps/basicinterpret in C, in the idiom of the ak* libraries. All 41
.bas files in the reference's corpus produce byte-identical stdout, including
the trailing double newline on an error line -- that comes from basicError
building a string ending in \n and handing it to Println, and
array_outofbounds.txt encodes it.
The corpus is driven in place from the submodule as 41 individual CTest cases
rather than copied, so it cannot drift from upstream. Eighteen unit tests cover
what the corpus cannot reach.
Three structural changes carry most of the work. Go's three reflection lookups
(Command*, Function*, ParseCommand*) become one sorted dispatch table in
src/verbs.c searched with bsearch; adding a verb is a row and two functions. The
five Go maps become one fixed open-addressed table over aksl_strhash_djb2. And
run(), which owned the process until MODE_QUIT, splits into step() plus a
bounded run() -- goal 3 requires a host game to be able to bound execution, and
nothing in the library now terminates the process or touches SDL.
Output goes through an akbasic_TextSink vtable. src/sink_stdio.c is what makes
the corpus runnable with no SDL present; the akgl-backed sink is still to come
and is blocked on libakgl having no text-measurement call.
src/convert.c exists because libakstdlib's aksl_ato* family cannot report a
conversion failure (its TODO.md 2.1.5). The reference checks strconv's error at
four sites and turns it into a BASIC error; routing those through aksl_atoi
would have turned four diagnosable errors into wrong answers, with VAL("garbage")
quietly returning 0. TODO.md 1.9 records which libakstdlib calls are cleared for
use here and which are not.
Reference defects are reproduced, not fixed: the golden files encode the observed
behaviour and a silent correction is a behaviour change. TODO.md section 6 lists
sixteen, and tests/known_reference_defects.c asserts the *correct* contract for
six of them under AKBASIC_KNOWN_FAILING_TESTS, so a fix shows up as
"unexpectedly passed". Five of the sixteen were found by this port and are new:
subtraction stops after one operator so 1-2-3 computes 1-2 and abandons the rest
of the line (a wrong answer, not a refused one); a unary-minus argument inflates
a function's arity so ABS(-9) is rejected; a comparison operator in a line's
final column is dropped; hex literals never survive the scanner; and the
"Reserved word in variable name" check is dead code.
Where the reference reaches undefined behaviour by a route that is defined in Go
-- an out-of-range shift, a negative string multiplier, integer division by zero
-- this raises instead of inheriting the UB. No golden case exercises any of
them.
The top-level CMakeLists shadows add_test, set_tests_properties and
add_custom_target around all three add_subdirectory calls. Without it libakerror's
tests land in our suite as Not Run, and its un-namespaced `coverage` target stops
a coverage build from configuring at all. Test targets are akbasic_test_<name>:
bare test_<name> collides with libakstdlib's, which is what broke libakgl's
configure in c2b16d3.
ctest 59/59; ASan+UBSan 59/59; 92.3% line and 96.9% function coverage; no
warnings under -Wall -Wextra. Branch coverage is not a target, for the reason
libakstdlib and libakgl both record: the akerror macros expand into large branch
trees at every call site.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
This commit is contained in:
162
include/akbasic/runtime.h
Normal file
162
include/akbasic/runtime.h
Normal file
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* @file runtime.h
|
||||
* @brief Declares the interpreter: source, pools, scanner, parser and evaluator.
|
||||
*
|
||||
* Ported from basicruntime.go, minus SDL and minus the for{} loop that owned the
|
||||
* process. The reference's run() does not return until MODE_QUIT; here
|
||||
* akbasic_runtime_step() executes exactly one iteration of that loop and returns,
|
||||
* and akbasic_runtime_run() bounds it. A host game must be able to step or bound
|
||||
* execution rather than surrender control, so the loop belongs to the caller.
|
||||
*
|
||||
* Nothing in this library terminates the process. Errors propagate out as
|
||||
* akerr_ErrorContext * for the host to handle; FINISH_NORETURN belongs only to
|
||||
* the driver's main().
|
||||
*/
|
||||
|
||||
#ifndef _AKBASIC_RUNTIME_H_
|
||||
#define _AKBASIC_RUNTIME_H_
|
||||
|
||||
#include <akerror.h>
|
||||
|
||||
#include <akbasic/environment.h>
|
||||
#include <akbasic/grammar.h>
|
||||
#include <akbasic/sink.h>
|
||||
#include <akbasic/types.h>
|
||||
#include <akbasic/value.h>
|
||||
#include <akbasic/variable.h>
|
||||
|
||||
/** @brief The BASIC-visible error classes, used to build the "? n : CLASS msg" line. */
|
||||
typedef enum
|
||||
{
|
||||
AKBASIC_ERRCLASS_NONE = 0,
|
||||
AKBASIC_ERRCLASS_IO,
|
||||
AKBASIC_ERRCLASS_PARSE,
|
||||
AKBASIC_ERRCLASS_SYNTAX,
|
||||
AKBASIC_ERRCLASS_RUNTIME
|
||||
} akbasic_ErrorClass;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char code[AKBASIC_MAX_LINE_LENGTH];
|
||||
int64_t lineno;
|
||||
} akbasic_SourceLine;
|
||||
|
||||
/** @brief A user-defined subroutine or single-expression function. */
|
||||
typedef struct
|
||||
{
|
||||
char name[AKBASIC_SYMTAB_MAX_KEY];
|
||||
akbasic_ASTLeaf *arglist;
|
||||
akbasic_ASTLeaf *expression;
|
||||
int64_t lineno;
|
||||
akbasic_Environment *environment;
|
||||
/* Deep copies of the arglist and expression need storage that outlives the line. */
|
||||
akbasic_ASTLeaf leafstorage[AKBASIC_MAX_LEAVES * 2];
|
||||
akbasic_LeafPool leafpool;
|
||||
bool used;
|
||||
} akbasic_FunctionDef;
|
||||
|
||||
typedef struct akbasic_Runtime
|
||||
{
|
||||
akbasic_SourceLine source[AKBASIC_MAX_SOURCE_LINES];
|
||||
|
||||
/* Pools. Nothing here is malloc'd; everything is drawn from and returned. */
|
||||
akbasic_Environment environments[AKBASIC_MAX_ENVIRONMENTS];
|
||||
akbasic_Variable variables[AKBASIC_MAX_VARIABLES];
|
||||
akbasic_FunctionDef functions[AKBASIC_MAX_FUNCTIONS];
|
||||
akbasic_ValuePool valuepool;
|
||||
|
||||
akbasic_Value staticTrueValue;
|
||||
akbasic_Value staticFalseValue;
|
||||
|
||||
int mode;
|
||||
int run_finished_mode;
|
||||
akbasic_ErrorClass errclass;
|
||||
int64_t autoLineNumber;
|
||||
|
||||
/*
|
||||
* When false, evaluating an identifier yields the live value rather than a
|
||||
* clone. POKE and POINTER need the address of the real storage. The
|
||||
* reference declares this on both the runtime and the environment and only
|
||||
* ever reads the runtime's; the environment copy is dropped here.
|
||||
*/
|
||||
bool eval_clone_identifiers;
|
||||
|
||||
akbasic_Environment *environment;
|
||||
akbasic_TextSink *sink;
|
||||
|
||||
/* REPL line assembly */
|
||||
char userline[AKBASIC_MAX_LINE_LENGTH];
|
||||
|
||||
/* Scanner state */
|
||||
char line[AKBASIC_MAX_LINE_LENGTH];
|
||||
int current;
|
||||
int start;
|
||||
akbasic_TokenType tokentype;
|
||||
bool hasError;
|
||||
|
||||
bool inputEof;
|
||||
} akbasic_Runtime;
|
||||
|
||||
/**
|
||||
* @brief Bring a runtime up: register status codes, build the root environment.
|
||||
* @param obj Object to initialize, inspect, or modify.
|
||||
* @param sink Where output goes and input comes from; required.
|
||||
*/
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_init(akbasic_Runtime *obj, akbasic_TextSink *sink);
|
||||
|
||||
/** @brief Reset the per-line state without disturbing the program or variables. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_zero(akbasic_Runtime *obj);
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_new_environment(akbasic_Runtime *obj);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_prev_environment(akbasic_Runtime *obj);
|
||||
|
||||
/** @brief Report a BASIC error on the current line, in the reference's format. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_error(akbasic_Runtime *obj, akbasic_ErrorClass errclass, const char *message);
|
||||
|
||||
/** @brief Write text, mirroring the reference's Write(). */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_write(akbasic_Runtime *obj, const char *text);
|
||||
/** @brief Write text and a newline, mirroring the reference's Println(). */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_println(akbasic_Runtime *obj, const char *text);
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_set_mode(akbasic_Runtime *obj, int mode);
|
||||
|
||||
/** @brief Evaluate one AST leaf, drawing scratch values from the environment. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_evaluate(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value **dest);
|
||||
|
||||
/** @brief Evaluate a leaf only when it is a command a REPL may run immediately. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_interpret_immediate(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value **dest);
|
||||
|
||||
/** @brief Evaluate a leaf unless the environment is skipping forward to a verb. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_interpret(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value **dest);
|
||||
|
||||
/** @brief Call a user-defined function or subroutine. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_user_function(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value **dest);
|
||||
|
||||
/**
|
||||
* @brief Execute exactly one iteration of the reference's run() loop.
|
||||
*
|
||||
* One call reads or runs at most one source line. It never blocks beyond a
|
||||
* single readline on the sink, and it always returns.
|
||||
*/
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_step(akbasic_Runtime *obj);
|
||||
|
||||
/**
|
||||
* @brief Step until the runtime quits or `maxsteps` steps have elapsed.
|
||||
* @param maxsteps Step ceiling; zero or negative means unbounded.
|
||||
*/
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_run(akbasic_Runtime *obj, int maxsteps);
|
||||
|
||||
/** @brief Point the runtime at an input stream and choose a starting mode. */
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_start(akbasic_Runtime *obj, int mode);
|
||||
|
||||
/* --- Internal API: exposed for the scanner, parser and verb handlers only. --- */
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_new_variable(akbasic_Runtime *obj, akbasic_Variable **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_new_function(akbasic_Runtime *obj, akbasic_FunctionDef **dest);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_store_line(akbasic_Runtime *obj, int64_t lineno, const char *code);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_process_line_run(akbasic_Runtime *obj);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_process_line_runstream(akbasic_Runtime *obj);
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akbasic_runtime_process_line_repl(akbasic_Runtime *obj);
|
||||
int64_t akbasic_runtime_find_previous_lineno(akbasic_Runtime *obj);
|
||||
|
||||
#endif // _AKBASIC_RUNTIME_H_
|
||||
Reference in New Issue
Block a user