From d8dbf61487fd31d4b5b7d0a2c14a9790f0ace6d4 Mon Sep 17 00:00:00 2001 From: summerji Date: Sat, 29 Aug 2020 23:00:51 -0700 Subject: [PATCH 1/5] feat: Add operation expr --- .../generator/engine/ast/OperationExpr.java | 18 +++++++++++ .../generator/engine/ast/OperatorKind.java | 31 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/com/google/api/generator/engine/ast/OperationExpr.java create mode 100644 src/main/java/com/google/api/generator/engine/ast/OperatorKind.java diff --git a/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java new file mode 100644 index 0000000000..d5540cc229 --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java @@ -0,0 +1,18 @@ +// 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; + +public interface OperationExpr extends Expr { + + OperatorKind operatorKind(); +} diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java new file mode 100644 index 0000000000..c4a9dd7736 --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -0,0 +1,31 @@ +// 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; + +public enum OperatorKind { + ARITHMETIC_ADDITION(true), + UNARY_POST_INCREMENT(true), + LOGICAL_NOT(false), + RELATIONAL_EQUAL_TO(true), + RELATIONAL_NOT_EQUAL_TO(true); + + private final boolean isPostfixOperator; + + OperatorKind(final boolean isPostfixOperator) { + this.isPostfixOperator = isPostfixOperator; + } + + public boolean isPostfixOperator() { + return this.isPostfixOperator; + } +} From 477ffd1a2dea63a085c98d91859bb5f80b3c0662 Mon Sep 17 00:00:00 2001 From: summerji Date: Sat, 29 Aug 2020 23:10:13 -0700 Subject: [PATCH 2/5] add javawritervistior for operator string --- .../generator/engine/ast/OperationExpr.java | 2 ++ .../generator/engine/ast/OperatorKind.java | 8 ++++-- .../engine/writer/JavaWriterVisitor.java | 28 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java index d5540cc229..06e214561a 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java @@ -1,3 +1,5 @@ +// 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 diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java index c4a9dd7736..0a2887f08b 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -1,3 +1,5 @@ +// 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 @@ -14,10 +16,10 @@ public enum OperatorKind { ARITHMETIC_ADDITION(true), - UNARY_POST_INCREMENT(true), - LOGICAL_NOT(false), RELATIONAL_EQUAL_TO(true), - RELATIONAL_NOT_EQUAL_TO(true); + RELATIONAL_NOT_EQUAL_TO(true), + UNARY_LOGICAL_NOT(false), + UNARY_POST_INCREMENT(true); private final boolean isPostfixOperator; diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index b246e7234f..5fd3bc6f9d 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -35,6 +35,7 @@ import com.google.api.generator.engine.ast.MethodDefinition; import com.google.api.generator.engine.ast.MethodInvocationExpr; import com.google.api.generator.engine.ast.NewObjectExpr; +import com.google.api.generator.engine.ast.OperatorKind; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; import com.google.api.generator.engine.ast.ReturnExpr; import com.google.api.generator.engine.ast.ScopeNode; @@ -100,6 +101,13 @@ public class JavaWriterVisitor implements AstNodeVisitor { private static final String VOLATILE = "volatile"; private static final String WHILE = "while"; + // Operators + private static final String ADDITION = "+"; + private static final String EQUAL_TO = "=="; + private static final String NOT_EQUAL_TO = "!="; + private static final String INCREMENT = "++"; + private static final String LOGICAL_NOT = "!"; + private final StringBuffer buffer = new StringBuffer(); private final ImportWriterVisitor importWriterVisitor = new ImportWriterVisitor(); @@ -822,4 +830,24 @@ private void rightBrace() { private void semicolon() { buffer.append(SEMICOLON); } + + private void operator(OperatorKind kind) { + switch (kind) { + case RELATIONAL_EQUAL_TO: + buffer.append(EQUAL_TO); + break; + case RELATIONAL_NOT_EQUAL_TO: + buffer.append(NOT_EQUAL_TO); + break; + case UNARY_POST_INCREMENT: + buffer.append(INCREMENT); + break; + case UNARY_LOGICAL_NOT: + buffer.append(LOGICAL_NOT); + break; + case ARITHMETIC_ADDITION: + buffer.append(ADDITION); + break; + } + } } From 48a2448649a964cd9976163bb5735ded8e657a3b Mon Sep 17 00:00:00 2001 From: summerji Date: Mon, 31 Aug 2020 23:44:31 -0700 Subject: [PATCH 3/5] Add lookup in enum operatorkind --- .../generator/engine/ast/OperationExpr.java | 4 ++++ .../generator/engine/ast/OperatorKind.java | 22 ++++++++++--------- .../engine/writer/JavaWriterVisitor.java | 20 ++++++++--------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java index 06e214561a..924a6a6930 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java @@ -17,4 +17,8 @@ public interface OperationExpr extends Expr { OperatorKind operatorKind(); + + TypeNode type(); + + void accept(AstNodeVisitor visitor); } diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java index 0a2887f08b..af367b8c17 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -14,20 +14,22 @@ package com.google.api.generator.engine.ast; +import java.util.EnumSet; + public enum OperatorKind { - ARITHMETIC_ADDITION(true), - RELATIONAL_EQUAL_TO(true), - RELATIONAL_NOT_EQUAL_TO(true), - UNARY_LOGICAL_NOT(false), - UNARY_POST_INCREMENT(true); + ARITHMETIC_ADDITION, + UNARY_POST_INCREMENT, + LOGICAL_NOT, + RELATIONAL_EQUAL_TO, + RELATIONAL_NOT_EQUAL_TO; - private final boolean isPostfixOperator; + private static final EnumSet PREFIX_OPERATORS_SET; - OperatorKind(final boolean isPostfixOperator) { - this.isPostfixOperator = isPostfixOperator; + static { + PREFIX_OPERATORS_SET = EnumSet.of(OperatorKind.LOGICAL_NOT); } - public boolean isPostfixOperator() { - return this.isPostfixOperator; + public boolean isPrefixOperator() { + return PREFIX_OPERATORS_SET.contains(this); } } diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 5fd3bc6f9d..f8aed7379f 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -102,11 +102,11 @@ public class JavaWriterVisitor implements AstNodeVisitor { private static final String WHILE = "while"; // Operators - private static final String ADDITION = "+"; - private static final String EQUAL_TO = "=="; - private static final String NOT_EQUAL_TO = "!="; - private static final String INCREMENT = "++"; - private static final String LOGICAL_NOT = "!"; + private static final String OPERATOR_ADDITION = "+"; + private static final String OPERATOR_EQUAL_TO = "=="; + private static final String OPERATOR_NOT_EQUAL_TO = "!="; + private static final String OPERATOR_INCREMENT = "++"; + private static final String OPERATOR_LOGICAL_NOT = "!"; private final StringBuffer buffer = new StringBuffer(); private final ImportWriterVisitor importWriterVisitor = new ImportWriterVisitor(); @@ -834,19 +834,19 @@ private void semicolon() { private void operator(OperatorKind kind) { switch (kind) { case RELATIONAL_EQUAL_TO: - buffer.append(EQUAL_TO); + buffer.append(OPERATOR_EQUAL_TO); break; case RELATIONAL_NOT_EQUAL_TO: - buffer.append(NOT_EQUAL_TO); + buffer.append(OPERATOR_NOT_EQUAL_TO); break; case UNARY_POST_INCREMENT: - buffer.append(INCREMENT); + buffer.append(OPERATOR_INCREMENT); break; case UNARY_LOGICAL_NOT: - buffer.append(LOGICAL_NOT); + buffer.append(OPERATOR_LOGICAL_NOT); break; case ARITHMETIC_ADDITION: - buffer.append(ADDITION); + buffer.append(OPERATOR_ADDITION); break; } } From 00b866ccca9b554f23c59ed1897c05b5ab79330e Mon Sep 17 00:00:00 2001 From: summerji Date: Mon, 31 Aug 2020 23:51:06 -0700 Subject: [PATCH 4/5] fix Operator Name --- .../com/google/api/generator/engine/ast/OperatorKind.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java index af367b8c17..e1d69e7927 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -18,15 +18,15 @@ public enum OperatorKind { ARITHMETIC_ADDITION, - UNARY_POST_INCREMENT, - LOGICAL_NOT, RELATIONAL_EQUAL_TO, - RELATIONAL_NOT_EQUAL_TO; + RELATIONAL_NOT_EQUAL_TO, + UNARY_LOGICAL_NOT, + UNARY_POST_INCREMENT; private static final EnumSet PREFIX_OPERATORS_SET; static { - PREFIX_OPERATORS_SET = EnumSet.of(OperatorKind.LOGICAL_NOT); + PREFIX_OPERATORS_SET = EnumSet.of(OperatorKind.UNARY_LOGICAL_NOT); } public boolean isPrefixOperator() { From b117a86471bf4f393c1b9a5d9853b9f790a806ae Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 1 Sep 2020 10:35:26 -0700 Subject: [PATCH 5/5] remove static block --- .../com/google/api/generator/engine/ast/OperatorKind.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java index e1d69e7927..8805695ed8 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -23,11 +23,8 @@ public enum OperatorKind { UNARY_LOGICAL_NOT, UNARY_POST_INCREMENT; - private static final EnumSet PREFIX_OPERATORS_SET; - - static { - PREFIX_OPERATORS_SET = EnumSet.of(OperatorKind.UNARY_LOGICAL_NOT); - } + private static final EnumSet PREFIX_OPERATORS_SET = + EnumSet.of(OperatorKind.UNARY_LOGICAL_NOT); public boolean isPrefixOperator() { return PREFIX_OPERATORS_SET.contains(this);