Search in sources :

Example 1 with BinaryExpression

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

the class BinaryExpressionTransformer method transformDeclarationExpression.

private static Expression transformDeclarationExpression(final BinaryExpression bin) {
    Expression leftExpression = bin.getLeftExpression();
    if (leftExpression instanceof VariableExpression) {
        if (ClassHelper.char_TYPE.equals(((VariableExpression) leftExpression).getOriginType())) {
            Expression rightExpression = bin.getRightExpression();
            if (rightExpression instanceof ConstantExpression && ClassHelper.STRING_TYPE.equals(rightExpression.getType())) {
                String text = (String) ((ConstantExpression) rightExpression).getValue();
                if (text.length() == 1) {
                    // optimize char initialization
                    ConstantExpression ce = new ConstantExpression(text.charAt(0), true);
                    ce.setSourcePosition(rightExpression);
                    bin.setRightExpression(ce);
                    return bin;
                }
            }
        }
    }
    return null;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListOfExpressionsExpression(org.codehaus.groovy.transform.sc.ListOfExpressionsExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 2 with BinaryExpression

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

the class BinaryExpressionTransformer method convertInOperatorToTernary.

private Expression convertInOperatorToTernary(final BinaryExpression bin, final Expression rightExpression, final Expression leftExpression) {
    MethodCallExpression call = new MethodCallExpression(rightExpression, "isCase", leftExpression);
    call.setMethodTarget((MethodNode) bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
    call.setSourcePosition(bin);
    call.copyNodeMetaData(bin);
    TernaryExpression tExp = new TernaryExpression(new BooleanExpression(new BinaryExpression(rightExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null))), new BinaryExpression(leftExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null)), call);
    return staticCompilationTransformer.transform(tExp);
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 3 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project gcontracts by andresteingress.

the class Assertion method or.

public void or(T other) {
    Validate.notNull(other);
    BooleanExpression newBooleanExpression = new BooleanExpression(new BinaryExpression(booleanExpression(), Token.newSymbol(Types.LOGICAL_OR, -1, -1), other.booleanExpression()));
    newBooleanExpression.setSourcePosition(booleanExpression());
    renew(newBooleanExpression);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression)

Example 4 with BinaryExpression

use of org.codehaus.groovy.ast.expr.BinaryExpression in project spock by spockframework.

the class AbstractDeepBlockRewriter method visitBinaryExpression.

@Override
public final void visitBinaryExpression(BinaryExpression expr) {
    BinaryExpression oldBinaryExpression = currBinaryExpr;
    currBinaryExpr = expr;
    try {
        doVisitBinaryExpression(expr);
    } finally {
        currBinaryExpr = oldBinaryExpression;
    }
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression)

Example 5 with BinaryExpression

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

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)

Aggregations

BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)141 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)118 Expression (org.codehaus.groovy.ast.expr.Expression)105 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)86 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)85 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)79 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)69 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)67 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)62 ClassNode (org.codehaus.groovy.ast.ClassNode)54 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)52 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)50 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)46 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)42 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)39 FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)39 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)39 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)37 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)37 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)35