public void visitBinary(Binary binary) { CodeTree right = stack.pop(); CodeTree left = stack.pop(); stack.push(combine(left, string(" " + binary.getOperator() + " "), right)); }
public void visitBinary(Binary binary) { CodeTree right = stack.pop(); CodeTree left = stack.pop(); stack.push(combine(left, string(" " + binary.getOperator() + " "), right)); }
public void visitBinary(Binary binary) { String operator = binary.getOperator(); TypeMirror leftType = binary.getLeft().getResolvedType(); TypeMirror rightType = binary.getRight().getResolvedType(); if (!ElementUtils.areTypesCompatible(leftType, rightType)) { throw new InvalidExpressionException(String.format("Incompatible operand types %s and %s.", ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } TypeMirror booleanType = context.getType(boolean.class); boolean valid; if (LOGIC_OPERATORS.contains(operator)) { valid = ElementUtils.typeEquals(leftType, booleanType); } else if (COMPARABLE_OPERATORS.contains(operator)) { valid = ElementUtils.isPrimitive(leftType); } else if (IDENTITY_OPERATORS.contains(operator)) { valid = leftType.getKind().isPrimitive() || leftType.getKind() == TypeKind.DECLARED || leftType.getKind() == TypeKind.ARRAY; } else { throw new InvalidExpressionException(String.format("The operator %s is undefined.", operator)); } binary.setResolvedType(booleanType); if (!valid) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type(s) %s %s.", operator, ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } }
public void visitBinary(Binary binary) { String operator = binary.getOperator(); TypeMirror leftType = binary.getLeft().getResolvedType(); TypeMirror rightType = binary.getRight().getResolvedType(); if (!ElementUtils.areTypesCompatible(leftType, rightType)) { throw new InvalidExpressionException(String.format("Incompatible operand types %s and %s.", ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } TypeMirror booleanType = context.getType(boolean.class); boolean valid; if (LOGIC_OPERATORS.contains(operator)) { valid = ElementUtils.typeEquals(leftType, booleanType); } else if (COMPARABLE_OPERATORS.contains(operator)) { valid = ElementUtils.isPrimitive(leftType); } else if (IDENTITY_OPERATORS.contains(operator)) { valid = leftType.getKind().isPrimitive() || leftType.getKind() == TypeKind.DECLARED || leftType.getKind() == TypeKind.ARRAY; } else { throw new InvalidExpressionException(String.format("The operator %s is undefined.", operator)); } binary.setResolvedType(booleanType); if (!valid) { throw new InvalidExpressionException(String.format("The operator %s is undefined for the argument type(s) %s %s.", operator, ElementUtils.getSimpleName(leftType), ElementUtils.getSimpleName(rightType))); } }
@Override public DSLExpression visitBinary(Binary binary) { // signature: value == value can be folded to true if (IDENTITY_FOLD_OPERATORS.contains(binary.getOperator())) { if (binary.getLeft() instanceof Variable && binary.getRight() instanceof Variable) { Variable leftVar = ((Variable) binary.getLeft()); Variable rightVar = ((Variable) binary.getRight()); if (leftVar.equals(rightVar)) { // double and float cannot be folded as NaN is never identity equal if (!ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(float.class)) && !ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(double.class))) { return new BooleanLiteral(true); } } } } return super.visitBinary(binary); } });
@Override public DSLExpression reduce(DSLExpressionReducer reducer) { DSLExpression newLeft = left.reduceImpl(reducer); DSLExpression newRight = right.reduceImpl(reducer); Binary b = this; if (newLeft != left || newRight != right) { b = new Binary(getOperator(), newLeft, newRight); b.setResolvedTargetType(getResolvedTargetType()); b.setResolvedType(getResolvedType()); } return reducer.visitBinary(b); }