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

Method parseIf

chapter08/src/main/java/com/seaofnodes/simple/Parser.java:267–304  ·  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

265 * @return a {@link Node}, never {@code null}
266 */
267 private Node parseIf() {
268 require("(");
269 // Parse predicate
270 var pred = require(parseExpression(), ")");
271 // IfNode takes current control and predicate
272 IfNode ifNode = (IfNode)new IfNode(ctrl(), pred).<IfNode>keep().peephole();
273 // Setup projection nodes
274 Node ifT = new ProjNode(ifNode, 0, "True" ).peephole();
275 ifNode.unkeep();
276 Node ifF = new ProjNode(ifNode, 1, "False").peephole();
277 // In if true branch, the ifT proj node becomes the ctrl
278 // But first clone the scope and set it as current
279 int ndefs = _scope.nIns();
280 ScopeNode fScope = _scope.dup(); // Duplicate current scope
281 _xScopes.push(fScope); // For graph visualization we need all scopes
282
283 // Parse the true side
284 ctrl(ifT); // set ctrl token to ifTrue projection
285 parseStatement(); // Parse true-side
286 ScopeNode tScope = _scope;
287
288 // Parse the false side
289 _scope = fScope; // Restore scope, then parse else block if any
290 ctrl(ifF); // Ctrl token is now set to ifFalse projection
291 if (matchx("else")) {
292 parseStatement();
293 fScope = _scope;
294 }
295
296 if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
297 throw error("Cannot define a new name on one arm of an if");
298
299 // Merge results
300 _scope = tScope;
301 _xScopes.pop(); // Discard pushed from graph display
302
303 return ctrl(tScope.mergeScopes(fScope));
304 }
305
306
307 /**

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