private Node transformForInLoop(ForInLoop loop) { decompiler.addToken(Token.FOR); if (loop.isForEach()) decompiler.addName("each "); decompiler.addToken(Token.LP); loop.setType(Token.LOOP); pushScope(loop); try { int declType = -1; AstNode iter = loop.getIterator(); if (iter instanceof VariableDeclaration) { declType = ((VariableDeclaration)iter).getType(); } Node lhs = transform(iter); if (loop.isForOf()) { decompiler.addName("of "); } else { decompiler.addToken(Token.IN); } Node obj = transform(loop.getIteratedObject()); decompiler.addToken(Token.RP); decompiler.addEOL(Token.LC); Node body = transform(loop.getBody()); decompiler.addEOL(Token.RC); return createForIn(declType, loop, lhs, obj, body, loop.isForEach(), loop.isForOf()); } finally { popScope(); } }
/** * generate constraints for a for-in loop */ private void processForInLoop(ForInLoop loop) { AstNode it = loop.getIterator(); AstNode obj = loop.getIteratedObject(); ITypeTerm objTerm = processExpression(obj); MapType mapType = new MapType(factory.freshTypeVar()); addSubTypeConstraint( objTerm, findOrCreateTypeTerm(mapType, loop.getLineno()), loop.getLineno(), (solution) -> typeEqualityError("for-in loop can only iterate over map objects; " + obj.toSource() + " is not a map", solution.typeOfTerm(objTerm), mapType, locationOf(obj))); if (it instanceof VariableDeclaration){ VariableDeclaration vd = (VariableDeclaration)it; for (VariableInitializer vi : vd.getVariables()){ AstNode target = vi.getTarget(); if (target instanceof Name){ ITypeTerm leftTerm = findOrCreateNameDeclarationTerm((Name)target); addSubTypeConstraint( leftTerm, findOrCreateTypeTerm(StringType.make(), loop.getLineno()), loop.getLineno(), (solution) -> typeEqualityError( "loop variable " + it.toSource() + " of for-in loop must have string type", solution.typeOfTerm(leftTerm), StringType.make(), locationOf(it))); } } } else { error("unhandled type of iterator in for-in loop: " + it.getClass().getName(), it); } }
private void print(ForInLoop node) throws IOException { writer.append("for"); if (node.isForEach()) { writer.append(" each"); } writer.ws().append("("); print(node.getIterator()); writer.append(" in "); print(node.getIteratedObject()); writer.append(')').ws(); print(node.getBody()); }
/** * This method generates constraints for all relevant AstNodes. It delegates its work to various * processXXX() methods that handle AstNodes of type XXX. */ @Override public boolean visit(AstNode node) { if (node instanceof VariableInitializer){ processVariableInitializer(node); } else if (node instanceof ReturnStatement){ processReturnStatement((ReturnStatement)node); } else if (node instanceof ExpressionStatement){ processExpressionStatement((ExpressionStatement)node); } else if (node instanceof ForLoop){ processForLoop((ForLoop)node); } else if (node instanceof ForInLoop){ processForInLoop((ForInLoop)node); }else if (node instanceof WhileLoop){ processWhileLoop((WhileLoop)node); } else if (node instanceof DoLoop){ processDoLoop((DoLoop)node); } else if (node instanceof NewExpression){ processNewExpression((NewExpression)node); } else if (node instanceof FunctionCall){ processFunctionCall((FunctionCall)node); } else if (node instanceof ElementGet){ processElementGet((ElementGet)node); } else if (node instanceof FunctionNode){ processFunctionNode((FunctionNode)node); } else if (node instanceof IfStatement){ processIfStatement((IfStatement)node); } else if (node instanceof KeywordLiteral){ processKeywordLiteral((KeywordLiteral)node); } else if (node instanceof SwitchStatement){ processSwitchStatement((SwitchStatement)node); } else if (node instanceof SwitchCase){ processSwitchCase((SwitchCase)node); } else if ((node instanceof AstRoot) || //AstRoot: no constraints need to be generated (node instanceof BreakStatement) || //BreakStatement: no constraints need to be generated (node instanceof VariableDeclaration) || //VariableDeclaration: we generate constraints for its constituent VariableInitializer nodes (node instanceof Name) || //Name: generate constraints for complex expressions that refer to names (node instanceof NumberLiteral) || //NumberLiteral: generate constraints for complex expressions that refer to names (node instanceof StringLiteral) || //StringLiteral: generate constraints for complex expressions that refer to names (node instanceof Assignment) || // Assignment is a special case of InfixExpression (node instanceof ArrayLiteral) || (node instanceof UnaryExpression) || (node instanceof InfixExpression) || (node instanceof ConditionalExpression) || (node instanceof ParenthesizedExpression) || (node instanceof EmptyExpression) || (node instanceof ObjectLiteral) || (node instanceof EmptyStatement) || (node instanceof ContinueStatement) || (node instanceof Scope) || (node instanceof Block)){ // // occurs in programs with for loops -- nothing to be done here? /* nothing */ } else { error("unsupported node " + node.toSource().trim() + " of type: " + node.getClass().getName(), node); } return true; }