diff options
Diffstat (limited to 'js/scripting-lang/baba-yaga-c/src/parser.c')
-rw-r--r-- | js/scripting-lang/baba-yaga-c/src/parser.c | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/js/scripting-lang/baba-yaga-c/src/parser.c b/js/scripting-lang/baba-yaga-c/src/parser.c index 896c24f..c490bd4 100644 --- a/js/scripting-lang/baba-yaga-c/src/parser.c +++ b/js/scripting-lang/baba-yaga-c/src/parser.c @@ -2618,7 +2618,6 @@ static ASTNode* parser_parse_when_expression(Parser* parser) { Token* current_token = parser_peek(parser); if (current_token->type == TOKEN_LPAREN) { /* Expression in parentheses - parse the expression */ - /* Parse expression but stop at 'is' token */ identifiers[i] = parser_parse_expression(parser); if (identifiers[i] == NULL) { /* Cleanup on error */ @@ -2628,13 +2627,6 @@ static ASTNode* parser_parse_when_expression(Parser* parser) { free(identifiers); return NULL; } - - /* Check if we consumed the 'is' token and back up if needed */ - if (parser->current < parser->token_count && - parser->tokens[parser->current]->type == TOKEN_KEYWORD_IS) { - /* We consumed the 'is' token, need to back up */ - parser->current--; - } } else { /* Identifier - parse as identifier */ Token* id_token = parser_advance(parser); @@ -2644,6 +2636,18 @@ static ASTNode* parser_parse_when_expression(Parser* parser) { /* Create a sequence node for the identifiers */ test = ast_sequence_node(identifiers, identifier_count, when_token->line, when_token->column); + + /* Ensure we're positioned at the 'is' token */ + if (parser->current < parser->token_count && + parser->tokens[parser->current]->type != TOKEN_KEYWORD_IS) { + /* We're not at the 'is' token - find it */ + for (int j = parser->current; j < parser->token_count; j++) { + if (parser->tokens[j]->type == TOKEN_KEYWORD_IS) { + parser->current = j; + break; + } + } + } } else { /* Parse as single expression */ test = parser_parse_expression(parser); @@ -2662,6 +2666,14 @@ static ASTNode* parser_parse_when_expression(Parser* parser) { // Parse pattern ASTNode* pattern = parser_parse_when_pattern(parser); if (!pattern) break; + + // Debug: Show current token before consuming 'then' + Token* current_token = parser_peek(parser); + if (current_token) { + DEBUG_TRACE("Before consuming 'then', current token type=%d, lexeme='%s'", + current_token->type, current_token->lexeme ? current_token->lexeme : "NULL"); + } + // Expect 'then' Token* then_token = parser_consume(parser, TOKEN_KEYWORD_THEN, "Expected 'then' after pattern in when case"); if (!then_token) { ast_destroy_node(pattern); break; } @@ -2766,6 +2778,7 @@ static ASTNode* parser_parse_when_pattern(Parser* parser) { if (token->type == TOKEN_IDENTIFIER || token->type == TOKEN_NUMBER || token->type == TOKEN_STRING || + token->type == TOKEN_BOOLEAN || (token->type == TOKEN_IDENTIFIER && token->lexeme && strcmp(token->lexeme, "_") == 0)) { literal_count++; } else if (token->type == TOKEN_LPAREN) { @@ -2796,6 +2809,9 @@ static ASTNode* parser_parse_when_pattern(Parser* parser) { /* If we hit a comparison operator, it's not multi-parameter */ literal_count = 0; break; + } else if (token->type == TOKEN_SEMICOLON) { + /* If we hit a semicolon, stop looking */ + break; } else { /* If we hit anything other than a literal or expression, it's not multi-parameter */ literal_count = 0; @@ -2844,6 +2860,9 @@ static ASTNode* parser_parse_when_pattern(Parser* parser) { } else if (lit_token->type == TOKEN_STRING) { /* String pattern */ literals[i] = ast_literal_node(baba_yaga_value_string(lit_token->lexeme), lit_token->line, lit_token->column); + } else if (lit_token->type == TOKEN_BOOLEAN) { + /* Boolean pattern */ + literals[i] = ast_literal_node(baba_yaga_value_boolean(lit_token->literal.boolean), lit_token->line, lit_token->column); } else { /* Cleanup on error */ for (int j = 0; j < i; j++) { @@ -2937,6 +2956,13 @@ static ASTNode* parser_parse_when_pattern(Parser* parser) { return NULL; } DEBUG_TRACE("Parsed pattern test expression"); + + // Debug: Show current token after parsing pattern + Token* after_token = parser_peek(parser); + if (after_token) { + DEBUG_TRACE("After parsing pattern, current token type=%d, lexeme='%s'", + after_token->type, after_token->lexeme ? after_token->lexeme : "NULL"); + } } DEBUG_TRACE("parser_parse_when_pattern success"); |