* @param no The pattern match node.
* @param bn The node to get bindings from (if exists).
* @return The augmented IfStatement.
*/
public Node augmentIfStatement(Node n, String matchArg, GNode no, Node bn) {
Node block = n.getGeneric(1);
GNode pattern = no.getGeneric(0).getGeneric(0);
List<Integer> indexList = getIndexList(pattern);
int size = block.size();
// Get the last return statement of the block
Node retNode = block.getGeneric(size - 1);
assert ("ReturnStatement" == retNode.getName()) :
"The last statement of the if block is not a return statement";
// Update matching_nodes.
block.set(size - 1, factory.matchingNodesAdd(toIdentifier(matchArg)));
// Check if this node needs to process scope
block.add(factory.processScope(toIdentifier(matchArg)));
// Check if enter a new scope
block.add(factory.checkEnterScope(toIdentifier(matchArg)));
// Check if needed to process scope of the offsprings
String nodeName = table.freshJavaId("nodeName");
String listName = table.freshJavaId("listName");
if (null != indexList && indexList.size() > 1) {
block.add(factory.spOffspringList(listName));
block.add(factory.spRunNode(nodeName, toIdentifier(matchArg)));
int index;
for (int ind = 0; ind < indexList.size() - 1; ind++){
index = indexList.get(ind);
block.add(factory.spGetGeneric(toIdentifier(nodeName),
toLiteral("IntegerLiteral", "" + index)));
block.add(factory.processScope(toIdentifier(nodeName)));
block.add(factory.checkEnterScope(toIdentifier(nodeName)));
block.add(factory.spOffspringListAdd(toIdentifier(listName),
toIdentifier(nodeName)));
}
}
// Add bindings if exists
List<LetBinding> bl = getBindings(bn);
if (null != bl) {
for (LetBinding bind : bl) {
if (!bind.name.equals(spareVar)) {
if (mapper.hasTypeVariables(bind.typeObject)) {
block.add(factory.fieldDecl2(bind.type, bind.name, bind.value));
} else {
block.add(factory.fieldDecl2(bind.type, bind.name,
factory.cast(bind.value)));
}
} else {
if (mapper.hasTypeVariables(bind.typeObject)) {
block.add(factory.assign(toIdentifier(bind.name), bind.value));
} else {
block.add(factory.assign(toIdentifier(bind.name),
factory.cast(bind.value)));
}
}
}
}
// Store the return value
String freshId = table.freshJavaId("retValue");
block.add(factory.storeValue(freshId, retNode.getGeneric(0)));
// Check to exit scope
if (null != indexList && indexList.size() > 1) {
block.add(factory.spForLoop(toIdentifier(listName)));
}
block.add(factory.checkExitScope(toIdentifier(matchArg)));