Skip to content

Commit

Permalink
Allowed for empty arrays and trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Dec 9, 2024
1 parent 1a36c14 commit 5f75b5f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
12 changes: 12 additions & 0 deletions scripts/trailing.toy
Original file line number Diff line number Diff line change
@@ -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";
28 changes: 26 additions & 2 deletions source/toy_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions source/toy_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/integrations/test_arrays.toy
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ barr[1][1] = 99;

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;

0 comments on commit 5f75b5f

Please sign in to comment.