-
-
Notifications
You must be signed in to change notification settings - Fork 473
Add else if support
#879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add else if support
#879
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for else if chaining in conditional expressions, allowing multiple conditional branches without deep nesting. The implementation leverages recursive parsing to handle arbitrary chains of else if statements.
Key changes:
- Modified the parser to recognize and handle
else ifsyntax by recursively callingparseConditionalIf()when "if" follows "else" - Added test cases covering basic
else iffunctionality, error handling, end-to-end execution, and AST printing
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| parser/parser.go | Added conditional logic to check for "if" token after "else" and recursively parse nested conditionals |
| parser/parser_test.go | Added test case for basic else if parsing and error test for malformed else followed by identifier |
| expr_test.go | Added end-to-end test verifying correct evaluation of else if expressions |
| ast/print_test.go | Added test verifying else if expressions print as nested ternary operators with proper parenthesization |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| { | ||
| "if a { 1 } else if b { 2 } else { 3 }", | ||
| &ConditionalNode{ | ||
| Cond: &IdentifierNode{Value: "a"}, | ||
| Exp1: &IntegerNode{Value: 1}, | ||
| Exp2: &ConditionalNode{ | ||
| Cond: &IdentifierNode{Value: "b"}, | ||
| Exp1: &IntegerNode{Value: 2}, | ||
| Exp2: &IntegerNode{Value: 3}}}, | ||
| }, |
Copilot
AI
Dec 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a test case for multiple chained else if statements to verify that the recursive implementation correctly handles chains longer than two conditionals. For example: if a { 1 } else if b { 2 } else if c { 3 } else { 4 } would test that the parser can handle arbitrarily long chains of conditionals.
| { | ||
| `if 1 == 2 { "no" } else if 1 == 1 { "yes" } else { "maybe" }`, | ||
| "yes", | ||
| }, |
Copilot
AI
Dec 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a test case with multiple chained else if statements to ensure the feature works correctly for longer chains. For example: if 1 == 2 { "a" } else if 2 == 3 { "b" } else if 3 == 3 { "c" } else { "d" } with expected result "c".
No description provided.