public Node visitRequireExpression(GNode n) {
int nodeSize = n.size();
// get the expression
Node expr = n.getGeneric(nodeSize -1);
passVariables(n, expr);
if (n.hasProperty(INANALYZE)) expr.setProperty(INANALYZE, Boolean.TRUE);
//dispatch this node to translate
Node valExpr = (Node)dispatch(expr);
final Node genericType;
if (expr.hasProperty(TYPE)) {
genericType = mapper.toTypeNode(expr.getProperty(TYPE), false);
} else {
genericType = GNode.create("Type",
GNode.create("QualifiedIdentifier", "Object"),
null);
}
ArrayList<Node> list = new ArrayList<Node>();
for (int id = 0; id < nodeSize - 1; id++) {
Node no = n.getGeneric(id);
list.add(no);
}
// Create require block
List<Node> requireBlock = new ArrayList<Node>();
/* A list of translated expressions */
ArrayList<Node> listExpr = new ArrayList<Node>();
// Add statement
for (Node node: list) {
Node boolNode = node.getGeneric(0);
//boolNode.setProperty(NEWLET, Boolean.TRUE);
if (n.hasProperty(INANALYZE)) {
boolNode.setProperty(INANALYZE, Boolean.TRUE);
}
Node boolExpr = (Node)dispatch(boolNode);
boolExpr = checkToLet(boolExpr, boolNode.getProperty(TYPE));
final String newVar = table.freshJavaId("var");
requireBlock.add(factory.boolVar(newVar, boolExpr));
listExpr.add(toIdentifier(newVar));
Node tagNode = node.getGeneric(1);
String tag = tagNode.getString(0);
Node msgNode = node.getGeneric(2);
Node msgExpr = (Node)dispatch(msgNode);
Node atNode = node.getGeneric(3);
Node atExpr;
if (null == atNode) {
atExpr = nullNode;
} else {
atExpr = (Node)dispatch(atNode);
}
Node ifNode = factory.ifStatement3(toIdentifier(newVar),
GNode.create("StringLiteral",
"\"" + tag + "\""), msgExpr,atExpr);
requireBlock.add(ifNode);
}
// check for returning null (bottom)
if (1 == listExpr.size()) {
Node checkNode = factory.ifStatement4(listExpr.get(0));
requireBlock.add(checkNode);
} else {
Node logicalOr = GNode.create("LogicalOrExpression");
logicalOr.add(factory.isNull(listExpr.get(0)));
logicalOr.add(factory.isNull(listExpr.get(1)));
for (int i = 2; i < listExpr.size(); i++ ) {
logicalOr = GNode.create("LogicalOrExpression", logicalOr,
factory.isNull(listExpr.get(i)));
}
Node checkNode = factory.ifStatement5(logicalOr);
requireBlock.add(checkNode);
}
// Check to return expr
if (1 == listExpr.size()) {
Node checkNode;
if(!"Block".equals(valExpr.getName())) {
checkNode = factory.ifStatement(listExpr.get(0), factory.ret(valExpr));
} else {
checkNode = toIfStatement(listExpr.get(0), valExpr);
}
requireBlock.add(checkNode);
} else {
Node logicalAnd = GNode.create("LogicalAndExpression");
logicalAnd.add(listExpr.get(0));
logicalAnd.add(listExpr.get(1));
for(int i = 2; i < listExpr.size(); i++) {
logicalAnd = GNode.create("LogicalAndExpression", logicalAnd,
listExpr.get(i));
}
Node checkNode;
if (!"Block".equals(valExpr.getName())) {
checkNode = factory.ifStatement(logicalAnd, factory.ret(valExpr));
} else {
checkNode = toIfStatement(logicalAnd, valExpr);
}
requireBlock.add(checkNode);
}
// return null at the end
requireBlock.add(factory.ret(nullNode));
Node ret = factory.requireExpression(genericType, requireBlock);
passBindings(valExpr, ret);
return ret;
}