Skip to content

Commit

Permalink
Merge branch 'main' into clientdata
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Aug 26, 2024
2 parents 721e333 + e551027 commit 876d2b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
15 changes: 15 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,18 @@ func TestCrashKeys(t *testing.T) {
t.Errorf("should not have errored: %v", err)
}
}

func TestParenInIf(t *testing.T) {
inp := `if (1+2)==3 {42}`
s := eval.NewState()
res, err := eval.EvalString(s, inp, false)
if err != nil {
t.Fatalf("should not have errored: %v", err)
}
if res.Type() != object.INTEGER {
t.Fatalf("should have returned an integer, got %#v", res)
}
if res.Inspect() != "42" {
t.Errorf("wrong result, got %q", res.Inspect())
}
}
4 changes: 4 additions & 0 deletions main_test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ cmp stdout json_output
grol -quiet -c '()=>{{"a":1,"b":2}}'
stdout '^\(\)=>{{"a":1,"b":2}}$'

# if extra paren are needed (like for a[x] in the left part of if condition) it should still parse.
grol -quiet -c '()=> if 1+2==3 {4}'
stdout '^\(\)=>if \(1\+2\)==3\{4}$'

-- json_output --
{
"63": 63,
Expand Down
10 changes: 0 additions & 10 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,9 @@ func (p *Parser) parseIfExpression() ast.Node {
expression := &ast.IfExpression{}
expression.Token = p.curToken

needCloseParen := false
if p.peekTokenIs(token.LPAREN) {
needCloseParen = true
p.nextToken()
}

p.nextToken()
expression.Condition = p.parseExpression(LOWEST)

if needCloseParen && !p.expectPeek(token.RPAREN) {
return nil
}

if !p.expectPeek(token.LBRACE) {
return nil
}
Expand Down

0 comments on commit 876d2b9

Please sign in to comment.