Allowed for empty arrays and trailing commas

This commit is contained in:
2024-12-10 10:44:13 +11:00
parent 1a36c14247
commit 5f75b5f1a3
4 changed files with 49 additions and 4 deletions

12
scripts/trailing.toy Normal file
View File

@@ -0,0 +1,12 @@
//test trailing commas
var a = [1, 2, 3, ];
print a;
//test empty arrays
var b = [];
print b;
//deep
assert [[1, 2, 3],[4,99,6],[7,8,9]], "2-D array failed";

View File

@@ -197,7 +197,7 @@ static ParsingTuple parsingRulesetTable[] = {
{PREC_NONE,group,NULL},// TOY_TOKEN_OPERATOR_PAREN_LEFT, {PREC_NONE,group,NULL},// TOY_TOKEN_OPERATOR_PAREN_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_PAREN_RIGHT, {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_PAREN_RIGHT,
{PREC_GROUP,compound,aggregate},// TOY_TOKEN_OPERATOR_BRACKET_LEFT, {PREC_GROUP,compound,aggregate},// TOY_TOKEN_OPERATOR_BRACKET_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACKET_RIGHT, {PREC_NONE,compound,aggregate},// TOY_TOKEN_OPERATOR_BRACKET_RIGHT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACE_LEFT, {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACE_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACE_RIGHT, {PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACE_RIGHT,
@@ -569,13 +569,33 @@ static Toy_AstFlag group(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast*
static Toy_AstFlag compound(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) { static Toy_AstFlag compound(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
//read in an array or dictionary compound definition //read in an array or dictionary compound definition
if (parser->previous.type == TOY_TOKEN_OPERATOR_BRACKET_LEFT) { if (parser->previous.type == TOY_TOKEN_OPERATOR_BRACKET_LEFT) {
//BUGFIX: special case for empty arrays
if (match(parser, TOY_TOKEN_OPERATOR_BRACKET_RIGHT)) {
Toy_private_emitAstPass(bucketHandle, rootHandle);
Toy_private_emitAstCompound(bucketHandle, rootHandle, TOY_AST_FLAG_COMPOUND_ARRAY);
return TOY_AST_FLAG_NONE;
}
parsePrecedence(bucketHandle, parser, rootHandle, PREC_GROUP); parsePrecedence(bucketHandle, parser, rootHandle, PREC_GROUP);
//BUGFIX: special case for trailing commas
if (parser->previous.type == TOY_TOKEN_OPERATOR_BRACKET_RIGHT && parser->current.type != TOY_TOKEN_OPERATOR_BRACKET_RIGHT) {
Toy_private_emitAstCompound(bucketHandle, rootHandle, TOY_AST_FLAG_COMPOUND_ARRAY);
//NOTE: will probably need tweaking for tables
return TOY_AST_FLAG_NONE;
}
consume(parser, TOY_TOKEN_OPERATOR_BRACKET_RIGHT, "Expected ']' at the end of compound expression"); consume(parser, TOY_TOKEN_OPERATOR_BRACKET_RIGHT, "Expected ']' at the end of compound expression");
Toy_private_emitAstCompound(bucketHandle, rootHandle, TOY_AST_FLAG_COMPOUND_ARRAY); Toy_private_emitAstCompound(bucketHandle, rootHandle, TOY_AST_FLAG_COMPOUND_ARRAY);
return TOY_AST_FLAG_NONE; return TOY_AST_FLAG_NONE;
//TODO: read in a dictionary //TODO: read in a table
}
else if (parser->previous.type == TOY_TOKEN_OPERATOR_BRACKET_RIGHT) {
//allows for trailing commas
Toy_private_emitAstPass(bucketHandle, rootHandle);
return TOY_AST_FLAG_NONE;
} }
else { else {
printError(parser, parser->previous, "Unexpected token passed to compound precedence rule"); printError(parser, parser->previous, "Unexpected token passed to compound precedence rule");
@@ -598,6 +618,10 @@ static Toy_AstFlag aggregate(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_
consume(parser, TOY_TOKEN_OPERATOR_BRACKET_RIGHT, "Expected ']' at the end of index expression"); consume(parser, TOY_TOKEN_OPERATOR_BRACKET_RIGHT, "Expected ']' at the end of index expression");
return TOY_AST_FLAG_INDEX; return TOY_AST_FLAG_INDEX;
} }
else if (parser->previous.type == TOY_TOKEN_OPERATOR_BRACKET_RIGHT) {
Toy_private_emitAstPass(bucketHandle, rootHandle);
return TOY_AST_FLAG_NONE;
}
else { else {
printError(parser, parser->previous, "Unexpected token passed to aggregate precedence rule"); printError(parser, parser->previous, "Unexpected token passed to aggregate precedence rule");
Toy_private_emitAstError(bucketHandle, rootHandle); Toy_private_emitAstError(bucketHandle, rootHandle);

View File

@@ -715,11 +715,11 @@ static unsigned int writeRoutineCode(Toy_Routine** rt, Toy_Ast* ast) {
result += writeInstructionAccess(rt, ast->varAccess); result += writeInstructionAccess(rt, ast->varAccess);
break; break;
//meta instructions are disallowed
case TOY_AST_PASS: case TOY_AST_PASS:
//NOTE: this *should* be disallowed, but for now it's required for testing //NO-OP
break; break;
//meta instructions are disallowed
case TOY_AST_ERROR: case TOY_AST_ERROR:
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Invalid AST type found: Unknown 'error'\n" TOY_CC_RESET); fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Invalid AST type found: Unknown 'error'\n" TOY_CC_RESET);
(*rt)->panic = true; (*rt)->panic = true;

View File

@@ -15,3 +15,12 @@ barr[1][1] = 99;
assert barr == [[1, 2, 3],[4,99,6],[7,8,9]], "2-D array failed"; assert barr == [[1, 2, 3],[4,99,6],[7,8,9]], "2-D array failed";
//test trailing commas
var a = [1, 2, 3, ];
print a;
//test empty arrays
var b = [];
print b;