diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 96a1587d..87caa43b 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -21,7 +21,7 @@ const ( // - LHS: // - MHS: // - RHS: - // - List0: function call arguments + // - List0: function call arguments or associative op arguments // - FlagsSuspendible is "f(x)" vs "f?(x)" // // A zero ID0 means an identifier or literal in ID1, like "foo" or "42". @@ -32,6 +32,9 @@ const ( // operands. The LHS may be null for the "-" and "+" operators, which are // also unary operators. // + // For associative binary operators, both LHS and RHS may be null, and + // multiple operands are in List0. + // // For function calls, like "LHS(List0)", ID0 is IDOpenParen. // // For indexes, like "LHS[RHS]", ID0 is IDOpenBracket. diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 97e39c85..ff252802 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -141,8 +141,8 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { } p.src = p.src[1:] return &a.Node{ - Kind: a.KUse, - ID1: id1, + Kind: a.KUse, + ID1: id1, }, nil } @@ -504,11 +504,29 @@ func (p *parser) parseExpr() (*a.Node, error) { if err != nil { return nil, err } + + if !x.IsAssociative() || x != p.peekID() { + return &a.Node{ + Kind: a.KExpr, + ID0: x, + LHS: lhs, + RHS: rhs, + }, nil + } + + list0 := []*a.Node{lhs, rhs} + for p.peekID() == x { + p.src = p.src[1:] + arg, err := p.parseOperand() + if err != nil { + return nil, err + } + list0 = append(list0, arg) + } return &a.Node{ - Kind: a.KExpr, - ID0: x, - LHS: lhs, - RHS: rhs, + Kind: a.KExpr, + ID0: x, + List0: list0, }, nil } return lhs, nil