Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[operators][2/2]feat: Add Assignment operation expr with xor assignemnt #342

Merged
merged 9 commits into from
Sep 28, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ private AssignmentOperationExpr build() {
}

// Check type for XOR and assignment operator (^=).
// TODO(summerji): Complete the type-checking for ^= and unit test.
if (operator.equals(OperatorKind.ASSIGNMENT_XOR)) {
Preconditions.checkState(isValidXORAssignmentType(lhsType, rhsType), errorMsg);
Preconditions.checkState(isValidXorAssignmentType(lhsType, rhsType), errorMsg);
}
return assignmentOperationExpr;
}
Expand Down Expand Up @@ -130,9 +129,26 @@ private boolean isValidMultiplyAssignmentType(TypeNode variableType, TypeNode va
return false;
}

// TODO(summerji): Complete the type-checking for ^= and unit test.
private boolean isValidXORAssignmentType(TypeNode variableType, TypeNode valueType) {
return true;
// isValidXorAssignmentType validates the types for LHS variable expr and RHS expr.
// ^= can be applied on boolean and non-floating-point numeric type.
private boolean isValidXorAssignmentType(TypeNode variableType, TypeNode valueType) {
// LHS is boolean or its boxed type, RHS should be boolean or its boxed type.
if (variableType.equals(TypeNode.BOOLEAN)) {
return valueType.equals(variableType);
}
// LHS is non-floating-point numeric types, RHS should be non-float-point numeric types or
// their boxed types.
if (TypeNode.isNumericType(variableType)
&& !TypeNode.isBoxedType(variableType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking up-front and returning false first if:

  • One of variableType or valueType is not a numeric type.
  • valueType is boxed or is a floating-point type.
  • valueType is a floating-point type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do like

    private boolean isValidXorAssignmentType(TypeNode variableType, TypeNode valueType) {

      // LHS is boolean or its boxed type, RHS should be boolean or its boxed type.
      if (variableType.equals(TypeNode.BOOLEAN)) {
        return valueType.equals(variableType);
      }
      // LHS is integer boxed type, RHS should be non-floating-point numeric types or their boxed
      // types.
      if (variableType.equals(TypeNode.INT)) {
        return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType);
      }

      if (!TypeNode.isNumericType(variableType) || !TypeNode.isNumericType(valueType)) {
        return false;
      }
      if (TypeNode.isFloatingPointType(variableType) || TypeNode.isFloatingPointType(valueType)) {
        return false;
      }
      if (TypeNode.isBoxedType(variableType)) {
        return false;
      }
      return true;
    }

But I don't see the point of checking up-front. Could you explain more the benefit to do so?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When rearranged this way, we can now simplify it as follows:

return TypeNode.isNumericType(variableType) && TypeNode.isNumericType(valueType)
    && !TypeNode.isFloatingPointType(variableType) && !TypeNode.isFloatingPointType(valueType)
    && !TypeNode.isBoxedType(variableType);

The original benefit was to make the true-checks easier to read, but this rearrangement works too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, LMK what you think on new version.

&& !TypeNode.isFloatingPointType(variableType)) {
return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType);
}
// LHS is integer boxed type, RHS should be non-floating-point numeric types or their boxed
// types.
if (variableType.equals(TypeNode.INT)) {
return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType);
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public abstract class Variable {
public abstract IdentifierNode identifier();

public abstract TypeNode type();
public abstract com.google.api.generator.engine.ast.TypeNode type();

abstract String name();

Expand Down
Loading