Files
akbasic/tests/scanner_tokens.c
Andrew Kesterson f8c15c2f2c 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>
2026-07-30 23:53:56 -04:00

159 lines
6.7 KiB
C

/**
* @file scanner_tokens.c
* @brief Tests the line tokenizer.
*/
#include "harness.h"
static akbasic_Environment *env(void)
{
return HARNESS_RUNTIME.environment;
}
/* Scan a line and assert the token type at a given index. */
static void expect_token(int index, akbasic_TokenType want, const char *lexeme)
{
TEST_REQUIRE(index < env()->nexttoken,
"expected a token at index %d, only %d were produced",
index, env()->nexttoken);
if ( index >= env()->nexttoken ) {
return;
}
TEST_REQUIRE_INT(env()->tokens[index].tokentype, want);
if ( lexeme != NULL ) {
TEST_REQUIRE_STR(env()->tokens[index].lexeme, lexeme);
}
}
int main(void)
{
char rewritten[AKBASIC_MAX_LINE_LENGTH];
TEST_REQUIRE_OK(harness_start(NULL));
/* Single-character operators. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "+-*/^(),", NULL, 0));
TEST_REQUIRE_INT(env()->nexttoken, 8);
expect_token(0, AKBASIC_TOK_PLUS, "+");
expect_token(1, AKBASIC_TOK_MINUS, "-");
expect_token(2, AKBASIC_TOK_STAR, "*");
expect_token(3, AKBASIC_TOK_LEFT_SLASH, "/");
expect_token(4, AKBASIC_TOK_CARAT, "^");
expect_token(5, AKBASIC_TOK_LEFT_PAREN, "(");
expect_token(6, AKBASIC_TOK_RIGHT_PAREN, ")");
expect_token(7, AKBASIC_TOK_COMMA, ",");
/*
* Two-character comparisons, and the one-character fallbacks. The trailing
* space is not decoration: matchNextChar cannot peek past the end of the
* line, and when it fails it returns without setting a token type, so a
* comparison operator in the final column is silently dropped. TODO.md
* section 12 item 14.
*/
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "<= <> >= < > == = ", NULL, 0));
TEST_REQUIRE_INT(env()->nexttoken, 7);
expect_token(0, AKBASIC_TOK_LESS_THAN_EQUAL, NULL);
expect_token(1, AKBASIC_TOK_NOT_EQUAL, NULL);
expect_token(2, AKBASIC_TOK_GREATER_THAN_EQUAL, NULL);
expect_token(3, AKBASIC_TOK_LESS_THAN, NULL);
expect_token(4, AKBASIC_TOK_GREATER_THAN, NULL);
expect_token(5, AKBASIC_TOK_EQUAL, NULL);
expect_token(6, AKBASIC_TOK_ASSIGNMENT, NULL);
/* Without the trailing space the final operator vanishes. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "A# =", NULL, 0));
TEST_REQUIRE_INT(env()->nexttoken, 1);
/* Literals. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT 1.5", NULL, 0));
expect_token(0, AKBASIC_TOK_COMMAND, "PRINT");
expect_token(1, AKBASIC_TOK_LITERAL_FLOAT, "1.5");
/*
* A hex literal does not survive the scanner: matchNumber allows 'x' through
* but not the hex digits after it, so "0xff" lexes as "0x" and the "ff" is
* scanned as an identifier. The parser's base-16 branch is therefore
* unreachable. TODO.md section 12 item 15.
*/
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT 0xff", NULL, 0));
expect_token(1, AKBASIC_TOK_LITERAL_INT, "0x");
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT \"HELLO\"", NULL, 0));
expect_token(1, AKBASIC_TOK_LITERAL_STRING, "HELLO");
/* An empty string literal really is empty, not a stray quote. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT \"\"", NULL, 0));
expect_token(1, AKBASIC_TOK_LITERAL_STRING, "");
/* Identifier suffixes pick the token type. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "A# B% C$ D", NULL, 0));
expect_token(0, AKBASIC_TOK_IDENTIFIER_INT, "A#");
expect_token(1, AKBASIC_TOK_IDENTIFIER_FLOAT, "B%");
expect_token(2, AKBASIC_TOK_IDENTIFIER_STRING, "C$");
expect_token(3, AKBASIC_TOK_IDENTIFIER, "D");
/* Verbs are case-insensitive; a variable is not a verb. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "print goto", NULL, 0));
expect_token(0, AKBASIC_TOK_COMMAND, "print");
expect_token(1, AKBASIC_TOK_COMMAND, "goto");
/* Reserved words get their own token types, not COMMAND. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "A# AND B# OR NOT C#", NULL, 0));
expect_token(1, AKBASIC_TOK_AND, NULL);
expect_token(3, AKBASIC_TOK_OR, NULL);
expect_token(4, AKBASIC_TOK_NOT, NULL);
/*
* The line-number rule: an integer in token position 0 is consumed, not
* emitted, and the line is rewritten to what follows it with leading spaces
* stripped. The REPL stores the rewritten line as the program text, so this
* is load-bearing rather than cosmetic.
*/
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "10 PRINT 1", rewritten, sizeof(rewritten)));
TEST_REQUIRE_STR(rewritten, "PRINT 1");
TEST_REQUIRE_INT(env()->lineno, 10);
expect_token(0, AKBASIC_TOK_COMMAND, "PRINT");
expect_token(1, AKBASIC_TOK_LITERAL_INT, "1");
/* An integer that is *not* in position 0 stays a literal. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT 10", NULL, 0));
expect_token(1, AKBASIC_TOK_LITERAL_INT, "10");
/* REM ends the line: nothing after it becomes a token. */
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "REM PRINT \"NOT SEEN\"", NULL, 0));
TEST_REQUIRE_INT(env()->nexttoken, 0);
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "10 REM A COMMENT", NULL, 0));
TEST_REQUIRE_INT(env()->nexttoken, 0);
TEST_REQUIRE_INT(env()->lineno, 10);
/*
* The "Reserved word in variable name" check never fires: the lexeme still
* carries its suffix when the verb table is searched, so "PRINT$" misses and
* becomes an ordinary string variable. Pinned as it stands -- TODO.md
* section 12 item 16.
*/
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT$ = 1", NULL, 0));
TEST_REQUIRE(!HARNESS_RUNTIME.hasError,
"PRINT$ is currently accepted as a variable name; see TODO.md 12.16");
expect_token(0, AKBASIC_TOK_IDENTIFIER_STRING, "PRINT$");
/* An unknown character is diagnosed. */
HARNESS_RUNTIME.hasError = false;
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "PRINT ~", NULL, 0));
TEST_REQUIRE(HARNESS_RUNTIME.hasError, "an unknown token must be diagnosed");
/* A malformed number is diagnosed rather than silently becoming zero. */
HARNESS_RUNTIME.hasError = false;
TEST_REQUIRE_OK(akbasic_scanner_scan(&HARNESS_RUNTIME, "1x2 PRINT 1", NULL, 0));
TEST_REQUIRE(HARNESS_RUNTIME.hasError,
"a malformed line number must be diagnosed, not converted to 0");
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "INTEGER CONVERSION ON '1x2'") != NULL,
"expected the reference's conversion message, got: %s", HARNESS_OUTPUT);
TEST_REQUIRE_STATUS(akbasic_scanner_scan(&HARNESS_RUNTIME, NULL, NULL, 0), AKERR_NULLPOINTER);
harness_stop();
return akbasic_test_failures;
}