Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add type() here as well? Although this is technically fine, adding type() here will help us see all the methods that need to be implemented in subclasses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since our purpose is to make all methods need to be implemented visible, I also added accept. LMK if you have other opinion. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for doing that.


TypeNode type();

void accept(AstNodeVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -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<OperatorKind> PREFIX_OPERATORS_SET =
EnumSet.of(OperatorKind.UNARY_LOGICAL_NOT);

public boolean isPrefixOperator() {
return PREFIX_OPERATORS_SET.contains(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
}
}