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

Method parseIf

chapter09/src/main/java/com/seaofnodes/simple/Parser.java:266–302  ·  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

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

Callers 1

parseStatementMethod · 0.95

Calls 14

requireMethod · 0.95
parseExpressionMethod · 0.95
ctrlMethod · 0.95
keepMethod · 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