use of org.stjs.generator.javascript.BinaryOperator in project st-js by st-js.
the class BinaryWriter method visit.
@Override
public JS visit(WriterVisitor<JS> visitor, BinaryTree tree, GenerationContext<JS> context) {
JS left = visitor.scan(tree.getLeftOperand(), context);
JS right = visitor.scan(tree.getRightOperand(), context);
BinaryOperator op = BinaryOperator.valueOf(tree.getKind());
assert op != null : "Unknow operator:" + tree.getKind();
TypeMirror leftType = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getLeftOperand()));
TypeMirror rightType = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getRightOperand()));
JavaScriptBuilder<JS> b = context.js();
if (isStringPlusChar(leftType, rightType)) {
right = b.functionCall(b.property(b.name("String"), "fromCharCode"), asList(right));
} else if (isStringPlusChar(rightType, leftType)) {
left = b.functionCall(b.property(b.name("String"), "fromCharCode"), asList(left));
}
boolean integerDivision = tree.getKind() == Kind.DIVIDE && TypesUtils.isIntegral(leftType) && TypesUtils.isIntegral(rightType);
@SuppressWarnings("unchecked") JS expr = b.binary(op, asList(left, right));
if (integerDivision) {
// force a cast for integer division to have the expected behavior in JavaScript too
JS target = b.property(b.name("stjs"), "trunc");
return b.functionCall(target, Collections.singleton(expr));
}
return expr;
}
use of org.stjs.generator.javascript.BinaryOperator in project st-js by st-js.
the class ReplaceBinaryWriter method visit.
@Override
public JS visit(WriterVisitor<JS> visitor, BinaryTree tree, GenerationContext<JS> context) {
JS left = visitor.scan(tree.getLeftOperand(), context);
// JS right = visitor.scan(tree.getRightOperand(), context);
BinaryOperator op = BinaryOperator.valueOf(tree.getKind());
assert op != null : "Unknow operator:" + tree.getKind();
@SuppressWarnings("unchecked") JS // replaces on purpose the second operand at 2
expr = context.js().binary(op, Arrays.asList(left, context.js().number(2)));
return expr;
}
use of org.stjs.generator.javascript.BinaryOperator 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);
}
Aggregations