Finish the language: every remaining verb group, and the defects that blocked them
Closes groups A, C, D, E, F, H and J of TODO.md section 4, plus RESTORE and RENUMBER, and closes section 6 -- all seventeen reference defects. Seven of those turned out to have been fixed or never ported and nobody had written it down; the audit records the evidence for each. Two of the seventeen were real. math_plus mutated its left operand when the operand was mutable, so A# + 1 could modify A#; it was gated on FOR/NEXT coverage because NEXT relied on the mutation, so tests/for_next.c came first and NEXT now writes the counter back itself. And the binary operators summed both numeric fields of their right operand, which no BASIC program can reach -- that one needed a test written against the value API. Writing the tests turned up eight defects nobody had listed. Seven are fixed: IF A = 2 THEN was a parse error; only == worked IF ... AND ... was a parse error, because a condition parsed as one relation IF A = 1 OR B = 2 THEN was silently always false, and so was IF A THEN EXIT before any NEXT restarted the program and exhausted the variable pool READ never found a DATA line above it, and swallowed the lines between PRINT 2 + 2 at the prompt was filed as program text instead of answering a short read discarded its bytes, so COPY produced empty files every verb taking an argument list said "peek() returned nil token!" on none The eighth is not fixed and cannot be quietly: a FOR whose step overshoots runs its body one extra time, and FOR I = 1 TO 1 runs it zero times. The two errors cancel for a step of 1, which is why neither was noticed. Correcting them changes the expected output of a checked-in acceptance file, and tests/reference/README.md forbids editing one to suit this interpreter. It is tests/for_semantics.c in AKBASIC_KNOWN_FAILING_TESTS, asserting the correct contract, and TODO.md items 19 and 20. Sprites are real libakgl actors with a renderfunc of their own, because akgl_actor_render draws every sprite square and an actor has no per-axis scale. Both are filed upstream. SPRSAV takes an image file, an SSHAPE handle or a 63-element integer array -- a string here cannot hold a zero byte. Verbs that need hardware that does not exist are refused by name with the reason rather than faked: SYS, HEADER, COLLECT, BACKUP, BOOT, FILTER, and DIRECTORY, which is refused for a missing libakstdlib wrapper filed upstream. 94 tests in the default build, 93 with SDL, 94 under ASan and UBSan, doxygen clean. The Go acceptance corpus stayed green throughout. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -38,6 +38,15 @@ akerr_ErrorContext *akbasic_parse_arglist(akbasic_Parser *parser, akbasic_ASTLea
|
||||
* column to carry one.
|
||||
*/
|
||||
PASS(errctx, akbasic_parser_previous(parser, &operator_));
|
||||
/*
|
||||
* Checked before parsing rather than after. Handing an empty token stream to
|
||||
* the expression parser fails deep inside it with "peek() returned nil
|
||||
* token!", which tells a program author nothing at all -- and this path is
|
||||
* reached by every verb that takes a list, so the bad message was the one
|
||||
* most people saw.
|
||||
*/
|
||||
FAIL_ZERO_RETURN(errctx, (akbasic_parser_peek(parser) != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"%s expected at least one argument", operator_->lexeme);
|
||||
PASS(errctx, akbasic_parser_argument_list(parser, AKBASIC_TOK_FUNCTION_ARGUMENT, false, &arglist));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"%s expected at least one argument", operator_->lexeme);
|
||||
@@ -100,6 +109,413 @@ akerr_ErrorContext *akbasic_parse_draw(akbasic_Parser *parser, akbasic_ASTLeaf *
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_parse_movspr(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *arglist = NULL;
|
||||
akbasic_ASTLeaf *form = NULL;
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_ASTLeaf *tail = NULL;
|
||||
akbasic_Token *operator_ = NULL;
|
||||
akbasic_Token *peeked = NULL;
|
||||
const char *formlexeme = "0";
|
||||
|
||||
/*
|
||||
* MOVSPR is the one verb in the language whose arguments are not simply
|
||||
* comma-separated. It has four forms and the separators are what tell them
|
||||
* apart:
|
||||
*
|
||||
* MOVSPR n, x, y absolute
|
||||
* MOVSPR n, +x, -y relative -- a signed coordinate, not a negative one
|
||||
* MOVSPR n, d ; a polar: distance, then angle
|
||||
* MOVSPR n, a # s continuous: angle, then speed
|
||||
*
|
||||
* So the form has to be decided here, where the tokens still exist, and
|
||||
* carried to the exec handler somehow. It is carried as a synthetic integer
|
||||
* literal prepended to the argument list, which flattens the whole statement
|
||||
* to `form, n, a, b` -- the same trick akbasic_parse_draw uses to make a
|
||||
* polyline look like a plain argument chain.
|
||||
*/
|
||||
PASS(errctx, akbasic_parser_previous(parser, &operator_));
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &arglist));
|
||||
arglist->leaftype = AKBASIC_LEAF_ARGUMENTLIST;
|
||||
arglist->operator_ = AKBASIC_TOK_FUNCTION_ARGUMENT;
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &form));
|
||||
PASS(errctx, akbasic_parser_expression(parser, &arglist->right));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist->right != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"MOVSPR expected a sprite number");
|
||||
tail = arglist->right;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMA),
|
||||
AKBASIC_ERR_SYNTAX, "MOVSPR expected a comma after the sprite number");
|
||||
|
||||
/*
|
||||
* A leading sign makes it relative. `+` has to be consumed here because the
|
||||
* expression parser has no unary plus; `-` is left where it is, because
|
||||
* unary minus is a real operator and re-parsing it would drop the sign.
|
||||
*/
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
if ( peeked != NULL &&
|
||||
(peeked->tokentype == AKBASIC_TOK_PLUS || peeked->tokentype == AKBASIC_TOK_MINUS) ) {
|
||||
formlexeme = "1";
|
||||
if ( peeked->tokentype == AKBASIC_TOK_PLUS ) {
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_PLUS);
|
||||
}
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_parser_expression(parser, &tail->next));
|
||||
FAIL_ZERO_RETURN(errctx, (tail->next != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"MOVSPR expected a coordinate, a distance or an angle");
|
||||
tail = tail->next;
|
||||
|
||||
if ( akbasic_parser_match1(parser, AKBASIC_TOK_SEMICOLON) ) {
|
||||
formlexeme = "2";
|
||||
} else if ( akbasic_parser_match1(parser, AKBASIC_TOK_HASHMARK) ) {
|
||||
formlexeme = "3";
|
||||
} else {
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMA),
|
||||
AKBASIC_ERR_SYNTAX,
|
||||
"MOVSPR expected `, y', `; angle' or `# speed'");
|
||||
if ( formlexeme[0] == '0' ) {
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
if ( peeked != NULL && peeked->tokentype == AKBASIC_TOK_PLUS ) {
|
||||
formlexeme = "1";
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_PLUS);
|
||||
} else if ( peeked != NULL && peeked->tokentype == AKBASIC_TOK_MINUS ) {
|
||||
formlexeme = "1";
|
||||
}
|
||||
} else if ( akbasic_parser_peek(parser) != NULL &&
|
||||
akbasic_parser_peek(parser)->tokentype == AKBASIC_TOK_PLUS ) {
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_PLUS);
|
||||
}
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_parser_expression(parser, &tail->next));
|
||||
FAIL_ZERO_RETURN(errctx, (tail->next != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"MOVSPR expected a second value");
|
||||
|
||||
PASS(errctx, akbasic_leaf_new_literal_int(form, formlexeme));
|
||||
form->next = arglist->right;
|
||||
arglist->right = form;
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, operator_->lexeme, arglist));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read an optional `WHILE`/`UNTIL` condition off a DO or a LOOP.
|
||||
*
|
||||
* Both ends of a DO/LOOP take the same optional clause, so both parse it the
|
||||
* same way. @p kind comes back as one of the AKBASIC_LOOPCOND_* values and
|
||||
* @p condition is NULL when there was no clause.
|
||||
*/
|
||||
static akerr_ErrorContext *parse_loop_condition(akbasic_Parser *parser, akbasic_ASTLeaf **condition, int *kind)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Token *peeked = NULL;
|
||||
|
||||
*condition = NULL;
|
||||
*kind = AKBASIC_LOOPCOND_NONE;
|
||||
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
if ( peeked == NULL || peeked->tokentype != AKBASIC_TOK_COMMAND ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( strcmp(peeked->lexeme, "WHILE") == 0 ) {
|
||||
*kind = AKBASIC_LOOPCOND_WHILE;
|
||||
} else if ( strcmp(peeked->lexeme, "UNTIL") == 0 ) {
|
||||
*kind = AKBASIC_LOOPCOND_UNTIL;
|
||||
} else {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_COMMAND);
|
||||
|
||||
/* A loop condition is a condition, so a lone `=` in it is an equality test. */
|
||||
parser->comparing = true;
|
||||
PASS(errctx, akbasic_parser_expression(parser, condition));
|
||||
parser->comparing = false;
|
||||
FAIL_ZERO_RETURN(errctx, (*condition != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected a condition after WHILE or UNTIL");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* DO [WHILE|UNTIL (condition)]
|
||||
*
|
||||
* Pushes the loop's scope at parse time, exactly as FOR does and for the same
|
||||
* reason: the condition leaf has to outlive the line it was written on, and an
|
||||
* environment's leaf pool is what gives it somewhere to live.
|
||||
*/
|
||||
akerr_ErrorContext *akbasic_parse_do(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Runtime *runtime = parser->runtime;
|
||||
akbasic_Environment *parent = runtime->environment;
|
||||
akbasic_Environment *newenv = NULL;
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_ASTLeaf *condition = NULL;
|
||||
int kind = AKBASIC_LOOPCOND_NONE;
|
||||
int64_t firstline = parent->lineno + 1;
|
||||
|
||||
PASS(errctx, akbasic_runtime_new_environment(runtime));
|
||||
newenv = runtime->environment;
|
||||
runtime->environment = parent;
|
||||
|
||||
/*
|
||||
* Parsed against the parent's token stream but stored in the new scope --
|
||||
* the body is scanned line by line afterwards, and the new scope must not be
|
||||
* active until parsing is done.
|
||||
*/
|
||||
PASS(errctx, parse_loop_condition(parser, &condition, &kind));
|
||||
|
||||
newenv->isDoLoop = true;
|
||||
newenv->loopFirstLine = firstline;
|
||||
newenv->doConditionKind = kind;
|
||||
if ( condition != NULL ) {
|
||||
PASS(errctx, akbasic_leaf_clone(condition, &newenv->doLeafPool, &newenv->doConditionLeaf));
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "DO", NULL));
|
||||
|
||||
runtime->environment = newenv;
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* LOOP [WHILE|UNTIL (condition)]
|
||||
*
|
||||
* The condition hangs off the command leaf's `.right`, with the WHILE/UNTIL
|
||||
* choice in that leaf's integer literal and the expression on its `.left`. No
|
||||
* cloning: this line is still scanned when the verb runs.
|
||||
*/
|
||||
akerr_ErrorContext *akbasic_parse_loop(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_ASTLeaf *holder = NULL;
|
||||
akbasic_ASTLeaf *condition = NULL;
|
||||
int kind = AKBASIC_LOOPCOND_NONE;
|
||||
|
||||
PASS(errctx, parse_loop_condition(parser, &condition, &kind));
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
if ( condition != NULL ) {
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &holder));
|
||||
PASS(errctx, akbasic_leaf_init(holder, AKBASIC_LEAF_LITERAL_INT));
|
||||
holder->literal_int = kind;
|
||||
holder->left = condition;
|
||||
}
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "LOOP", holder));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* ON (expression) GOTO|GOSUB (target) [, ...]
|
||||
*
|
||||
* Flattened to one argument chain: a marker carrying the GOSUB flag, the
|
||||
* selector expression, then the targets. Same trick akbasic_parse_draw and
|
||||
* akbasic_parse_movspr use, so the exec handler walks a plain list.
|
||||
*/
|
||||
akerr_ErrorContext *akbasic_parse_on(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *arglist = NULL;
|
||||
akbasic_ASTLeaf *marker = NULL;
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_ASTLeaf *tail = NULL;
|
||||
akbasic_Token *operator_ = NULL;
|
||||
bool gosub = false;
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &arglist));
|
||||
arglist->leaftype = AKBASIC_LEAF_ARGUMENTLIST;
|
||||
arglist->operator_ = AKBASIC_TOK_FUNCTION_ARGUMENT;
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &marker));
|
||||
PASS(errctx, akbasic_leaf_init(marker, AKBASIC_LEAF_LITERAL_INT));
|
||||
|
||||
PASS(errctx, akbasic_parser_expression(parser, &marker->next));
|
||||
FAIL_ZERO_RETURN(errctx, (marker->next != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected ON (expression) GOTO|GOSUB (line) [, ...]");
|
||||
tail = marker->next;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMAND),
|
||||
AKBASIC_ERR_SYNTAX, "Expected GOTO or GOSUB after ON (expression)");
|
||||
PASS(errctx, akbasic_parser_previous(parser, &operator_));
|
||||
if ( strcmp(operator_->lexeme, "GOSUB") == 0 ) {
|
||||
gosub = true;
|
||||
} else {
|
||||
FAIL_NONZERO_RETURN(errctx, strcmp(operator_->lexeme, "GOTO"), AKBASIC_ERR_SYNTAX,
|
||||
"Expected GOTO or GOSUB after ON (expression)");
|
||||
}
|
||||
marker->literal_int = (gosub ? 1 : 0);
|
||||
|
||||
for ( ;; ) {
|
||||
PASS(errctx, akbasic_parser_expression(parser, &tail->next));
|
||||
FAIL_ZERO_RETURN(errctx, (tail->next != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected a line number or label after ON ... %s",
|
||||
(gosub ? "GOSUB" : "GOTO"));
|
||||
tail = tail->next;
|
||||
if ( !akbasic_parser_match1(parser, AKBASIC_TOK_COMMA) ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
arglist->right = marker;
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "ON", arglist));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* RESUME [NEXT | (line)]
|
||||
*
|
||||
* `NEXT` is a COMMAND token, so the default command path -- which parses the
|
||||
* rval as an expression -- cannot read it: `RESUME NEXT` came back as "Expected
|
||||
* expression or literal". The word is kept as the command leaf's rval so the
|
||||
* exec handler can tell the three forms apart by looking at it.
|
||||
*/
|
||||
akerr_ErrorContext *akbasic_parse_resume(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_ASTLeaf *target = NULL;
|
||||
akbasic_Token *peeked = NULL;
|
||||
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
if ( peeked != NULL && peeked->tokentype == AKBASIC_TOK_COMMAND &&
|
||||
strcmp(peeked->lexeme, "NEXT") == 0 ) {
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_COMMAND);
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &target));
|
||||
PASS(errctx, akbasic_leaf_new_command(target, "NEXT", NULL));
|
||||
} else if ( peeked != NULL && peeked->tokentype != AKBASIC_TOK_COLON ) {
|
||||
PASS(errctx, akbasic_parser_expression(parser, &target));
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "RESUME", target));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* PRINT [USING (format);] (expression)
|
||||
*
|
||||
* Only the USING form needs a parse handler; without it PRINT keeps the default
|
||||
* path, which parses one expression. `USING` is a COMMAND token, so the default
|
||||
* path could not read it -- the same reason RESUME needed one for `NEXT`.
|
||||
*
|
||||
* The result is a command leaf whose rval is an argument list of exactly two:
|
||||
* the format and the value. A plain PRINT keeps its single-expression rval, so
|
||||
* the exec handler tells them apart by looking for the list.
|
||||
*/
|
||||
akerr_ErrorContext *akbasic_parse_print(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_ASTLeaf *arglist = NULL;
|
||||
akbasic_ASTLeaf *right = NULL;
|
||||
akbasic_Token *peeked = NULL;
|
||||
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
|
||||
/*
|
||||
* `PRINT #1, expr` writes to a file channel. A C128 spells it `PRINT#1` with
|
||||
* no space, which this scanner cannot produce: `PRINT#` reads as an
|
||||
* identifier with an integer suffix, and a verb name carrying a suffix is
|
||||
* refused (deviation 30). So the `#` is its own token and the space before
|
||||
* it is required. Recorded in TODO.md section 5.
|
||||
*
|
||||
* The channel form is marked by a leaf named "PRINT#" holding an argument
|
||||
* list of two: the channel and the value.
|
||||
*/
|
||||
if ( peeked != NULL && peeked->tokentype == AKBASIC_TOK_HASHMARK ) {
|
||||
akbasic_ASTLeaf *arglist = NULL;
|
||||
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_HASHMARK);
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &arglist));
|
||||
arglist->leaftype = AKBASIC_LEAF_ARGUMENTLIST;
|
||||
arglist->operator_ = AKBASIC_TOK_FUNCTION_ARGUMENT;
|
||||
PASS(errctx, akbasic_parser_expression(parser, &arglist->right));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist->right != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected PRINT #(channel), (expression)");
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMA),
|
||||
AKBASIC_ERR_SYNTAX,
|
||||
"Expected a comma after PRINT #(channel)");
|
||||
PASS(errctx, akbasic_parser_expression(parser, &arglist->right->next));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist->right->next != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected PRINT #(channel), (expression)");
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "PRINT#", arglist));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
if ( peeked == NULL || peeked->tokentype != AKBASIC_TOK_COMMAND ||
|
||||
strcmp(peeked->lexeme, "USING") != 0 ) {
|
||||
/* The ordinary PRINT: one expression, or none at all. */
|
||||
if ( peeked != NULL && peeked->tokentype != AKBASIC_TOK_COLON ) {
|
||||
PASS(errctx, akbasic_parser_expression(parser, &right));
|
||||
}
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "PRINT", right));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_COMMAND);
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &arglist));
|
||||
arglist->leaftype = AKBASIC_LEAF_ARGUMENTLIST;
|
||||
arglist->operator_ = AKBASIC_TOK_FUNCTION_ARGUMENT;
|
||||
|
||||
PASS(errctx, akbasic_parser_expression(parser, &arglist->right));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist->right != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected PRINT USING (format); (expression)");
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_SEMICOLON),
|
||||
AKBASIC_ERR_SYNTAX,
|
||||
"Expected a semicolon between PRINT USING's format and its value");
|
||||
PASS(errctx, akbasic_parser_expression(parser, &arglist->right->next));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist->right->next != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected PRINT USING (format); (expression)");
|
||||
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, "PRINT", arglist));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* The argument list, for verbs that may be written with no arguments at all --
|
||||
* bare `KEY` lists the macros, bare `RENUMBER` renumbers with its defaults.
|
||||
* `akbasic_parse_arglist` insists on at least one argument, which is right for
|
||||
* every other verb that uses it.
|
||||
*/
|
||||
akerr_ErrorContext *akbasic_parse_optional_arglist(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_ASTLeaf *expr = NULL;
|
||||
akbasic_Token *operator_ = NULL;
|
||||
akbasic_Token *peeked = NULL;
|
||||
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
if ( peeked != NULL && peeked->tokentype != AKBASIC_TOK_COLON ) {
|
||||
PASS(errctx, akbasic_parse_arglist(parser, dest));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
PASS(errctx, akbasic_parser_previous(parser, &operator_));
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &expr));
|
||||
PASS(errctx, akbasic_leaf_new_command(expr, operator_->lexeme, NULL));
|
||||
*dest = expr;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_parse_let(akbasic_Parser *parser, akbasic_ASTLeaf **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
@@ -388,7 +804,21 @@ akerr_ErrorContext *akbasic_parse_if(akbasic_Parser *parser, akbasic_ASTLeaf **d
|
||||
akbasic_ASTLeaf *branch = NULL;
|
||||
akbasic_Token *operator_ = NULL;
|
||||
|
||||
PASS(errctx, akbasic_parser_relation(parser, &relation));
|
||||
/*
|
||||
* Everything from here to the THEN is a condition, so a lone `=` in it is an
|
||||
* equality test. Cleared immediately afterwards: the statement the THEN arm
|
||||
* holds may well be an assignment.
|
||||
*/
|
||||
/*
|
||||
* The whole expression, not just a relation. The reference parses one
|
||||
* relation here, which is below AND and OR in the grammar chain -- so
|
||||
* `IF A# = 5 AND B# = 3 THEN` stopped after the first comparison, left the
|
||||
* AND unconsumed, and reported "Incomplete IF statement" against a line that
|
||||
* is ordinary BASIC. A condition is an expression.
|
||||
*/
|
||||
parser->comparing = true;
|
||||
PASS(errctx, akbasic_parser_expression(parser, &relation));
|
||||
parser->comparing = false;
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMAND),
|
||||
AKBASIC_ERR_SYNTAX, "Incomplete IF statement");
|
||||
PASS(errctx, akbasic_parser_previous(parser, &operator_));
|
||||
@@ -423,6 +853,34 @@ akerr_ErrorContext *akbasic_parse_input(akbasic_Parser *parser, akbasic_ASTLeaf
|
||||
akbasic_ASTLeaf *promptexpr = NULL;
|
||||
akbasic_ASTLeaf *identifier = NULL;
|
||||
akbasic_ASTLeaf *command = NULL;
|
||||
akbasic_Token *peeked = NULL;
|
||||
|
||||
/*
|
||||
* `INPUT #1, VAR` reads a line from a file channel rather than from the
|
||||
* sink. Same spelling note as PRINT: the `#` is its own token and the space
|
||||
* before it is required.
|
||||
*/
|
||||
peeked = akbasic_parser_peek(parser);
|
||||
if ( peeked != NULL && peeked->tokentype == AKBASIC_TOK_HASHMARK ) {
|
||||
akbasic_ASTLeaf *arglist = NULL;
|
||||
|
||||
(void)akbasic_parser_match1(parser, AKBASIC_TOK_HASHMARK);
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &arglist));
|
||||
arglist->leaftype = AKBASIC_LEAF_ARGUMENTLIST;
|
||||
arglist->operator_ = AKBASIC_TOK_FUNCTION_ARGUMENT;
|
||||
PASS(errctx, akbasic_parser_expression(parser, &arglist->right));
|
||||
FAIL_ZERO_RETURN(errctx, (arglist->right != NULL), AKBASIC_ERR_SYNTAX,
|
||||
"Expected INPUT #(channel), (variable)");
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_parser_match1(parser, AKBASIC_TOK_COMMA),
|
||||
AKBASIC_ERR_SYNTAX, "Expected a comma after INPUT #(channel)");
|
||||
PASS(errctx, akbasic_parser_primary(parser, &arglist->right->next));
|
||||
FAIL_ZERO_RETURN(errctx, akbasic_leaf_is_identifier(arglist->right->next),
|
||||
AKBASIC_ERR_SYNTAX, "Expected INPUT #(channel), (variable)");
|
||||
PASS(errctx, akbasic_parser_new_leaf(parser, &command));
|
||||
PASS(errctx, akbasic_leaf_new_command(command, "INPUT#", arglist));
|
||||
*dest = command;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
PASS(errctx, akbasic_parser_expression(parser, &promptexpr));
|
||||
PASS(errctx, akbasic_parser_primary(parser, &identifier));
|
||||
|
||||
Reference in New Issue
Block a user