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

Method parseIf

chapter11/src/main/java/com/seaofnodes/simple/Parser.java:326–365  ·  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

324 * @return a {@link Node}, never {@code null}
325 */
326 private Node parseIf() {
327 require("(");
328 // Parse predicate
329 var pred = require(parseExpression(), ")").keep();
330 // IfNode takes current control and predicate
331 Node ifNode = new IfNode(ctrl(), pred).peephole();
332 // Setup projection nodes
333 Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
334 Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole().keep();
335 // In if true branch, the ifT proj node becomes the ctrl
336 // But first clone the scope and set it as current
337 int ndefs = _scope.nIns();
338 ScopeNode fScope = _scope.dup(); // Duplicate current scope
339 _xScopes.push(fScope); // For graph visualization we need all scopes
340
341 // Parse the true side
342 ctrl(ifT.unkeep()); // set ctrl token to ifTrue projection
343 _scope.upcast(ifT,pred,false); // Up-cast predicate
344 parseStatement(); // Parse true-side
345 ScopeNode tScope = _scope;
346
347 // Parse the false side
348 _scope = fScope; // Restore scope, then parse else block if any
349 ctrl(ifF.unkeep()); // Ctrl token is now set to ifFalse projection
350 if (matchx("else")) {
351 _scope.upcast(ifF,pred,true); // Up-cast predicate
352 parseStatement();
353 fScope = _scope;
354 }
355 pred.unkeep();
356
357 if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
358 throw error("Cannot define a new name on one arm of an if");
359
360 // Merge results
361 _scope = tScope;
362 _xScopes.pop(); // Discard pushed from graph display
363
364 return ctrl(tScope.mergeScopes(fScope));
365 }
366
367
368 /**

Callers 1

parseStatementMethod · 0.95

Calls 15

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

Tested by

no test coverage detected