MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / parseIf

Method parseIf

chapter07/src/main/java/com/seaofnodes/simple/Parser.java:220–257  ·  view source on GitHub ↗

Parses a statement if ( expression ) statement [else statement] @return a Node, never null

()

Source from the content-addressed store, hash-verified

218 * @return a {@link Node}, never {@code null}
219 */
220 private Node parseIf() {
221 require("(");
222 // Parse predicate
223 var pred = require(parseExpression(), ")");
224 // IfNode takes current control and predicate
225 IfNode ifNode = (IfNode)new IfNode(ctrl(), pred).<IfNode>keep().peephole();
226 // Setup projection nodes
227 Node ifT = new ProjNode(ifNode, 0, "True" ).peephole();
228 ifNode.unkeep();
229 Node ifF = new ProjNode(ifNode, 1, "False").peephole();
230 // In if true branch, the ifT proj node becomes the ctrl
231 // But first clone the scope and set it as current
232 int ndefs = _scope.nIns();
233 ScopeNode fScope = _scope.dup(); // Duplicate current scope
234 _xScopes.push(fScope); // For graph visualization we need all scopes
235
236 // Parse the true side
237 ctrl(ifT); // set ctrl token to ifTrue projection
238 parseStatement(); // Parse true-side
239 ScopeNode tScope = _scope;
240
241 // Parse the false side
242 _scope = fScope; // Restore scope, then parse else block if any
243 ctrl(ifF); // Ctrl token is now set to ifFalse projection
244 if (matchx("else")) {
245 parseStatement();
246 fScope = _scope;
247 }
248
249 if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
250 throw error("Cannot define a new name on one arm of an if");
251
252 // Merge results
253 _scope = tScope;
254 _xScopes.pop(); // Discard pushed from graph display
255
256 return ctrl(tScope.mergeScopes(fScope));
257 }
258
259
260 /**

Callers 1

parseStatementMethod · 0.95

Calls 14

requireMethod · 0.95
parseExpressionMethod · 0.95
ctrlMethod · 0.95
parseStatementMethod · 0.95
matchxMethod · 0.95
errorMethod · 0.95
mergeScopesMethod · 0.95
peepholeMethod · 0.45
keepMethod · 0.45
unkeepMethod · 0.45
nInsMethod · 0.45
dupMethod · 0.45

Tested by

no test coverage detected