Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ast/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestPrint(t *testing.T) {
{`(3 + 5) / (5 % 3)`, `(3 + 5) / (5 % 3)`},
{`(-(1+1)) == 2`, `-(1 + 1) == 2`},
{`if true { 1 } else { 2 }`, `true ? 1 : 2`},
{`if true { 1 } else if false { 2 } else { 3 }`, `true ? 1 : (false ? 2 : 3)`},
}

for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,10 @@ func TestExpr(t *testing.T) {
`if "a" < "b" {let x = "a"; x} else {"abc"}`,
"a",
},
{
`if 1 == 2 { "no" } else if 1 == 1 { "yes" } else { "maybe" }`,
"yes",
},
Comment on lines +1393 to +1396
Copy link

Copilot AI Dec 14, 2025

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".

Copilot uses AI. Check for mistakes.
{
`1; 2; 3`,
3,
Expand Down
12 changes: 9 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,15 @@ func (p *Parser) parseConditionalIf() Node {
expr1 := p.parseSequenceExpression()
p.expect(Bracket, "}")
p.expect(Operator, "else")
p.expect(Bracket, "{")
expr2 := p.parseSequenceExpression()
p.expect(Bracket, "}")

var expr2 Node
if p.current.Is(Operator, "if") {
expr2 = p.parseConditionalIf()
} else {
p.expect(Bracket, "{")
expr2 = p.parseSequenceExpression()
p.expect(Bracket, "}")
}

return &ConditionalNode{
Cond: nodeCondition,
Expand Down
16 changes: 16 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@ world`},
Exp1: &BoolNode{Value: true},
Exp2: &IdentifierNode{Value: "x"}},
},
{
"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}}},
},
Comment on lines +667 to +676
Copy link

Copilot AI Dec 14, 2025

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.

Copilot uses AI. Check for mistakes.
{
"1; 2; 3",
&SequenceNode{
Expand Down Expand Up @@ -1019,6 +1029,12 @@ func TestParse_error(t *testing.T) {
`unexpected token Operator("if") (1:5)
| 1 + if true { 2 } else { 3 }
| ....^`,
},
{
`if a { 1 } else b`,
`unexpected token Identifier("b") (1:17)
| if a { 1 } else b
| ................^`,
},
{
`list | all(#,,)`,
Expand Down
Loading