Fixed list literal issue
@@ -61,6 +61,12 @@ static void advance(Parser *parser) {
}
}
+static void recede(Parser *parser){
+ for (int i = 0; i < parser->current.length; ++i) {
+ backTrack(&parser->scanner);
+ }
+}
+
static void consume(Compiler *compiler, TokenType type, const char *message) {
if (compiler->parser->current.type == type) {
advance(compiler->parser);
@@ -2484,13 +2490,40 @@ static void whileStatement(Compiler *compiler) {
static void unpackListStatement(Compiler *compiler){
int varCount = 0;
Token variables[255];
+ Token previous=compiler->parser->previous;
do {
+ if(!check(compiler,TOKEN_IDENTIFIER)){
+ if(varCount>0){
+ recede(compiler->parser);
+ }
+ int varsAndCommas=varCount+(varCount-1);
+ for(int var=0;var<varsAndCommas;var++){
+ recede(compiler->parser);
+ }
+ recede(compiler->parser);
+ compiler->parser->current=previous;
+ expressionStatement(compiler);
+ return;
+ }
consume(compiler, TOKEN_IDENTIFIER, "Expect variable name.");
variables[varCount] = compiler->parser->previous;
varCount++;
} while (match(compiler, TOKEN_COMMA));
consume(compiler, TOKEN_RIGHT_BRACKET, "Expect ']' after list destructure.");
+
+ if(!check(compiler, TOKEN_EQUAL)){
+ recede(compiler->parser);
+ int varsAndCommas=varCount+(varCount-1);
+ for(int var=0;var<varsAndCommas;var++){
+ recede(compiler->parser);
+ }
+ recede(compiler->parser);
+ compiler->parser->current=previous;
+ expressionStatement(compiler);
+ return;
+ }
+
consume(compiler, TOKEN_EQUAL, "Expect '=' after list destructure.");
expression(compiler);
