Search in sources :

Example 26 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy-core by groovy.

the class BinaryExpressionHelper method evaluateBinaryExpressionWithAssignment.

protected void evaluateBinaryExpressionWithAssignment(String method, BinaryExpression expression) {
    Expression leftExpression = expression.getLeftExpression();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    if (leftExpression instanceof BinaryExpression) {
        BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
        if (leftBinExpr.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
            evaluateArrayAssignmentWithOperator(method, expression, leftBinExpr);
            return;
        }
    }
    evaluateBinaryExpression(method, expression);
    // br to leave a copy of rvalue on the stack. see also isPopRequired()
    operandStack.dup();
    controller.getCompileStack().pushLHS(true);
    leftExpression.visit(acg);
    controller.getCompileStack().popLHS();
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Example 27 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy-core by groovy.

the class BinaryExpressionHelper method evaluateBinaryExpression.

protected void evaluateBinaryExpression(String message, BinaryExpression binExp) {
    CompileStack compileStack = controller.getCompileStack();
    Expression receiver = binExp.getLeftExpression();
    Expression arguments = binExp.getRightExpression();
    // ensure VariableArguments are read, not stored
    compileStack.pushLHS(false);
    controller.getInvocationWriter().makeSingleArgumentCall(receiver, message, arguments);
    compileStack.popLHS();
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression)

Example 28 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy-core by groovy.

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);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Example 29 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy-core by groovy.

the class BinaryExpressionMultiTypeDispatcher method isAssignmentToArray.

private boolean isAssignmentToArray(BinaryExpression binExp) {
    Expression leftExpression = binExp.getLeftExpression();
    if (!(leftExpression instanceof BinaryExpression))
        return false;
    BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
    if (leftBinExpr.getOperation().getType() != LEFT_SQUARE_BRACKET)
        return false;
    return true;
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression)

Example 30 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project groovy-core by groovy.

the class BinaryExpressionMultiTypeDispatcher method evaluateCompareExpression.

@Override
protected void evaluateCompareExpression(final MethodCaller compareMethod, BinaryExpression binExp) {
    ClassNode current = getController().getClassNode();
    TypeChooser typeChooser = getController().getTypeChooser();
    int operation = binExp.getOperation().getType();
    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);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression)

Aggregations

BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)93 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)82 Expression (org.codehaus.groovy.ast.expr.Expression)70 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)56 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)50 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)49 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)47 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)38 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)35 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)33 ClassNode (org.codehaus.groovy.ast.ClassNode)31 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)29 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)28 FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)28 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)25 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)23 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)21 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)20 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)20 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)19