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

Method parseIf

chapter16/src/main/java/com/seaofnodes/simple/Parser.java:339–378  ·  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

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

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