Commit 49d1e307 authored by Matthew Dempsky's avatar Matthew Dempsky

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: default avatarRobert Griesemer <gri@golang.org>
parent c8579e57
......@@ -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))
}
return x
}
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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment