Search in sources :

Example 1 with UnaryOperator

use of org.stjs.generator.javascript.UnaryOperator in project st-js by st-js.

the class SetterUnaryTemplate method doVisit.

protected JS doVisit(WriterVisitor<JS> visitor, UnaryTree tree, GenerationContext<JS> context, boolean global) {
    UnaryOperator op = UnaryOperator.valueOf(tree.getKind());
    assert op != null : "Unknow operator:" + tree.getKind();
    BinaryOperator binaryOp = getBinaryOperator(op);
    if (binaryOp == null) {
        return super.visit(visitor, tree, context);
    }
    JS operand = visitor.scan(tree.getExpression(), context);
    TreeWrapper<ExpressionTree, JS> twOperand = context.getCurrentWrapper().child(tree.getExpression());
    JS target = SetterAssignmentTemplate.getTarget(visitor, twOperand, context);
    JS field = SetterAssignmentTemplate.getField(twOperand, context);
    JS value = context.js().binary(binaryOp, Arrays.asList(operand, context.js().number(1)));
    List<JS> arguments = new ArrayList<JS>();
    arguments.add(field);
    arguments.add(value);
    if (op.isPostfix()) {
        arguments.add(context.js().keyword(Keyword.TRUE));
    }
    if (global) {
        arguments.add(0, target);
        return context.js().functionCall(context.js().property(context.js().name("stjs"), "setField"), arguments);
    }
    return context.js().functionCall(context.js().property(target, "set"), arguments);
}
Also used : ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) UnaryOperator(org.stjs.generator.javascript.UnaryOperator) BinaryOperator(org.stjs.generator.javascript.BinaryOperator)

Example 2 with UnaryOperator

use of org.stjs.generator.javascript.UnaryOperator in project st-js by st-js.

the class DefaultUnaryTemplate method visit.

@Override
public JS visit(WriterVisitor<JS> visitor, UnaryTree tree, GenerationContext<JS> context) {
    JS operand = visitor.scan(tree.getExpression(), context);
    UnaryOperator op = UnaryOperator.valueOf(tree.getKind());
    assert op != null : "Unknow operator:" + tree.getKind();
    return context.js().unary(op, operand);
}
Also used : UnaryOperator(org.stjs.generator.javascript.UnaryOperator)

Aggregations

UnaryOperator (org.stjs.generator.javascript.UnaryOperator)2 ExpressionTree (com.sun.source.tree.ExpressionTree)1 ArrayList (java.util.ArrayList)1 BinaryOperator (org.stjs.generator.javascript.BinaryOperator)1