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;
}
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());
}
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();
}
}
}
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;
}
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;
}
Aggregations