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

Method parseIf

chapter10/src/main/java/com/seaofnodes/simple/Parser.java:315–354  ·  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

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

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