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

Method parseIf

chapter12/src/main/java/com/seaofnodes/simple/Parser.java:329–368  ·  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

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

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