From: Matthew Dempsky Date: Sat, 20 Feb 2016 08:00:53 +0000 (-0800) Subject: cmd/compile: simplify if statement parsing X-Git-Tag: go1.7beta1~1812 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3a11c8d3190210c510c152471ddb85eb36a15e76;p=gostls13.git cmd/compile: simplify if statement parsing Somewhat notably, this means long if statement chains are now parsed recursively, rather than iteratively. This shouldn't be a concern though, as several other functions (e.g., gen, typecheck, walk) already use recursion to process the parsed if statement Node trees. Change-Id: Ic8c12ace9021c870d60c06f5db86a48c4ec57084 Reviewed-on: https://go-review.googlesource.com/19756 Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/compile/internal/gc/parser.go b/src/cmd/compile/internal/gc/parser.go index 048b81e01b..3a5b508393 100644 --- a/src/cmd/compile/internal/gc/parser.go +++ b/src/cmd/compile/internal/gc/parser.go @@ -1064,65 +1064,16 @@ func (p *parser) if_stmt() *Node { stmt.Nbody = p.loop_body("if clause") - l := p.elseif_list_else() // does markdcl - - n := stmt - popdcl() - for nn := l; nn != nil; nn = nn.Next { - if nn.N.Op == OIF { - popdcl() - } - n.Rlist = list1(nn.N) - n = nn.N - } - - return stmt -} - -func (p *parser) elseif() *NodeList { - if trace && Debug['x'] != 0 { - defer p.trace("elseif")() - } - - // LELSE LIF already consumed - markdcl() // matching popdcl in if_stmt - - stmt := p.if_header() - if stmt.Left == nil { - Yyerror("missing condition in if statement") - } - - stmt.Nbody = p.loop_body("if clause") - - return list1(stmt) -} - -func (p *parser) elseif_list_else() (l *NodeList) { - if trace && Debug['x'] != 0 { - defer p.trace("elseif_list_else")() - } - - for p.got(LELSE) { - if p.got(LIF) { - l = concat(l, p.elseif()) + if p.got(LELSE) { + if p.tok == LIF { + stmt.Rlist = list1(p.if_stmt()) } else { - l = concat(l, p.else_()) - break + stmt.Rlist = list1(p.compound_stmt(true)) } } - return l -} - -func (p *parser) else_() *NodeList { - if trace && Debug['x'] != 0 { - defer p.trace("else")() - } - - l := &NodeList{N: p.compound_stmt(true)} - l.End = l - return l - + popdcl() + return stmt } // switch_stmt parses both expression and type switch statements.