-
Notifications
You must be signed in to change notification settings - Fork 69
[operators] feat: Add UnaryOperationExpr and unit test #268
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eba3225
Add UnaryOperationExpr
summer-ji-eng 6c54c6a
use typenode intead if typekind in look up set for numeric type
summer-ji-eng 9468478
fix TypeNode
summer-ji-eng febbb4a
fix typnode in valueExpr
summer-ji-eng 60041fc
remove isBoxedType
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
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
101 changes: 101 additions & 0 deletions
101
src/main/java/com/google/api/generator/engine/ast/UnaryOperationExpr.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,101 @@ | ||
| // 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 UnaryOperationExpr implements OperationExpr { | ||
|
|
||
| public abstract Expr expr(); | ||
|
|
||
| public abstract OperatorKind operatorKind(); | ||
|
|
||
| public abstract TypeNode type(); | ||
|
|
||
| public void accept(AstNodeVisitor visitor) { | ||
| visitor.visit(this); | ||
| } | ||
|
|
||
| public static UnaryOperationExpr postfixIncrementWithExpr(Expr expr) { | ||
| return builder() | ||
| .setExpr(expr) | ||
| .setOperatorKind(OperatorKind.UNARY_POST_INCREMENT) | ||
| .setType(expr.type()) | ||
| .build(); | ||
| } | ||
|
|
||
| public static UnaryOperationExpr logicalNotWithExpr(Expr expr) { | ||
| return builder() | ||
| .setOperatorKind(OperatorKind.UNARY_LOGICAL_NOT) | ||
| .setExpr(expr) | ||
| .setType(TypeNode.BOOLEAN) | ||
| .build(); | ||
| } | ||
|
|
||
| private static Builder builder() { | ||
| return new AutoValue_UnaryOperationExpr.Builder(); | ||
| } | ||
|
|
||
| @AutoValue.Builder | ||
| abstract static class Builder { | ||
|
|
||
| // Private setter. | ||
| abstract Builder setExpr(Expr expr); | ||
|
|
||
| // Private setter. | ||
| abstract Builder setOperatorKind(OperatorKind operator); | ||
|
|
||
| // Private setter. | ||
| abstract Builder setType(TypeNode type); | ||
|
|
||
| abstract UnaryOperationExpr autoBuild(); | ||
|
|
||
| private UnaryOperationExpr build() { | ||
| UnaryOperationExpr unaryOperationExpr = autoBuild(); | ||
| TypeNode exprType = unaryOperationExpr.expr().type(); | ||
| OperatorKind operator = unaryOperationExpr.operatorKind(); | ||
| final String errorMsg = | ||
| String.format( | ||
| "Unary operator %s can not be applied to %s. ", operator, exprType.toString()); | ||
|
|
||
| Preconditions.checkState( | ||
| !exprType.equals(TypeNode.VOID) && !exprType.equals(TypeNode.NULL), errorMsg); | ||
|
|
||
| if (operator.equals(OperatorKind.UNARY_LOGICAL_NOT)) { | ||
| Preconditions.checkState(isValidLogicalNotType(exprType), errorMsg); | ||
| } | ||
|
|
||
| if (operator.equals(OperatorKind.UNARY_POST_INCREMENT)) { | ||
| Preconditions.checkState(isValidIncrementType(exprType), errorMsg); | ||
| } | ||
|
|
||
| return unaryOperationExpr; | ||
| } | ||
| } | ||
|
|
||
| private static boolean isValidLogicalNotType(TypeNode exprType) { | ||
| // Logical not (!) can only be applied on boolean/Boolean type | ||
| return exprType.equals(TypeNode.BOOLEAN); | ||
| } | ||
|
|
||
| private static boolean isValidIncrementType(TypeNode exprType) { | ||
| // Increment (++) can be applied on numeric types(int, double, float, long, short, char) | ||
| // and its boxed type (exclude Boolean) | ||
| return TypeNode.isNumericType(exprType) | ||
| || (TypeNode.isBoxedType(exprType) && !exprType.equals(TypeNode.BOOLEAN_OBJECT)); | ||
| } | ||
| } |
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
95 changes: 95 additions & 0 deletions
95
src/test/java/com/google/api/generator/engine/ast/UnaryOperationExprTest.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,95 @@ | ||
| // 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 static org.junit.Assert.assertThrows; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class UnaryOperationExprTest { | ||
| /** =============================== Logic Not Operation Expr =============================== */ | ||
| @Test | ||
| public void logicalNotOperationExpr_validBasic() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable( | ||
| Variable.builder().setName("x").setType(TypeNode.BOOLEAN).build()); | ||
| UnaryOperationExpr.logicalNotWithExpr(variableExpr); | ||
| // No exception thrown, we're good. | ||
| } | ||
|
|
||
| @Test | ||
| public void logicalNot_validBoxedType() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable( | ||
| Variable.builder().setName("x").setType(TypeNode.BOOLEAN_OBJECT).build()); | ||
| UnaryOperationExpr.logicalNotWithExpr(variableExpr); | ||
| // No exception thrown, we're good. | ||
| } | ||
|
|
||
| @Test | ||
| public void logicalNot_invalidNumericType() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.INT).build()); | ||
| assertThrows( | ||
| IllegalStateException.class, () -> UnaryOperationExpr.logicalNotWithExpr(variableExpr)); | ||
| } | ||
|
|
||
| @Test | ||
| public void logicalNot_invalidReferenceType() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.STRING).build()); | ||
| assertThrows( | ||
| IllegalStateException.class, () -> UnaryOperationExpr.logicalNotWithExpr(variableExpr)); | ||
| } | ||
|
|
||
| /** | ||
| * =============================== Post Increment Operation Expr =============================== | ||
| */ | ||
| @Test | ||
| public void postIncrement_validBasic() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.INT).build()); | ||
| UnaryOperationExpr.postfixIncrementWithExpr(variableExpr); | ||
| // No exception thrown, we're good. | ||
| } | ||
|
|
||
| @Test | ||
| public void postIncrement_validBoxedType() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable( | ||
| Variable.builder().setName("x").setType(TypeNode.FLOAT_OBJECT).build()); | ||
| UnaryOperationExpr.postfixIncrementWithExpr(variableExpr); | ||
| // No exception thrown, we're good. | ||
| } | ||
|
|
||
| @Test | ||
| public void postIncrement_invalidBoxedBooleanType() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable( | ||
| Variable.builder().setName("x").setType(TypeNode.BOOLEAN_OBJECT).build()); | ||
| assertThrows( | ||
| IllegalStateException.class, | ||
| () -> UnaryOperationExpr.postfixIncrementWithExpr(variableExpr)); | ||
| } | ||
|
|
||
| @Test | ||
| public void postIncrement_invalidReferenceType() { | ||
| VariableExpr variableExpr = | ||
| VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.STRING).build()); | ||
| assertThrows( | ||
| IllegalStateException.class, | ||
| () -> UnaryOperationExpr.postfixIncrementWithExpr(variableExpr)); | ||
| } | ||
| } |
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
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.