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..924a6a6930 --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/OperationExpr.java @@ -0,0 +1,24 @@ +// 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; + +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 new file mode 100644 index 0000000000..8805695ed8 --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -0,0 +1,32 @@ +// 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 java.util.EnumSet; + +public enum OperatorKind { + ARITHMETIC_ADDITION, + RELATIONAL_EQUAL_TO, + RELATIONAL_NOT_EQUAL_TO, + UNARY_LOGICAL_NOT, + UNARY_POST_INCREMENT; + + private static final EnumSet PREFIX_OPERATORS_SET = + EnumSet.of(OperatorKind.UNARY_LOGICAL_NOT); + + 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 b246e7234f..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 @@ -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 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(); @@ -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(OPERATOR_EQUAL_TO); + break; + case RELATIONAL_NOT_EQUAL_TO: + buffer.append(OPERATOR_NOT_EQUAL_TO); + break; + case UNARY_POST_INCREMENT: + buffer.append(OPERATOR_INCREMENT); + break; + case UNARY_LOGICAL_NOT: + buffer.append(OPERATOR_LOGICAL_NOT); + break; + case ARITHMETIC_ADDITION: + buffer.append(OPERATOR_ADDITION); + break; + } + } }