Java Code Examples for org.codehaus.groovy.syntax.Types#LEFT_SQUARE_BRACKET

The following examples show how to use org.codehaus.groovy.syntax.Types#LEFT_SQUARE_BRACKET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void visitBinaryExpression(BinaryExpression expression) {
    if (expression.getOperation().getType() == Types.LEFT_SQUARE_BRACKET &&
            expression.getRightExpression() instanceof MapEntryExpression) {
        addError("You tried to use a map entry for an index operation, this is not allowed. " +
                "Maybe something should be set in parentheses or a comma is missing?",
                expression.getRightExpression());
    }
    super.visitBinaryExpression(expression);

    if (Types.isAssignment(expression.getOperation().getType())) {
        checkFinalFieldAccess(expression.getLeftExpression());
        checkSuperOrThisOnLHS(expression.getLeftExpression());
    }
}
 
Example 2
Source File: BinaryExpression.java    From groovy with Apache License 2.0 4 votes vote down vote up
public String getText() {
    if (operation.getType() == Types.LEFT_SQUARE_BRACKET) {
        return leftExpression.getText() + (safe ? "?" : "") + "[" + rightExpression.getText() + "]";
    }
    return "(" + leftExpression.getText() + " " + operation.getText() + " " + rightExpression.getText() + ")";
}
 
Example 3
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    if (expression.getNodeMetaData(StatementMeta.class) != null) return;
    super.visitBinaryExpression(expression);

    ClassNode leftType = typeChooser.resolveType(expression.getLeftExpression(), node);
    ClassNode rightType = typeChooser.resolveType(expression.getRightExpression(), node);
    ClassNode resultType = null;
    int operation = expression.getOperation().getType();

    if (operation == Types.LEFT_SQUARE_BRACKET && leftType.isArray()) {
        opt.chainShouldOptimize(true);
        resultType = leftType.getComponentType();
    } else {
        switch (operation) {
            case Types.COMPARE_EQUAL:
            case Types.COMPARE_LESS_THAN:
            case Types.COMPARE_LESS_THAN_EQUAL:
            case Types.COMPARE_GREATER_THAN:
            case Types.COMPARE_GREATER_THAN_EQUAL:
            case Types.COMPARE_NOT_EQUAL:
                if (isIntCategory(leftType) && isIntCategory(rightType)) {
                    opt.chainShouldOptimize(true);
                } else if (isLongCategory(leftType) && isLongCategory(rightType)) {
                    opt.chainShouldOptimize(true);
                } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
                    opt.chainShouldOptimize(true);
                } else {
                    opt.chainCanOptimize(true);
                }
                resultType = boolean_TYPE;
                break;
            case Types.LOGICAL_AND:
            case Types.LOGICAL_AND_EQUAL:
            case Types.LOGICAL_OR:
            case Types.LOGICAL_OR_EQUAL:
                if (boolean_TYPE.equals(leftType) && boolean_TYPE.equals(rightType)) {
                    opt.chainShouldOptimize(true);
                } else {
                    opt.chainCanOptimize(true);
                }
                expression.setType(boolean_TYPE);
                resultType = boolean_TYPE;
                break;
            case Types.DIVIDE:
            case Types.DIVIDE_EQUAL:
                if (isLongCategory(leftType) && isLongCategory(rightType)) {
                    resultType = BigDecimal_TYPE;
                    opt.chainShouldOptimize(true);
                } else if (isBigDecCategory(leftType) && isBigDecCategory(rightType)) {
                    // no optimization for BigDecimal yet
                    //resultType = BigDecimal_TYPE;
                } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
                    resultType = double_TYPE;
                    opt.chainShouldOptimize(true);
                }
                break;
            case Types.POWER:
            case Types.POWER_EQUAL:
                // TODO: implement
                break;
            case Types.ASSIGN:
                resultType = optimizeDivWithIntOrLongTarget(expression.getRightExpression(), leftType);
                opt.chainCanOptimize(true);
                break;
            default:
                if (isIntCategory(leftType) && isIntCategory(rightType)) {
                    resultType = int_TYPE;
                    opt.chainShouldOptimize(true);
                } else if (isLongCategory(leftType) && isLongCategory(rightType)) {
                    resultType = long_TYPE;
                    opt.chainShouldOptimize(true);
                } else if (isBigDecCategory(leftType) && isBigDecCategory(rightType)) {
                    // no optimization for BigDecimal yet
                    //resultType = BigDecimal_TYPE;
                } else if (isDoubleCategory(leftType) && isDoubleCategory(rightType)) {
                    resultType = double_TYPE;
                    opt.chainShouldOptimize(true);
                }
        }
    }

    if (resultType != null) {
        addMeta(expression).type = resultType;
        opt.chainInvolvedType(resultType);
        opt.chainInvolvedType(rightType);
        opt.chainInvolvedType(leftType);
    }
}