use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method decompileFunctionHeader.
Node decompileFunctionHeader(FunctionNode fn) {
Node mexpr = null;
if (fn.getFunctionName() != null) {
decompiler.addName(fn.getName());
} else if (fn.getMemberExprNode() != null) {
mexpr = transform(fn.getMemberExprNode());
}
boolean isArrow = fn.getFunctionType() == FunctionNode.ARROW_FUNCTION;
boolean noParen = isArrow && fn.getLp() == -1;
if (!noParen) {
decompiler.addToken(Token.LP);
}
List<AstNode> params = fn.getParams();
for (int i = 0; i < params.size(); i++) {
decompile(params.get(i));
if (i < params.size() - 1) {
decompiler.addToken(Token.COMMA);
}
}
if (!noParen) {
decompiler.addToken(Token.RP);
}
if (isArrow) {
decompiler.addToken(Token.ARROW);
}
if (!fn.isExpressionClosure()) {
decompiler.addEOL(Token.LC);
}
return mexpr;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformScript.
private Node transformScript(ScriptNode node) {
decompiler.addToken(Token.SCRIPT);
if (currentScope != null)
Kit.codeBug();
currentScope = node;
Node body = new Node(Token.BLOCK);
for (Node kid : node) {
body.addChildToBack(transform((AstNode) kid));
}
node.removeChildren();
Node children = body.getFirstChild();
if (children != null) {
node.addChildrenToBack(children);
}
return node;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method genExprTransformHelper.
private Node genExprTransformHelper(GeneratorExpression node) {
decompiler.addToken(Token.LP);
int lineno = node.getLineno();
Node expr = transform(node.getResult());
List<GeneratorExpressionLoop> 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++) {
GeneratorExpressionLoop acl = loops.get(i);
decompiler.addName(" ");
decompiler.addToken(Token.FOR);
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 yield = new Node(Token.YIELD, expr, node.getLineno());
Node body = new Node(Token.EXPR_VOID, yield, 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--) {
GeneratorExpressionLoop 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.RP);
return body;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformNewExpr.
private Node transformNewExpr(NewExpression node) {
decompiler.addToken(Token.NEW);
Node nx = createCallOrNew(Token.NEW, transform(node.getTarget()));
nx.setLineno(node.getLineno());
List<AstNode> args = node.getArguments();
decompiler.addToken(Token.LP);
for (int i = 0; i < args.size(); i++) {
AstNode arg = args.get(i);
nx.addChildToBack(transform(arg));
if (i < args.size() - 1) {
decompiler.addToken(Token.COMMA);
}
}
decompiler.addToken(Token.RP);
if (node.getInitializer() != null) {
nx.addChildToBack(transformObjectLiteral(node.getInitializer()));
}
return nx;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformAssignment.
private Node transformAssignment(Assignment node) {
AstNode left = removeParens(node.getLeft());
Node target = null;
if (isDestructuring(left)) {
decompile(left);
target = left;
} else {
target = transform(left);
}
decompiler.addToken(node.getType());
return createAssignment(node.getType(), target, transform(node.getRight()));
}
Aggregations