]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: simplify if statement parsing
authorMatthew Dempsky <mdempsky@google.com>
Sat, 20 Feb 2016 08:00:53 +0000 (00:00 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Sat, 20 Feb 2016 18:59:57 +0000 (18:59 +0000)
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 <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/parser.go

index 048b81e01bc3bb4bd009fb7c1eefb4f887c487c4..3a5b508393ddc353e9dcc5cfe82adb4582943ee9 100644 (file)
@@ -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.