diff --git a/scripts/trailing.toy b/scripts/trailing.toy new file mode 100644 index 0000000..b963d90 --- /dev/null +++ b/scripts/trailing.toy @@ -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"; \ No newline at end of file diff --git a/source/toy_parser.c b/source/toy_parser.c index a52d7a3..52433ee 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -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); diff --git a/source/toy_routine.c b/source/toy_routine.c index af6090a..814a5bb 100644 --- a/source/toy_routine.c +++ b/source/toy_routine.c @@ -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; diff --git a/tests/integrations/test_arrays.toy b/tests/integrations/test_arrays.toy index b174f09..1e92e4c 100644 --- a/tests/integrations/test_arrays.toy +++ b/tests/integrations/test_arrays.toy @@ -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; \ No newline at end of file