Search in sources :

Example 6 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformFunctionCall.

private Node transformFunctionCall(FunctionCall node) {
    Node call = createCallOrNew(Token.CALL, transform(node.getTarget()));
    call.setLineno(node.getLineno());
    decompiler.addToken(Token.LP);
    List<AstNode> args = node.getArguments();
    for (int i = 0; i < args.size(); i++) {
        AstNode arg = args.get(i);
        call.addChildToBack(transform(arg));
        if (i < args.size() - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    decompiler.addToken(Token.RP);
    return call;
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 7 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformReturn.

private Node transformReturn(ReturnStatement node) {
    boolean expClosure = Boolean.TRUE.equals(node.getProp(Node.EXPRESSION_CLOSURE_PROP));
    boolean isArrow = Boolean.TRUE.equals(node.getProp(Node.ARROW_FUNCTION_PROP));
    if (expClosure) {
        if (!isArrow) {
            decompiler.addName(" ");
        }
    } else {
        decompiler.addToken(Token.RETURN);
    }
    AstNode rv = node.getReturnValue();
    Node value = rv == null ? null : transform(rv);
    if (!expClosure)
        decompiler.addEOL(Token.SEMI);
    return rv == null ? new Node(Token.RETURN, node.getLineno()) : new Node(Token.RETURN, value, node.getLineno());
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 8 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformBlock.

private Node transformBlock(AstNode node) {
    if (node instanceof Scope) {
        pushScope((Scope) node);
    }
    try {
        List<Node> kids = new ArrayList<Node>();
        for (Node kid : node) {
            kids.add(transform((AstNode) kid));
        }
        node.removeChildren();
        for (Node kid : kids) {
            node.addChildToBack(kid);
        }
        return node;
    } finally {
        if (node instanceof Scope) {
            popScope();
        }
    }
}
Also used : Scope(org.mozilla.javascript.ast.Scope) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) ArrayList(java.util.ArrayList) AstNode(org.mozilla.javascript.ast.AstNode)

Example 9 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformVariables.

private Node transformVariables(VariableDeclaration node) {
    decompiler.addToken(node.getType());
    transformVariableInitializers(node);
    // Might be most robust to have parser record whether it was
    // a variable declaration statement, possibly as a node property.
    AstNode parent = node.getParent();
    if (!(parent instanceof Loop) && !(parent instanceof LetNode)) {
        decompiler.addEOL(Token.SEMI);
    }
    return node;
}
Also used : WhileLoop(org.mozilla.javascript.ast.WhileLoop) ForLoop(org.mozilla.javascript.ast.ForLoop) GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) Loop(org.mozilla.javascript.ast.Loop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) DoLoop(org.mozilla.javascript.ast.DoLoop) LetNode(org.mozilla.javascript.ast.LetNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 10 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method arrayCompTransformHelper.

private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) {
    decompiler.addToken(Token.LB);
    int lineno = node.getLineno();
    Node expr = transform(node.getResult());
    List<ArrayComprehensionLoop> loops = node.getLoops();
    int numLoops = loops.size();
    // Walk through loops, collecting and defining their iterator symbols.
    Node[] iterators = new Node[numLoops];
    Node[] iteratedObjs = new Node[numLoops];
    for (int i = 0; i < numLoops; i++) {
        ArrayComprehensionLoop acl = loops.get(i);
        decompiler.addName(" ");
        decompiler.addToken(Token.FOR);
        if (acl.isForEach()) {
            decompiler.addName("each ");
        }
        decompiler.addToken(Token.LP);
        AstNode iter = acl.getIterator();
        String name = null;
        if (iter.getType() == Token.NAME) {
            name = iter.getString();
            decompiler.addName(name);
        } else {
            // destructuring assignment
            decompile(iter);
            name = currentScriptOrFn.getNextTempName();
            defineSymbol(Token.LP, name, false);
            expr = createBinary(Token.COMMA, createAssignment(Token.ASSIGN, iter, createName(name)), expr);
        }
        Node init = createName(name);
        // Define as a let since we want the scope of the variable to
        // be restricted to the array comprehension
        defineSymbol(Token.LET, name, false);
        iterators[i] = init;
        if (acl.isForOf()) {
            decompiler.addName("of ");
        } else {
            decompiler.addToken(Token.IN);
        }
        iteratedObjs[i] = transform(acl.getIteratedObject());
        decompiler.addToken(Token.RP);
    }
    // generate code for tmpArray.push(body)
    Node call = createCallOrNew(Token.CALL, createPropertyGet(createName(arrayName), null, "push", 0));
    Node body = new Node(Token.EXPR_VOID, call, lineno);
    if (node.getFilter() != null) {
        decompiler.addName(" ");
        decompiler.addToken(Token.IF);
        decompiler.addToken(Token.LP);
        body = createIf(transform(node.getFilter()), body, null, lineno);
        decompiler.addToken(Token.RP);
    }
    // Now walk loops in reverse to build up the body statement.
    int pushed = 0;
    try {
        for (int i = numLoops - 1; i >= 0; i--) {
            ArrayComprehensionLoop acl = loops.get(i);
            Scope loop = createLoopNode(// no label
            null, acl.getLineno());
            pushScope(loop);
            pushed++;
            body = createForIn(Token.LET, loop, iterators[i], iteratedObjs[i], body, acl.isForEach(), acl.isForOf());
        }
    } finally {
        for (int i = 0; i < pushed; i++) {
            popScope();
        }
    }
    decompiler.addToken(Token.RB);
    // Now that we've accumulated any destructuring forms,
    // add expr to the call node; it's pushed on each iteration.
    call.addChildToBack(expr);
    return body;
}
Also used : Scope(org.mozilla.javascript.ast.Scope) ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)80 FunctionNode (org.mozilla.javascript.ast.FunctionNode)22 LetNode (org.mozilla.javascript.ast.LetNode)22 ScriptNode (org.mozilla.javascript.ast.ScriptNode)19 XmlString (org.mozilla.javascript.ast.XmlString)15 InfixExpression (org.mozilla.javascript.ast.InfixExpression)13 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)9 Comment (org.mozilla.javascript.ast.Comment)8 Name (org.mozilla.javascript.ast.Name)8 ArrayList (java.util.ArrayList)6 Block (org.mozilla.javascript.ast.Block)5 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)5 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)5 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)4 ErrorNode (org.mozilla.javascript.ast.ErrorNode)4 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)4 DoLoop (org.mozilla.javascript.ast.DoLoop)3 Scope (org.mozilla.javascript.ast.Scope)3 VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)3 IOException (java.io.IOException)2