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

View File

@@ -197,7 +197,7 @@ static ParsingTuple parsingRulesetTable[] = {
{PREC_NONE,group,NULL},// TOY_TOKEN_OPERATOR_PAREN_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_PAREN_RIGHT,
{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_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) {
//read in an array or dictionary compound definition
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);
//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");
Toy_private_emitAstCompound(bucketHandle, rootHandle, TOY_AST_FLAG_COMPOUND_ARRAY);
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 {
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");
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 {
printError(parser, parser->previous, "Unexpected token passed to aggregate precedence rule");
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);
break;
//meta instructions are disallowed
case TOY_AST_PASS:
//NOTE: this *should* be disallowed, but for now it's required for testing
//NO-OP
break;
//meta instructions are disallowed
case TOY_AST_ERROR:
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Invalid AST type found: Unknown 'error'\n" TOY_CC_RESET);
(*rt)->panic = true;