Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit e9de699

Browse files
[javanaut]feat: Add operation expr interface and operator kind enum (#255)
1 parent 901833f commit e9de699

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.engine.ast;
16+
17+
public interface OperationExpr extends Expr {
18+
19+
OperatorKind operatorKind();
20+
21+
TypeNode type();
22+
23+
void accept(AstNodeVisitor visitor);
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.engine.ast;
16+
17+
import java.util.EnumSet;
18+
19+
public enum OperatorKind {
20+
ARITHMETIC_ADDITION,
21+
RELATIONAL_EQUAL_TO,
22+
RELATIONAL_NOT_EQUAL_TO,
23+
UNARY_LOGICAL_NOT,
24+
UNARY_POST_INCREMENT;
25+
26+
private static final EnumSet<OperatorKind> PREFIX_OPERATORS_SET =
27+
EnumSet.of(OperatorKind.UNARY_LOGICAL_NOT);
28+
29+
public boolean isPrefixOperator() {
30+
return PREFIX_OPERATORS_SET.contains(this);
31+
}
32+
}

src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.api.generator.engine.ast.MethodDefinition;
3636
import com.google.api.generator.engine.ast.MethodInvocationExpr;
3737
import com.google.api.generator.engine.ast.NewObjectExpr;
38+
import com.google.api.generator.engine.ast.OperatorKind;
3839
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
3940
import com.google.api.generator.engine.ast.ReturnExpr;
4041
import com.google.api.generator.engine.ast.ScopeNode;
@@ -100,6 +101,13 @@ public class JavaWriterVisitor implements AstNodeVisitor {
100101
private static final String VOLATILE = "volatile";
101102
private static final String WHILE = "while";
102103

104+
// Operators
105+
private static final String OPERATOR_ADDITION = "+";
106+
private static final String OPERATOR_EQUAL_TO = "==";
107+
private static final String OPERATOR_NOT_EQUAL_TO = "!=";
108+
private static final String OPERATOR_INCREMENT = "++";
109+
private static final String OPERATOR_LOGICAL_NOT = "!";
110+
103111
private final StringBuffer buffer = new StringBuffer();
104112
private final ImportWriterVisitor importWriterVisitor = new ImportWriterVisitor();
105113

@@ -822,4 +830,24 @@ private void rightBrace() {
822830
private void semicolon() {
823831
buffer.append(SEMICOLON);
824832
}
833+
834+
private void operator(OperatorKind kind) {
835+
switch (kind) {
836+
case RELATIONAL_EQUAL_TO:
837+
buffer.append(OPERATOR_EQUAL_TO);
838+
break;
839+
case RELATIONAL_NOT_EQUAL_TO:
840+
buffer.append(OPERATOR_NOT_EQUAL_TO);
841+
break;
842+
case UNARY_POST_INCREMENT:
843+
buffer.append(OPERATOR_INCREMENT);
844+
break;
845+
case UNARY_LOGICAL_NOT:
846+
buffer.append(OPERATOR_LOGICAL_NOT);
847+
break;
848+
case ARITHMETIC_ADDITION:
849+
buffer.append(OPERATOR_ADDITION);
850+
break;
851+
}
852+
}
825853
}

0 commit comments

Comments
 (0)