diff --git a/eval/eval_test.go b/eval/eval_test.go index 83bb85e6..7088f2f7 100644 --- a/eval/eval_test.go +++ b/eval/eval_test.go @@ -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()) + } +} diff --git a/main_test.txtar b/main_test.txtar index f210583c..9d95b871 100644 --- a/main_test.txtar +++ b/main_test.txtar @@ -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, diff --git a/parser/parser.go b/parser/parser.go index cbdbb08c..501e7723 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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 }