Port onto libakstdlib 2b79aca and convert the eight bool predicates
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2% bypassed, against 86.4% on the same tree before this. The submodule bump 669b2b3 -> 2b79aca needed no source change of its own: the release is drop-in for what akbasic already used. Seven of the eight sites the earlier port left on raw libc change their own signature rather than swallowing an error, per andrew's ruling on libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end, peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's scroll/newline/putchar_at/echo_line/edit_key chain all return an akerr_ErrorContext * and hand the answer back through an out parameter. is_waiting_for and is_waiting_for_any are a public header change; every call site that used one as a term in a condition hoists it into a statement first. verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the comparator's signature, so there is no out parameter to report through -- which is what libakstdlib#38 concluded. It carries a comment saying so and saying why the bypass is safe there. Six snprintf sites stay raw because they want truncation as an answer rather than an error, and aksl_snprintf cannot express that until libakstdlib#34 hands the required length back. Each of the six says so at the site. Two of them, in host.c, are a latent defect rather than a decision: a host type name over 31 characters truncates silently and two sharing a prefix then collide, where structtype.c refuses the same case. DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the PASS in it returned past CLEANUP, so a scan error left the file open. Hoisting the loop into its own helper to convert fgets fixes it. Refs libakstdlib#26, libakstdlib#38 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
201
src/scanner.c
201
src/scanner.c
@@ -26,27 +26,67 @@ akerr_ErrorContext *akbasic_scanner_zero(akbasic_Runtime *obj)
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static bool is_at_end(akbasic_Runtime *obj)
|
||||
/**
|
||||
* @brief Is the cursor past the end of the line?
|
||||
*
|
||||
* The answer leaves through @p dest rather than the return value because
|
||||
* measuring the line can fail, and a `bool` has nowhere to put that. Same shape
|
||||
* as symtab.c's `probe`, and for the same reason. See libakstdlib #38.
|
||||
*/
|
||||
static akerr_ErrorContext *is_at_end(akbasic_Runtime *obj, bool *dest)
|
||||
{
|
||||
return (obj->current >= (int)strlen(obj->line));
|
||||
PREPARE_ERROR(errctx);
|
||||
size_t linelen = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in is_at_end");
|
||||
PASS(errctx, aksl_strlen(obj->line, &linelen));
|
||||
*dest = (obj->current >= (int)linelen);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static bool peek(akbasic_Runtime *obj, char *dest)
|
||||
/**
|
||||
* @brief The character under the cursor.
|
||||
* @param[out] dest The character. Untouched when there is none.
|
||||
* @param[out] got Whether there was one. The old `bool` return.
|
||||
*/
|
||||
static akerr_ErrorContext *peek(akbasic_Runtime *obj, char *dest, bool *got)
|
||||
{
|
||||
if ( is_at_end(obj) ) {
|
||||
return false;
|
||||
PREPARE_ERROR(errctx);
|
||||
bool atend = false;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL && got != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in peek");
|
||||
PASS(errctx, is_at_end(obj, &atend));
|
||||
if ( atend ) {
|
||||
*got = false;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
*dest = obj->line[obj->current];
|
||||
return true;
|
||||
*got = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static bool peek_next(akbasic_Runtime *obj, char *dest)
|
||||
/**
|
||||
* @brief The character one past the cursor.
|
||||
* @param[out] dest The character. Untouched when there is none.
|
||||
* @param[out] got Whether there was one. The old `bool` return.
|
||||
*/
|
||||
static akerr_ErrorContext *peek_next(akbasic_Runtime *obj, char *dest, bool *got)
|
||||
{
|
||||
if ( (obj->current + 1) >= (int)strlen(obj->line) ) {
|
||||
return false;
|
||||
PREPARE_ERROR(errctx);
|
||||
size_t linelen = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL && got != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in peek_next");
|
||||
PASS(errctx, aksl_strlen(obj->line, &linelen));
|
||||
if ( (obj->current + 1) >= (int)linelen ) {
|
||||
*got = false;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
*dest = obj->line[obj->current + 1];
|
||||
return true;
|
||||
*got = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -58,9 +98,12 @@ static bool peek_next(akbasic_Runtime *obj, char *dest)
|
||||
static akerr_ErrorContext *get_lexeme(akbasic_Runtime *obj, char *dest, size_t len)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int linelen = (int)strlen(obj->line);
|
||||
size_t measured = 0;
|
||||
int linelen = 0;
|
||||
int span = 0;
|
||||
|
||||
PASS(errctx, aksl_strlen(obj->line, &measured));
|
||||
linelen = (int)measured;
|
||||
if ( obj->current == linelen ) {
|
||||
span = linelen - obj->start;
|
||||
} else if ( obj->start == obj->current ) {
|
||||
@@ -74,7 +117,7 @@ static akerr_ErrorContext *get_lexeme(akbasic_Runtime *obj, char *dest, size_t l
|
||||
}
|
||||
FAIL_ZERO_RETURN(errctx, (span >= 0 && (size_t)span < len), AKBASIC_ERR_BOUNDS,
|
||||
"Lexeme of %d characters exceeds the %zu character limit", span, len - 1);
|
||||
memcpy(dest, obj->line + obj->start, (size_t)span);
|
||||
PASS(errctx, aksl_memcpy(dest, obj->line + obj->start, (size_t)span));
|
||||
dest[span] = '\0';
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -83,26 +126,39 @@ static akerr_ErrorContext *add_token(akbasic_Runtime *obj, akbasic_TokenType tok
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Environment *env = obj->environment;
|
||||
size_t lexemelen = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (env->nexttoken < AKBASIC_MAX_TOKENS), AKBASIC_ERR_BOUNDS,
|
||||
"Line %" PRId64 " has more than %d tokens",
|
||||
env->lineno, AKBASIC_MAX_TOKENS);
|
||||
FAIL_ZERO_RETURN(errctx, (strlen(lexeme) < AKBASIC_MAX_LINE_LENGTH), AKBASIC_ERR_BOUNDS,
|
||||
PASS(errctx, aksl_strlen(lexeme, &lexemelen));
|
||||
FAIL_ZERO_RETURN(errctx, (lexemelen < AKBASIC_MAX_LINE_LENGTH), AKBASIC_ERR_BOUNDS,
|
||||
"Token lexeme exceeds the %d character limit", AKBASIC_MAX_LINE_LENGTH - 1);
|
||||
env->tokens[env->nexttoken].tokentype = token;
|
||||
env->tokens[env->nexttoken].lineno = env->lineno;
|
||||
strncpy(env->tokens[env->nexttoken].lexeme, lexeme, AKBASIC_MAX_LINE_LENGTH - 1);
|
||||
env->tokens[env->nexttoken].lexeme[AKBASIC_MAX_LINE_LENGTH - 1] = '\0';
|
||||
PASS(errctx, aksl_strcpy(env->tokens[env->nexttoken].lexeme,
|
||||
sizeof(env->tokens[env->nexttoken].lexeme), lexeme));
|
||||
env->nexttoken += 1;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/* Consume one more character when it matches, choosing between two token types. */
|
||||
static bool match_next_char(akbasic_Runtime *obj, char cm, akbasic_TokenType truetype, akbasic_TokenType falsetype)
|
||||
/**
|
||||
* @brief Consume one more character when it matches, choosing between two token types.
|
||||
* @param[out] matched Whether the character was consumed. The old `bool` return.
|
||||
*
|
||||
* On the chain below `peek`, so it reports the same way. See libakstdlib #38.
|
||||
*/
|
||||
static akerr_ErrorContext *match_next_char(akbasic_Runtime *obj, char cm, akbasic_TokenType truetype,
|
||||
akbasic_TokenType falsetype, bool *matched)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
char nc = '\0';
|
||||
bool got = false;
|
||||
|
||||
if ( !peek(obj, &nc) ) {
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && matched != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in match_next_char");
|
||||
PASS(errctx, peek(obj, &nc, &got));
|
||||
if ( !got ) {
|
||||
/*
|
||||
* Nothing left to peek at, so the operator is whatever it is on its
|
||||
* own. The reference returns here *without* setting a type
|
||||
@@ -112,24 +168,34 @@ static bool match_next_char(akbasic_Runtime *obj, char cm, akbasic_TokenType tru
|
||||
* TODO.md section 6 item 14.
|
||||
*/
|
||||
obj->tokentype = falsetype;
|
||||
return false;
|
||||
*matched = false;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( nc == cm ) {
|
||||
obj->current += 1;
|
||||
obj->tokentype = truetype;
|
||||
return true;
|
||||
*matched = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
obj->tokentype = falsetype;
|
||||
return false;
|
||||
*matched = false;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *match_string(akbasic_Runtime *obj)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
char c = '\0';
|
||||
bool atend = false;
|
||||
bool got = false;
|
||||
|
||||
while ( !is_at_end(obj) ) {
|
||||
if ( !peek(obj, &c) ) {
|
||||
for ( ;; ) {
|
||||
PASS(errctx, is_at_end(obj, &atend));
|
||||
if ( atend ) {
|
||||
break;
|
||||
}
|
||||
PASS(errctx, peek(obj, &c, &got));
|
||||
if ( !got ) {
|
||||
PASS(errctx, akbasic_runtime_error(obj, AKBASIC_ERRCLASS_PARSE,
|
||||
"UNTERMINATED STRING LITERAL\n"));
|
||||
obj->hasError = true;
|
||||
@@ -154,12 +220,19 @@ static akerr_ErrorContext *match_number(akbasic_Runtime *obj)
|
||||
int64_t lineno = 0;
|
||||
long long converted = 0;
|
||||
bool hex = false;
|
||||
bool atend = false;
|
||||
bool got = false;
|
||||
|
||||
obj->tokentype = AKBASIC_TOK_LITERAL_INT;
|
||||
while ( !is_at_end(obj) ) {
|
||||
(void)peek(obj, &c);
|
||||
for ( ;; ) {
|
||||
PASS(errctx, is_at_end(obj, &atend));
|
||||
if ( atend ) {
|
||||
break;
|
||||
}
|
||||
PASS(errctx, peek(obj, &c, &got));
|
||||
if ( c == '.' ) {
|
||||
if ( !peek_next(obj, &nc) || !isdigit((unsigned char)nc) ) {
|
||||
PASS(errctx, peek_next(obj, &nc, &got));
|
||||
if ( !got || !isdigit((unsigned char)nc) ) {
|
||||
PASS(errctx, akbasic_runtime_error(obj, AKBASIC_ERRCLASS_PARSE,
|
||||
"INVALID FLOATING POINT LITERAL\n"));
|
||||
obj->hasError = true;
|
||||
@@ -200,13 +273,17 @@ static akerr_ErrorContext *match_number(akbasic_Runtime *obj)
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
char message[AKBASIC_MAX_LINE_LENGTH + 32];
|
||||
snprintf(message, sizeof(message), "INTEGER CONVERSION ON '%s'", lexeme);
|
||||
char message[AKBASIC_MAX_LINE_LENGTH + 32] = "INTEGER CONVERSION";
|
||||
int written = 0;
|
||||
/*
|
||||
* Reporting can itself fail if the sink is broken. Nothing useful
|
||||
* remains to be done about that here, so record the flag and let the
|
||||
* next operation surface it.
|
||||
* Reporting can itself fail if the sink is broken, and so can
|
||||
* formatting the message. Neither leaves anything useful to do here --
|
||||
* this block is already handling an error -- so both are ignored, the
|
||||
* message keeps the initialiser above if the format fails, and the flag
|
||||
* lets the next operation surface it.
|
||||
*/
|
||||
IGNORE(aksl_snprintf(&written, message, sizeof(message),
|
||||
"INTEGER CONVERSION ON '%s'", lexeme));
|
||||
IGNORE(akbasic_runtime_error(obj, AKBASIC_ERRCLASS_PARSE, message));
|
||||
obj->hasError = true;
|
||||
} FINISH(errctx, false);
|
||||
@@ -230,10 +307,16 @@ static akerr_ErrorContext *match_identifier(akbasic_Runtime *obj)
|
||||
bool userfunction = false;
|
||||
char c = '\0';
|
||||
size_t used = 0;
|
||||
bool atend = false;
|
||||
bool got = false;
|
||||
|
||||
obj->tokentype = AKBASIC_TOK_IDENTIFIER;
|
||||
while ( !is_at_end(obj) ) {
|
||||
(void)peek(obj, &c);
|
||||
for ( ;; ) {
|
||||
PASS(errctx, is_at_end(obj, &atend));
|
||||
if ( atend ) {
|
||||
break;
|
||||
}
|
||||
PASS(errctx, peek(obj, &c, &got));
|
||||
if ( isdigit((unsigned char)c) || isalpha((unsigned char)c) ) {
|
||||
obj->current += 1;
|
||||
continue;
|
||||
@@ -270,9 +353,8 @@ static akerr_ErrorContext *match_identifier(akbasic_Runtime *obj)
|
||||
* in variable name" branch below is dead code, and `PRINT$ = 1` is quietly
|
||||
* accepted as an ordinary string variable. TODO.md section 6 item 16.
|
||||
*/
|
||||
strncpy(basename, lexeme, sizeof(basename) - 1);
|
||||
basename[sizeof(basename) - 1] = '\0';
|
||||
used = strlen(basename);
|
||||
PASS(errctx, aksl_strcpy(basename, sizeof(basename), lexeme));
|
||||
PASS(errctx, aksl_strlen(basename, &used));
|
||||
if ( obj->tokentype != AKBASIC_TOK_IDENTIFIER && used > 0 ) {
|
||||
basename[used - 1] = '\0';
|
||||
}
|
||||
@@ -311,15 +393,18 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
char lexeme[AKBASIC_MAX_LINE_LENGTH];
|
||||
char c = '\0';
|
||||
bool done = false;
|
||||
bool atend = false;
|
||||
bool matched = false;
|
||||
size_t linelen = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL runtime in scan");
|
||||
FAIL_ZERO_RETURN(errctx, (line != NULL), AKERR_NULLPOINTER, "NULL line in scan");
|
||||
FAIL_ZERO_RETURN(errctx, (strlen(line) < AKBASIC_MAX_LINE_LENGTH), AKBASIC_ERR_BOUNDS,
|
||||
PASS(errctx, aksl_strlen(line, &linelen));
|
||||
FAIL_ZERO_RETURN(errctx, (linelen < AKBASIC_MAX_LINE_LENGTH), AKBASIC_ERR_BOUNDS,
|
||||
"Source line of %zu characters exceeds the %d character limit",
|
||||
strlen(line), AKBASIC_MAX_LINE_LENGTH - 1);
|
||||
linelen, AKBASIC_MAX_LINE_LENGTH - 1);
|
||||
|
||||
strncpy(obj->line, line, AKBASIC_MAX_LINE_LENGTH - 1);
|
||||
obj->line[AKBASIC_MAX_LINE_LENGTH - 1] = '\0';
|
||||
PASS(errctx, aksl_strcpy(obj->line, sizeof(obj->line), line));
|
||||
PASS(errctx, akbasic_environment_zero_parser(obj->environment));
|
||||
obj->current = 0;
|
||||
obj->start = 0;
|
||||
@@ -333,7 +418,11 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
*/
|
||||
obj->hadlinenumber = false;
|
||||
|
||||
while ( !is_at_end(obj) && !done ) {
|
||||
for ( ;; ) {
|
||||
PASS(errctx, is_at_end(obj, &atend));
|
||||
if ( atend || done ) {
|
||||
break;
|
||||
}
|
||||
c = obj->line[obj->current];
|
||||
obj->current += 1;
|
||||
|
||||
@@ -350,7 +439,7 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
* no spelling this makes ambiguous.
|
||||
*/
|
||||
case '-':
|
||||
(void)match_next_char(obj, '>', AKBASIC_TOK_ARROW, AKBASIC_TOK_MINUS);
|
||||
PASS(errctx, match_next_char(obj, '>', AKBASIC_TOK_ARROW, AKBASIC_TOK_MINUS, &matched));
|
||||
break;
|
||||
case '/': obj->tokentype = AKBASIC_TOK_LEFT_SLASH; break;
|
||||
case '*': obj->tokentype = AKBASIC_TOK_STAR; break;
|
||||
@@ -373,15 +462,19 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
case '[': obj->tokentype = AKBASIC_TOK_LEFT_SQUAREBRACKET; break;
|
||||
case ']': obj->tokentype = AKBASIC_TOK_RIGHT_SQUAREBRACKET; break;
|
||||
case '=':
|
||||
(void)match_next_char(obj, '=', AKBASIC_TOK_EQUAL, AKBASIC_TOK_ASSIGNMENT);
|
||||
PASS(errctx, match_next_char(obj, '=', AKBASIC_TOK_EQUAL, AKBASIC_TOK_ASSIGNMENT, &matched));
|
||||
break;
|
||||
case '<':
|
||||
if ( !match_next_char(obj, '=', AKBASIC_TOK_LESS_THAN_EQUAL, AKBASIC_TOK_LESS_THAN) ) {
|
||||
(void)match_next_char(obj, '>', AKBASIC_TOK_NOT_EQUAL, AKBASIC_TOK_LESS_THAN);
|
||||
PASS(errctx, match_next_char(obj, '=', AKBASIC_TOK_LESS_THAN_EQUAL,
|
||||
AKBASIC_TOK_LESS_THAN, &matched));
|
||||
if ( !matched ) {
|
||||
PASS(errctx, match_next_char(obj, '>', AKBASIC_TOK_NOT_EQUAL,
|
||||
AKBASIC_TOK_LESS_THAN, &matched));
|
||||
}
|
||||
break;
|
||||
case '>':
|
||||
(void)match_next_char(obj, '=', AKBASIC_TOK_GREATER_THAN_EQUAL, AKBASIC_TOK_GREATER_THAN);
|
||||
PASS(errctx, match_next_char(obj, '=', AKBASIC_TOK_GREATER_THAN_EQUAL,
|
||||
AKBASIC_TOK_GREATER_THAN, &matched));
|
||||
break;
|
||||
case '"':
|
||||
obj->start = obj->current;
|
||||
@@ -402,7 +495,10 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
PASS(errctx, match_identifier(obj));
|
||||
} else {
|
||||
char message[AKBASIC_MAX_LINE_LENGTH];
|
||||
snprintf(message, sizeof(message), "UNKNOWN TOKEN %c\n", c);
|
||||
int written = 0;
|
||||
|
||||
PASS(errctx, aksl_snprintf(&written, message, sizeof(message),
|
||||
"UNKNOWN TOKEN %c\n", c));
|
||||
PASS(errctx, akbasic_runtime_error(obj, AKBASIC_ERRCLASS_PARSE, message));
|
||||
obj->hasError = true;
|
||||
obj->start = obj->current;
|
||||
@@ -426,10 +522,13 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
* stores *that* as the program text.
|
||||
*/
|
||||
int skip = obj->current;
|
||||
size_t tail = 0;
|
||||
|
||||
while ( obj->line[skip] == ' ' ) {
|
||||
skip += 1;
|
||||
}
|
||||
memmove(obj->line, obj->line + skip, strlen(obj->line + skip) + 1);
|
||||
PASS(errctx, aksl_strlen(obj->line + skip, &tail));
|
||||
PASS(errctx, aksl_memmove(obj->line, obj->line + skip, tail + 1));
|
||||
obj->current = 0;
|
||||
} else {
|
||||
PASS(errctx, get_lexeme(obj, lexeme, sizeof(lexeme)));
|
||||
@@ -445,10 +544,10 @@ akerr_ErrorContext *akbasic_scanner_scan(akbasic_Runtime *obj, const char *line,
|
||||
}
|
||||
|
||||
if ( dest != NULL ) {
|
||||
FAIL_ZERO_RETURN(errctx, (strlen(obj->line) < len), AKBASIC_ERR_BOUNDS,
|
||||
PASS(errctx, aksl_strlen(obj->line, &linelen));
|
||||
FAIL_ZERO_RETURN(errctx, (linelen < len), AKBASIC_ERR_BOUNDS,
|
||||
"Scanned line does not fit the caller's buffer");
|
||||
strncpy(dest, obj->line, len - 1);
|
||||
dest[len - 1] = '\0';
|
||||
PASS(errctx, aksl_strcpy(dest, len, obj->line));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user