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

Method parseIf

chapter05/src/main/java/com/seaofnodes/simple/Parser.java:150–186  ·  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 Node ifF = new ProjNode(ifNode, 1, "False").peephole();
159 // In if true branch, the ifT proj node becomes the ctrl
160 // But first clone the scope and set it as current
161 int ndefs = _scope.nIns();
162 ScopeNode fScope = _scope.dup(); // Duplicate current scope
163 _xScopes.push(fScope); // For graph visualization we need all scopes
164
165 // Parse the true side
166 ctrl(ifT); // set ctrl token to ifTrue projection
167 parseStatement(); // Parse true-side
168 ScopeNode tScope = _scope;
169
170 // Parse the false side
171 _scope = fScope; // Restore scope, then parse else block if any
172 ctrl(ifF); // Ctrl token is now set to ifFalse projection
173 if (matchx("else")) {
174 parseStatement();
175 fScope = _scope;
176 }
177
178 if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
179 throw error("Cannot define a new name on one arm of an if");
180
181 // Merge results
182 _scope = tScope;
183 _xScopes.pop(); // Discard pushed from graph display
184
185 return ctrl(tScope.mergeScopes(fScope));
186 }
187
188
189 /**

Callers 1

parseStatementMethod · 0.95

Calls 13

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
nInsMethod · 0.45
dupMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected