diff options
| author | Marc Vertes <mvertes@free.fr> | 2026-01-26 18:44:29 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2026-01-26 18:44:29 +0100 |
| commit | aa5861917ac2543f85bf4cfefbb69cf501d4de41 (patch) | |
| tree | 94122ea3167c295da822f03d075a085b4f87e9ea /parser/parse.go | |
| parent | ece8bdbc45afc3ea626db884ea3283d9a8395ae5 (diff) | |
Do not consider assign operators in expression handling, they are
part of statements.
Isolate parsing of left hand side and right hand side in assignments.
Diffstat (limited to 'parser/parse.go')
| -rw-r--r-- | parser/parse.go | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/parser/parse.go b/parser/parse.go index 18613f4..9c1fd13 100644 --- a/parser/parse.go +++ b/parser/parse.go @@ -163,11 +163,37 @@ func (p *Parser) parseStmt(in Tokens) (out Tokens, err error) { func (p *Parser) parseAssign(in Tokens, aindex int) (out Tokens, err error) { rhs := in[aindex+1:].Split(lang.Comma) - if len(rhs) == 1 { - return p.parseExpr(in, "") - } lhs := in[:aindex].Split(lang.Comma) define := in[aindex].Tok == lang.Define + if len(rhs) == 1 { + for _, e := range lhs { + toks, err := p.parseExpr(e, "") + if err != nil { + return out, err + } + if define && len(e) == 1 && e[0].Tok == lang.Ident { + p.Symbols.Add(symbol.UnsetAddr, e[0].Str, vm.Value{}, symbol.Var, nil, false) + } + out = append(out, toks...) + } + toks, err := p.parseExpr(rhs[0], "") + if err != nil { + return out, err + } + if out[len(out)-1].Tok == lang.Index { + // Map elements cannot be assigned directly, but only through IndexAssign. + out = out[:len(out)-1] + out = append(out, toks...) + out = append(out, newToken(lang.IndexAssign, "", in[aindex].Pos)) + } else { + out = append(out, toks...) + if out[len(out)-1].Tok != lang.Range { + out = append(out, newToken(in[aindex].Tok, "", in[aindex].Pos)) + } + } + return out, err + } + // Multiple values in right hand side. for i, e := range rhs { toks, err := p.parseExpr(lhs[i], "") if err != nil { @@ -185,8 +211,15 @@ func (p *Parser) parseAssign(in Tokens, aindex int) (out Tokens, err error) { if err != nil { return out, err } - out = append(out, toks...) - out = append(out, newToken(in[aindex].Tok, "", in[aindex].Pos)) + if out[len(out)-1].Tok == lang.Index { + // Map elements cannot be assigned directly, but only through IndexAssign. + out = out[:len(out)-1] + out = append(out, toks...) + out = append(out, newToken(lang.IndexAssign, "", in[aindex].Pos)) + } else { + out = append(out, toks...) + out = append(out, newToken(in[aindex].Tok, "", in[aindex].Pos)) + } } return out, err } |
