Skip to content

Commit

Permalink
fix: parser test complicated operation expression
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Jan 17, 2025
1 parent 27d54e5 commit bd3f188
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch test function",
"name": "parser",
"type": "go",
"request": "launch",
"mode": "test",
Expand All @@ -17,7 +17,7 @@
},

{
"name": "Launch",
"name": "cell",
"type": "go",
"request": "launch",
"mode": "exec",
Expand Down
13 changes: 10 additions & 3 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,10 @@ func (p *parser) parseUntil(until lexer.Item) []Node {
// is returned in "reached"
func (p *parser) parseUntilEither(untils []lexer.Item) (res []Node, reached lexer.Item) {
for {
if p.i >= len(p.input) - 1 {
return
}

current := p.input[p.i]

// Check if we have reached the end
Expand All @@ -1171,9 +1175,12 @@ func (p *parser) parseUntilEither(untils []lexer.Item) (res []Node, reached lexe

one := p.parseOne(true)
if one != nil {
next := p.lookAhead(1)
if _, isOperationNode := opsCharToOp[next.Val]; next.Type == lexer.OPERATOR && isOperationNode {
one = p.parseOperation(one, false)
if p.i < len(p.input) - 1 {
// look ahead one more to see if there is an operator
next := p.lookAhead(1)
if _, isOperationNode := opsCharToOp[next.Val]; next.Type == lexer.OPERATOR && isOperationNode {
one = p.parseOperation(one, false)
}
}
res = append(res, one)
}
Expand Down
29 changes: 17 additions & 12 deletions compiler/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func TestCondition(t *testing.T) {
}

/*
(a + d > 2 && b == false) || c < 3
a + d > 2 && b == false || c < 3
*/

input = []lexer.Item{
Expand All @@ -405,25 +405,30 @@ func TestCondition(t *testing.T) {
Operator: "&&",
Left: &OperatorNode{
Operator: ">",
Left: &NameNode{Name: "a"},
Right: &NameNode{Name: "b"},
Left: &OperatorNode{
Operator: "+",
Left: &NameNode{Name: "a"},
Right: &NameNode{Name: "d"},
},
Right: &ConstantNode{Type: NUMBER, Value: 2},
},
Right: &OperatorNode{
Operator: "&&",
Operator: "||",
Left: &OperatorNode{
Operator: ">",
Left: &NameNode{Name: "a"},
Right: &NameNode{Name: "b"},
Operator: "==",
Left: &NameNode{Name: "b"},
Right: &ConstantNode{Type: BOOL, Value: 0},
},
Right: &OperatorNode{
Operator: ">",
Left: &NameNode{Name: "b"},
Right: &NameNode{Name: "c"},
Operator: "<",
Left: &NameNode{Name: "c"},
Right: &ConstantNode{Type: NUMBER, Value: 3},
},
},
},
},
}

assert.Equal(t, expected, Parse(input, &option.Options{Debug: false}))

actual := Parse(input, &option.Options{Debug: false})
assert.Equal(t, expected, actual)
}

0 comments on commit bd3f188

Please sign in to comment.