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

Method parseIf

chapter06/src/main/java/com/seaofnodes/simple/Parser.java:150–187  ·  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

148 * @return a {@link Node}, never {@code null}
149 */
150 private Node parseIf() {
151 require("(");
152 // Parse predicate
153 var pred = require(parseExpression(), ")");
154 // IfNode takes current control and predicate
155 IfNode ifNode = (IfNode)new IfNode(ctrl(), pred).<IfNode>keep().peephole();
156 // Setup projection nodes
157 Node ifT = new ProjNode(ifNode, 0, "True" ).peephole();
158 ifNode.unkeep();
159 Node ifF = new ProjNode(ifNode, 1, "False").peephole();
160 // In if true branch, the ifT proj node becomes the ctrl
161 // But first clone the scope and set it as current
162 int ndefs = _scope.nIns();
163 ScopeNode fScope = _scope.dup(); // Duplicate current scope
164 _xScopes.push(fScope); // For graph visualization we need all scopes
165
166 // Parse the true side
167 ctrl(ifT); // set ctrl token to ifTrue projection
168 parseStatement(); // Parse true-side
169 ScopeNode tScope = _scope;
170
171 // Parse the false side
172 _scope = fScope; // Restore scope, then parse else block if any
173 ctrl(ifF); // Ctrl token is now set to ifFalse projection
174 if (matchx("else")) {
175 parseStatement();
176 fScope = _scope;
177 }
178
179 if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
180 throw error("Cannot define a new name on one arm of an if");
181
182 // Merge results
183 _scope = tScope;
184 _xScopes.pop(); // Discard pushed from graph display
185
186 return ctrl(tScope.mergeScopes(fScope));
187 }
188
189
190 /**

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