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,25 @@ 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 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);
}
// LHS is non-floating-point numeric types, RHS should be non-float-point numeric types or
// their boxed types.
return TypeNode.isNumericType(variableType)
&& TypeNode.isNumericType(valueType)
&& !TypeNode.isFloatingPointType(variableType)
&& !TypeNode.isFloatingPointType(valueType)
&& !TypeNode.isBoxedType(variableType);
}
}
}
Loading