diff options
| author | Marc Vertes <mvertes@free.fr> | 2023-11-20 15:54:52 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2023-11-20 15:54:52 +0100 |
| commit | 6a0490257bf235d011004bc303306f617ac6ea31 (patch) | |
| tree | e04160a26f78afaa60c4b18b3b16662d35106d97 /parser/expr.go | |
| parent | 001ca51323a1a54c9b3db377f0783c330666c480 (diff) | |
parser: add support for unary operators
Diffstat (limited to 'parser/expr.go')
| -rw-r--r-- | parser/expr.go | 15 |
1 files changed, 13 insertions, 2 deletions
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 |
