From 819826627f90aecdd1d91e24563fe2162061ccf2 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 4 Nov 2023 17:56:15 +0100 Subject: fix: skip parsing empty expression --- parser/README.md | 2 +- parser/expr.go | 4 ++-- parser/interpreter_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/parser/README.md b/parser/README.md index 23c23b8..c9ff440 100644 --- a/parser/README.md +++ b/parser/README.md @@ -70,7 +70,7 @@ Go language support: - [ ] select statement - [x] binary operators - [ ] unary operators -- [ ] logical operators && and || +- [x] logical operators && and || - [ ] assign operators - [x] operator precedence rules - [x] parenthesis expressions diff --git a/parser/expr.go b/parser/expr.go index 18c97a5..d626e9e 100644 --- a/parser/expr.go +++ b/parser/expr.go @@ -85,7 +85,7 @@ func (p *Parser) ParseExpr(in Tokens) (out Tokens, err error) { if out, err = p.ParseLogical(out); err != nil { return out, err } - if l := len(out) - 1; out[l].Id == lang.Define || out[l].Id == lang.Assign { + if l := len(out) - 1; l >= 0 && (out[l].Id == lang.Define || out[l].Id == lang.Assign) { // Handle the assignment of a logical expression. s1 := p.subExprLen(out[:l]) head, err := p.ParseLogical(out[:l-s1]) @@ -139,7 +139,7 @@ func (p *Parser) ParseExprStr(s string) (tokens Tokens, err error) { // If the last token is not a logical operator then the function is idempotent. func (p *Parser) ParseLogical(in Tokens) (out Tokens, err error) { l := len(in) - 1 - if !in[l].Id.IsLogicalOp() { + if l < 0 || !in[l].Id.IsLogicalOp() { return in, nil } xp := strconv.Itoa(p.labelCount[p.scope]) diff --git a/parser/interpreter_test.go b/parser/interpreter_test.go index 9794337..02cd105 100644 --- a/parser/interpreter_test.go +++ b/parser/interpreter_test.go @@ -96,8 +96,8 @@ func TestIf(t *testing.T) { {src: "a := 0; if a == 1 { a = 2 } else { a = 1 }; a", res: "1"}, {src: "a := 0; if a == 1 { a = 2 } else if a == 0 { a = 3 } else { a = 1 }; a", res: "3"}, {src: "a := 0; if a == 1 { a = 2 } else if a == 2 { a = 3 } else { a = 1 }; a", res: "1"}, - //{src: "a := 1; if a > 0 && a < 2 { a = 3 }; a", res: "3"}, - //{src: "a := 1; if a < 0 || a < 2 { a = 3 }; a", res: "3"}, + {src: "a := 1; if a > 0 && a < 2 { a = 3 }; a", res: "3"}, + {src: "a := 1; if a < 0 || a < 2 { a = 3 }; a", res: "3"}, }) } -- cgit v1.2.3