use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class NAryOperationRewriter method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression exp) {
final int op = exp.getOperation().getType();
int token = TokenUtil.removeAssignment(op);
if (token == op) {
// no transform needed
return super.transform(exp);
}
BinaryExpression operation = new BinaryExpression(exp.getLeftExpression(), Token.newSymbol(token, -1, -1), exp.getRightExpression());
operation.setSourcePosition(exp);
BinaryExpression result = new BinaryExpression(exp.getLeftExpression(), Token.newSymbol(EQUAL, -1, -1), operation);
result.setSourcePosition(exp);
return result;
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionHelper method evaluateCompareExpression.
protected void evaluateCompareExpression(MethodCaller compareMethod, BinaryExpression expression) {
Expression leftExp = expression.getLeftExpression();
TypeChooser typeChooser = controller.getTypeChooser();
ClassNode cn = controller.getClassNode();
ClassNode leftType = typeChooser.resolveType(leftExp, cn);
Expression rightExp = expression.getRightExpression();
ClassNode rightType = typeChooser.resolveType(rightExp, cn);
boolean done = false;
if (ClassHelper.isPrimitiveType(leftType) && ClassHelper.isPrimitiveType(rightType)) {
BinaryExpressionMultiTypeDispatcher helper = new BinaryExpressionMultiTypeDispatcher(getController());
done = helper.doPrimitiveCompare(leftType, rightType, expression);
}
if (!done) {
AsmClassGenerator acg = controller.getAcg();
OperandStack operandStack = controller.getOperandStack();
leftExp.visit(acg);
operandStack.box();
rightExp.visit(acg);
operandStack.box();
compareMethod.call(controller.getMethodVisitor());
ClassNode resType = ClassHelper.boolean_TYPE;
if (compareMethod == findRegexMethod) {
resType = ClassHelper.OBJECT_TYPE;
}
operandStack.replace(resType, 2);
}
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionHelper method loadWithSubscript.
private VariableSlotLoader loadWithSubscript(Expression expression) {
final OperandStack operandStack = controller.getOperandStack();
// subscription
if (expression instanceof BinaryExpression) {
BinaryExpression be = (BinaryExpression) expression;
if (be.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
// right expression is the subscript expression
// we store the result of the subscription on the stack
Expression subscript = be.getRightExpression();
subscript.visit(controller.getAcg());
ClassNode subscriptType = operandStack.getTopOperand();
int id = controller.getCompileStack().defineTemporaryVariable("$subscript", subscriptType, true);
VariableSlotLoader subscriptExpression = new VariableSlotLoader(subscriptType, id, operandStack);
// do modified visit
BinaryExpression newBe = new BinaryExpression(be.getLeftExpression(), be.getOperation(), subscriptExpression);
newBe.copyNodeMetaData(be);
newBe.setSourcePosition(be);
newBe.visit(controller.getAcg());
return subscriptExpression;
}
}
// normal loading of expression
expression.visit(controller.getAcg());
return null;
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionMultiTypeDispatcher method evaluateBinaryExpression.
@Override
protected void evaluateBinaryExpression(final String message, BinaryExpression binExp) {
int operation = removeAssignment(binExp.getOperation().getType());
ClassNode current = getController().getClassNode();
Expression leftExp = binExp.getLeftExpression();
ClassNode leftTypeOrig = getController().getTypeChooser().resolveType(leftExp, current);
ClassNode leftType = leftTypeOrig;
Expression rightExp = binExp.getRightExpression();
ClassNode rightType = getController().getTypeChooser().resolveType(rightExp, current);
AsmClassGenerator acg = getController().getAcg();
OperandStack os = getController().getOperandStack();
if (operation == LEFT_SQUARE_BRACKET) {
leftType = leftTypeOrig.getComponentType();
int operationType = getOperandType(leftType);
BinaryExpressionWriter bew = binExpWriter[operationType];
if (leftTypeOrig.isArray() && isIntCastableType(rightExp) && bew.arrayGet(operation, true)) {
leftExp.visit(acg);
os.doGroovyCast(leftTypeOrig);
rightExp.visit(acg);
os.doGroovyCast(int_TYPE);
bew.arrayGet(operation, false);
os.replace(bew.getArrayGetResultType(), 2);
} else {
super.evaluateBinaryExpression(message, binExp);
}
} else if (operation == DIVIDE) {
int operationType = getOperandType(getController().getTypeChooser().resolveType(binExp, current));
BinaryExpressionWriter bew = binExpWriter[operationType];
if (bew.writeDivision(true)) {
leftExp.visit(acg);
os.doGroovyCast(bew.getDevisionOpResultType());
rightExp.visit(acg);
os.doGroovyCast(bew.getDevisionOpResultType());
bew.writeDivision(false);
} else {
super.evaluateBinaryExpression(message, binExp);
}
} else {
int operationType = getOperandConversionType(leftType, rightType);
BinaryExpressionWriter bew = binExpWriter[operationType];
if (isShiftOperation(operation) && isIntCastableType(rightExp) && bew.write(operation, true)) {
leftExp.visit(acg);
os.doGroovyCast(bew.getNormalOpResultType());
rightExp.visit(acg);
os.doGroovyCast(int_TYPE);
bew.write(operation, false);
} else if (bew.write(operation, true)) {
leftExp.visit(acg);
os.doGroovyCast(bew.getNormalOpResultType());
rightExp.visit(acg);
os.doGroovyCast(bew.getNormalOpResultType());
bew.write(operation, false);
} else {
super.evaluateBinaryExpression(message, binExp);
}
}
}
use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy by apache.
the class BinaryExpressionMultiTypeDispatcher method evaluateCompareExpression.
@Override
protected void evaluateCompareExpression(final MethodCaller compareMethod, BinaryExpression binExp) {
ClassNode current = getController().getClassNode();
TypeChooser typeChooser = getController().getTypeChooser();
Expression leftExp = binExp.getLeftExpression();
ClassNode leftType = typeChooser.resolveType(leftExp, current);
Expression rightExp = binExp.getRightExpression();
ClassNode rightType = typeChooser.resolveType(rightExp, current);
if (!doPrimitiveCompare(leftType, rightType, binExp)) {
super.evaluateCompareExpression(compareMethod, binExp);
}
}
Aggregations