-
Notifications
You must be signed in to change notification settings - Fork 69
[operators][1/2]feat: Add Assignment operation expr with multiply and assignment operator #320
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
24fbc24
add assignment operation expr, todo validate for the type
summer-ji-eng f3812cd
add initial unit test
summer-ji-eng 6e24221
Add more unit test
summer-ji-eng 8334fd3
add type-checking for *=
summer-ji-eng c24a9d6
Complete for *= tests
summer-ji-eng 2245040
add license
summer-ji-eng 2df2c8d
add license
summer-ji-eng 7f35ac4
fix linter
summer-ji-eng 88c1062
add check for variable declaration
summer-ji-eng 2083c82
add type-checking for *=
summer-ji-eng ea2ce3e
add license
summer-ji-eng f9a2044
fix linter
summer-ji-eng b0bc2fd
Merge branch 'master' into assignment_operation_expr
summer-ji-eng d857d38
import package'
summer-ji-eng 08e8a44
rename unit tests
summer-ji-eng e424b0d
consistent with INT
summer-ji-eng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/main/java/com/google/api/generator/engine/ast/AssignmentOperationExpr.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.google.api.generator.engine.ast; | ||
|
|
||
| import com.google.auto.value.AutoValue; | ||
| import com.google.common.base.Preconditions; | ||
|
|
||
| @AutoValue | ||
| public abstract class AssignmentOperationExpr implements OperationExpr { | ||
| public abstract VariableExpr variableExpr(); | ||
|
|
||
| public abstract Expr valueExpr(); | ||
|
|
||
| public abstract OperatorKind operatorKind(); | ||
|
|
||
| @Override | ||
| public TypeNode type() { | ||
| return variableExpr().type(); | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(AstNodeVisitor visitor) { | ||
| visitor.visit(this); | ||
| } | ||
|
|
||
| public static AssignmentOperationExpr xorAssignmentWithExprs( | ||
| VariableExpr variableExpr, Expr valueExpr) { | ||
| return builder() | ||
| .setVariableExpr(variableExpr) | ||
| .setValueExpr(valueExpr) | ||
| .setOperatorKind(OperatorKind.ASSIGNMENT_XOR) | ||
| .build(); | ||
| } | ||
|
|
||
| public static AssignmentOperationExpr multiplyAssignmentWithExprs( | ||
| VariableExpr variableExpr, Expr valueExpr) { | ||
| return builder() | ||
| .setVariableExpr(variableExpr) | ||
| .setValueExpr(valueExpr) | ||
| .setOperatorKind(OperatorKind.ASSIGNMENT_MULTIPLY) | ||
| .build(); | ||
| } | ||
|
|
||
| private static Builder builder() { | ||
| return new AutoValue_AssignmentOperationExpr.Builder(); | ||
| } | ||
|
|
||
| @AutoValue.Builder | ||
| abstract static class Builder { | ||
| // Private setter. | ||
| abstract Builder setVariableExpr(VariableExpr variableExpr); | ||
|
|
||
| // Private setter. | ||
| abstract Builder setValueExpr(Expr valueExpr); | ||
|
|
||
| // Private setter. | ||
| abstract Builder setOperatorKind(OperatorKind operator); | ||
|
|
||
| abstract AssignmentOperationExpr autoBuild(); | ||
|
|
||
| private AssignmentOperationExpr build() { | ||
| AssignmentOperationExpr assignmentOperationExpr = autoBuild(); | ||
| TypeNode lhsType = assignmentOperationExpr.variableExpr().variable().type(); | ||
| TypeNode rhsType = assignmentOperationExpr.valueExpr().type(); | ||
| OperatorKind operator = assignmentOperationExpr.operatorKind(); | ||
|
|
||
| // Check if the variable exprs have been declared, if yes, throw error. | ||
| Preconditions.checkState( | ||
| !assignmentOperationExpr.variableExpr().isDecl(), | ||
| String.format( | ||
| "Variable `%s` should not be declaration in the variable expression.", | ||
| assignmentOperationExpr.variableExpr().variable().name())); | ||
|
|
||
| // errorMsg is type checking error message for operators. | ||
| final String errorMsg = | ||
| String.format( | ||
| "Assignment operator %s can not be applied to %s, %s.", | ||
| operator, lhsType.toString(), rhsType.toString()); | ||
|
|
||
| // Check type for multiply and assignment operator (*=). | ||
| if (operator.equals(OperatorKind.ASSIGNMENT_MULTIPLY)) { | ||
| Preconditions.checkState(isValidMultiplyAssignmentType(lhsType, rhsType), errorMsg); | ||
| } | ||
|
|
||
| // 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); | ||
| } | ||
| return assignmentOperationExpr; | ||
| } | ||
|
|
||
| // isValidMultiplyAssignmentType validates the types for LHS variable expr and RHS expr. | ||
| // *= can be only applied on Primitive numeric type. | ||
| private boolean isValidMultiplyAssignmentType(TypeNode variableType, TypeNode valueType) { | ||
| // LHS is numeric type, RHS should be any numeric type or any numeric boxed type. | ||
| if (TypeNode.isNumericType(variableType) && !TypeNode.isBoxedType(variableType)) { | ||
| return TypeNode.isNumericType(valueType); | ||
| } | ||
| // LHS is integer boxed type, RHS should be any numeric type except long, float, double. | ||
| if (variableType.equals(TypeNode.INT_OBJECT)) { | ||
summer-ji-eng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return TypeNode.isNumericType(valueType) | ||
| && !(valueType.equals(TypeNode.LONG) || TypeNode.isFloatingPointType(valueType)); | ||
| } | ||
| // LHS is long boxed type, RHS should be any numeric type except float, double. | ||
| if (variableType.equals(TypeNode.LONG_OBJECT)) { | ||
| return TypeNode.isNumericType(valueType) && !TypeNode.isFloatingPointType(valueType); | ||
| } | ||
| // LHS is integer boxed type, RHS should be any numeric type except double. | ||
| if (variableType.equals(TypeNode.FLOAT_OBJECT)) { | ||
| return TypeNode.isNumericType(valueType) && !valueType.equals(TypeNode.DOUBLE); | ||
| } | ||
| // LHS is integer boxed type, RHS should be any numeric type or any numeric boxed type. | ||
| if (variableType.equals(TypeNode.DOUBLE_OBJECT)) { | ||
| return TypeNode.isNumericType(valueType); | ||
| } | ||
| // *= operator does not support boxed Short, Character, Byte, null, reference, void type. | ||
| return false; | ||
| } | ||
|
|
||
| // TODO(summerji): Complete the type-checking for ^= and unit test. | ||
| private boolean isValidXORAssignmentType(TypeNode variableType, TypeNode valueType) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.