Parses a statement if ( expression ) statement [else statement] @return a Node, never null
()
| 148 | * @return a {@link Node}, never {@code null} |
| 149 | */ |
| 150 | private Node parseIf() { |
| 151 | require("("); |
| 152 | // Parse predicate |
| 153 | var pred = require(parseExpression(), ")"); |
| 154 | // IfNode takes current control and predicate |
| 155 | IfNode ifNode = (IfNode)new IfNode(ctrl(), pred).<IfNode>keep().peephole(); |
| 156 | // Setup projection nodes |
| 157 | Node ifT = new ProjNode(ifNode, 0, "True" ).peephole(); |
| 158 | ifNode.unkeep(); |
| 159 | Node ifF = new ProjNode(ifNode, 1, "False").peephole(); |
| 160 | // In if true branch, the ifT proj node becomes the ctrl |
| 161 | // But first clone the scope and set it as current |
| 162 | int ndefs = _scope.nIns(); |
| 163 | ScopeNode fScope = _scope.dup(); // Duplicate current scope |
| 164 | _xScopes.push(fScope); // For graph visualization we need all scopes |
| 165 | |
| 166 | // Parse the true side |
| 167 | ctrl(ifT); // set ctrl token to ifTrue projection |
| 168 | parseStatement(); // Parse true-side |
| 169 | ScopeNode tScope = _scope; |
| 170 | |
| 171 | // Parse the false side |
| 172 | _scope = fScope; // Restore scope, then parse else block if any |
| 173 | ctrl(ifF); // Ctrl token is now set to ifFalse projection |
| 174 | if (matchx("else")) { |
| 175 | parseStatement(); |
| 176 | fScope = _scope; |
| 177 | } |
| 178 | |
| 179 | if( tScope.nIns() != ndefs || fScope.nIns() != ndefs ) |
| 180 | throw error("Cannot define a new name on one arm of an if"); |
| 181 | |
| 182 | // Merge results |
| 183 | _scope = tScope; |
| 184 | _xScopes.pop(); // Discard pushed from graph display |
| 185 | |
| 186 | return ctrl(tScope.mergeScopes(fScope)); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
no test coverage detected