mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile, go/parser: simpler binary expression parsing
The existing nested loops are too tricky for me to grok and don't seem necessary. Change-Id: I75c65c8470b799d6f4cfb05bb1b4796c5d7d32e7 Reviewed-on: https://go-review.googlesource.com/19927 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
c8579e57cb
commit
49d1e30710
2 changed files with 17 additions and 24 deletions
|
|
@ -1142,17 +1142,14 @@ func (p *parser) bexpr(prec int) *Node {
|
|||
// don't trace bexpr - only leads to overly nested trace output
|
||||
|
||||
x := p.uexpr()
|
||||
for {
|
||||
t := prectab[p.tok]
|
||||
for tprec := t.prec; tprec >= prec; tprec-- {
|
||||
for tprec == prec {
|
||||
p.next()
|
||||
y := p.bexpr(t.prec + 1)
|
||||
x = Nod(t.op, x, y)
|
||||
t = prectab[p.tok]
|
||||
tprec = t.prec
|
||||
}
|
||||
}
|
||||
if t.prec < prec {
|
||||
return x
|
||||
}
|
||||
p.next()
|
||||
x = Nod(t.op, x, p.bexpr(t.prec+1))
|
||||
}
|
||||
}
|
||||
|
||||
func (p *parser) expr() *Node {
|
||||
|
|
|
|||
|
|
@ -1597,23 +1597,19 @@ func (p *parser) parseBinaryExpr(lhs bool, prec1 int) ast.Expr {
|
|||
}
|
||||
|
||||
x := p.parseUnaryExpr(lhs)
|
||||
for _, prec := p.tokPrec(); prec >= prec1; prec-- {
|
||||
for {
|
||||
op, oprec := p.tokPrec()
|
||||
if oprec != prec {
|
||||
break
|
||||
if oprec < prec1 {
|
||||
return x
|
||||
}
|
||||
pos := p.expect(op)
|
||||
if lhs {
|
||||
p.resolve(x)
|
||||
lhs = false
|
||||
}
|
||||
y := p.parseBinaryExpr(false, prec+1)
|
||||
y := p.parseBinaryExpr(false, oprec+1)
|
||||
x = &ast.BinaryExpr{X: p.checkExpr(x), OpPos: pos, Op: op, Y: p.checkExpr(y)}
|
||||
}
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// If lhs is set and the result is an identifier, it is not resolved.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue