From 6a0490257bf235d011004bc303306f617ac6ea31 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 20 Nov 2023 15:54:52 +0100 Subject: parser: add support for unary operators --- parser/expr.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'parser/expr.go') diff --git a/parser/expr.go b/parser/expr.go index fa8afa4..207f38b 100644 --- a/parser/expr.go +++ b/parser/expr.go @@ -42,10 +42,21 @@ func (p *Parser) ParseExpr(in Tokens) (out Tokens, err error) { case lang.Int, lang.String: out = append(out, t) vl++ - case lang.Define, lang.Add, lang.Sub, lang.Assign, lang.Equal, lang.Greater, lang.Less, lang.Mul, lang.Land, lang.Lor, lang.Shl, lang.Shr: + case lang.Define, lang.Add, lang.Sub, lang.Assign, lang.Equal, lang.Greater, lang.Less, lang.Mul, lang.Land, lang.Lor, lang.Shl, lang.Shr, lang.Not: + if i == 0 || in[i-1].Id.IsOperator() { + // An operator preceded by an operator or no token is unary. + t.Id = lang.UnaryOp[t.Id] + j := len(out) - 1 + l := out[j] + if p.precedence(l) > 0 { + out = append(out[:j], t, l) + break + } + out = append(out, t) + break + } if vl < 2 { ops = append(ops, t) - break } case lang.ParenBlock: // If the previous token is an arithmetic, logic or assign operator then -- cgit v1.2.3