use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformTry.
private Node transformTry(TryStatement node) {
decompiler.addToken(Token.TRY);
decompiler.addEOL(Token.LC);
Node tryBlock = transform(node.getTryBlock());
decompiler.addEOL(Token.RC);
Node catchBlocks = new Block();
for (CatchClause cc : node.getCatchClauses()) {
decompiler.addToken(Token.CATCH);
decompiler.addToken(Token.LP);
String varName = cc.getVarName().getIdentifier();
decompiler.addName(varName);
Node catchCond = null;
AstNode ccc = cc.getCatchCondition();
if (ccc != null) {
decompiler.addName(" ");
decompiler.addToken(Token.IF);
catchCond = transform(ccc);
} else {
catchCond = new EmptyExpression();
}
decompiler.addToken(Token.RP);
decompiler.addEOL(Token.LC);
Node body = transform(cc.getBody());
decompiler.addEOL(Token.RC);
catchBlocks.addChildToBack(createCatch(varName, catchCond, body, cc.getLineno()));
}
Node finallyBlock = null;
if (node.getFinallyBlock() != null) {
decompiler.addToken(Token.FINALLY);
decompiler.addEOL(Token.LC);
finallyBlock = transform(node.getFinallyBlock());
decompiler.addEOL(Token.RC);
}
return createTryCatchFinally(tryBlock, catchBlocks, finallyBlock, node.getLineno());
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformVariableInitializers.
private Node transformVariableInitializers(VariableDeclaration node) {
List<VariableInitializer> vars = node.getVariables();
int size = vars.size(), i = 0;
for (VariableInitializer var : vars) {
AstNode target = var.getTarget();
AstNode init = var.getInitializer();
Node left = null;
if (var.isDestructuring()) {
// decompile but don't transform
decompile(target);
left = target;
} else {
left = transform(target);
}
Node right = null;
if (init != null) {
decompiler.addToken(Token.ASSIGN);
right = transform(init);
}
if (var.isDestructuring()) {
if (right == null) {
// TODO: should this ever happen?
node.addChildToBack(left);
} else {
Node d = createDestructuringAssignment(node.getType(), left, right);
node.addChildToBack(d);
}
} else {
if (right != null) {
left.addChildToBack(right);
}
node.addChildToBack(left);
}
if (i++ < size - 1) {
decompiler.addToken(Token.COMMA);
}
}
return node;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformSwitch.
private Node transformSwitch(SwitchStatement node) {
// The switch will be rewritten from:
//
// switch (expr) {
// case test1: statements1;
// ...
// default: statementsDefault;
// ...
// case testN: statementsN;
// }
//
// to:
//
// {
// switch (expr) {
// case test1: goto label1;
// ...
// case testN: goto labelN;
// }
// goto labelDefault;
// label1:
// statements1;
// ...
// labelDefault:
// statementsDefault;
// ...
// labelN:
// statementsN;
// breakLabel:
// }
//
// where inside switch each "break;" without label will be replaced
// by "goto breakLabel".
//
// If the original switch does not have the default label, then
// after the switch he transformed code would contain this goto:
// goto breakLabel;
// instead of:
// goto labelDefault;
decompiler.addToken(Token.SWITCH);
decompiler.addToken(Token.LP);
Node switchExpr = transform(node.getExpression());
decompiler.addToken(Token.RP);
node.addChildToBack(switchExpr);
Node block = new Node(Token.BLOCK, node, node.getLineno());
decompiler.addEOL(Token.LC);
for (SwitchCase sc : node.getCases()) {
AstNode expr = sc.getExpression();
Node caseExpr = null;
if (expr != null) {
decompiler.addToken(Token.CASE);
caseExpr = transform(expr);
} else {
decompiler.addToken(Token.DEFAULT);
}
decompiler.addEOL(Token.COLON);
List<AstNode> stmts = sc.getStatements();
Node body = new Block();
if (stmts != null) {
for (AstNode kid : stmts) {
body.addChildToBack(transform(kid));
}
}
addSwitchCase(block, caseExpr, body);
}
decompiler.addEOL(Token.RC);
closeSwitch(block);
return block;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformForInLoop.
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();
}
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class IRFactory method transformArrayLiteral.
private Node transformArrayLiteral(ArrayLiteral node) {
if (node.isDestructuring()) {
return node;
}
decompiler.addToken(Token.LB);
List<AstNode> elems = node.getElements();
Node array = new Node(Token.ARRAYLIT);
List<Integer> skipIndexes = null;
for (int i = 0; i < elems.size(); ++i) {
AstNode elem = elems.get(i);
if (elem.getType() != Token.EMPTY) {
array.addChildToBack(transform(elem));
} else {
if (skipIndexes == null) {
skipIndexes = new ArrayList<Integer>();
}
skipIndexes.add(i);
}
if (i < elems.size() - 1)
decompiler.addToken(Token.COMMA);
}
decompiler.addToken(Token.RB);
array.putIntProp(Node.DESTRUCTURING_ARRAY_LENGTH, node.getDestructuringLength());
if (skipIndexes != null) {
int[] skips = new int[skipIndexes.size()];
for (int i = 0; i < skipIndexes.size(); i++) skips[i] = skipIndexes.get(i);
array.putProp(Node.SKIP_INDEXES_PROP, skips);
}
return array;
}
Aggregations