Consume the COLON token: a line can hold several statements
The token has existed since the scanner was written and nothing read it, so 10 PRINT A$ : REM ... was a parse error. Leading separators are consumed before each statement and an empty statement yields a NULL leaf rather than an error, so a trailing colon and a run of them are both legal. BASIC 7.0 scopes everything after THEN to the condition, which the reference had no opinion about because it never got here. The rule is not "skip when false": the rest of the line belongs to whichever arm was written last. A whole FOR/NEXT on one line still does not loop -- block skipping works by source line. Recorded in TODO.md as what group A has to fix first. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
17
tests/language/statements/multiple_per_line.bas
Normal file
17
tests/language/statements/multiple_per_line.bas
Normal file
@@ -0,0 +1,17 @@
|
||||
10 REM Statements separated by colons. The COLON token existed from the start of
|
||||
20 REM the port and nothing consumed it, so a line could hold only one statement.
|
||||
30 PRINT "A" : PRINT "B"
|
||||
40 A# = 1 : B# = 2 : PRINT A# + B#
|
||||
50 REM An empty statement is not an error: a trailing separator, or a run of them.
|
||||
60 PRINT "C" :
|
||||
70 PRINT "D" :: PRINT "E"
|
||||
80 REM Everything after THEN belongs to the condition, which is BASIC 7.0 and is
|
||||
90 REM not something the reference had an opinion about -- it never got here.
|
||||
100 IF 1 == 1 THEN PRINT "TRUE-1" : PRINT "TRUE-2"
|
||||
110 IF 1 == 0 THEN PRINT "NEVER-1" : PRINT "NEVER-2"
|
||||
120 REM With an ELSE, the tail belongs to the ELSE instead.
|
||||
130 IF 1 == 1 THEN PRINT "T" ELSE PRINT "E" : PRINT "E-TAIL"
|
||||
140 IF 1 == 0 THEN PRINT "T" ELSE PRINT "E" : PRINT "E-TAIL"
|
||||
150 REM A colon does not confuse a bare verb that takes no argument.
|
||||
160 PRINT : PRINT "AFTER A BARE PRINT"
|
||||
170 PRINT "DONE"
|
||||
14
tests/language/statements/multiple_per_line.txt
Normal file
14
tests/language/statements/multiple_per_line.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
A
|
||||
B
|
||||
3
|
||||
C
|
||||
D
|
||||
E
|
||||
TRUE-1
|
||||
TRUE-2
|
||||
T
|
||||
E
|
||||
E-TAIL
|
||||
|
||||
AFTER A BARE PRINT
|
||||
DONE
|
||||
@@ -39,6 +39,42 @@ static void expect_parse_error(const char *line)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse a whole line and report how many statements came out of it.
|
||||
*
|
||||
* The statement loop the runtime uses, in miniature: parse until the token
|
||||
* stream is spent, skipping the NULL leaf that an empty statement produces.
|
||||
*/
|
||||
static int count_statements(const char *line)
|
||||
{
|
||||
akbasic_Parser parser;
|
||||
akbasic_ASTLeaf *leaf = NULL;
|
||||
akerr_ErrorContext *e = NULL;
|
||||
int count = 0;
|
||||
|
||||
e = akbasic_scanner_zero(&HARNESS_RUNTIME);
|
||||
if ( e == NULL ) {
|
||||
e = akbasic_scanner_scan(&HARNESS_RUNTIME, line, NULL, 0);
|
||||
}
|
||||
if ( e == NULL ) {
|
||||
e = akbasic_parser_init(&parser, &HARNESS_RUNTIME);
|
||||
}
|
||||
while ( e == NULL && !akbasic_parser_is_at_end(&parser) ) {
|
||||
leaf = NULL;
|
||||
e = akbasic_parser_parse(&parser, &leaf);
|
||||
if ( e == NULL && leaf != NULL ) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
if ( e != NULL ) {
|
||||
fprintf(stderr, "FAIL: \"%s\" did not parse: %s\n", line, e->message);
|
||||
akbasic_test_failures += 1;
|
||||
test_discard_error(e);
|
||||
return -1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
akbasic_ASTLeaf *leaf = NULL;
|
||||
@@ -106,6 +142,38 @@ int main(void)
|
||||
expect_parse_error("READ 1");
|
||||
expect_parse_error("DATA A#");
|
||||
|
||||
/*
|
||||
* Colons separate statements. The token has existed since the scanner was
|
||||
* written and nothing consumed it, so every one of these was a parse error
|
||||
* until the statement loop learned to skip past it -- TODO.md section 4.
|
||||
*/
|
||||
TEST_REQUIRE_INT(count_statements("PRINT 1"), 1);
|
||||
TEST_REQUIRE_INT(count_statements("PRINT 1 : PRINT 2"), 2);
|
||||
TEST_REQUIRE_INT(count_statements("A# = 1 : B# = 2 : PRINT A# + B#"), 3);
|
||||
|
||||
/* An empty statement is not an error, and does not count as one. */
|
||||
TEST_REQUIRE_INT(count_statements("PRINT 1 :"), 1);
|
||||
TEST_REQUIRE_INT(count_statements(": PRINT 1"), 1);
|
||||
TEST_REQUIRE_INT(count_statements("PRINT 1 :: PRINT 2"), 2);
|
||||
TEST_REQUIRE_INT(count_statements(":"), 0);
|
||||
TEST_REQUIRE_INT(count_statements(":::"), 0);
|
||||
|
||||
/*
|
||||
* A verb that takes no rval stops at the separator rather than trying to
|
||||
* parse one out of it.
|
||||
*/
|
||||
TEST_REQUIRE_INT(count_statements("PRINT : PRINT 2"), 2);
|
||||
|
||||
/*
|
||||
* IF takes exactly one statement for each arm; the rest of the line arrives
|
||||
* at the statement loop as ordinary statements, and it is the *runtime* that
|
||||
* decides whether to run them. That split is asserted end to end in
|
||||
* tests/language/statements/multiple_per_line.bas, because it is a question
|
||||
* about execution rather than about the shape of the tree.
|
||||
*/
|
||||
TEST_REQUIRE_INT(count_statements("IF 1 == 1 THEN PRINT 1 : PRINT 2"), 2);
|
||||
TEST_REQUIRE_INT(count_statements("IF 1 == 1 THEN PRINT 1 ELSE PRINT 2 : PRINT 3"), 2);
|
||||
|
||||
harness_stop();
|
||||
return akbasic_test_failures;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user