Skip to content
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

[javanaut][operationExpr][1/2]feat: Add Assignment operation expr with multiply and assignment operator #308

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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,122 @@
// 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 AssignmentOperationExpr implements OperationExpr {
public abstract Expr lhsExpr();

public abstract Expr rhsExpr();

public abstract OperatorKind operatorKind();

public abstract TypeNode type();

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}

public static AssignmentOperationExpr bitwiseExclusiveOrAndAssignmentWithExprs(
Expr lhsExpr, Expr rhsExpr) {
return builder()
.setLhsExpr(lhsExpr)
.setRhsExpr(rhsExpr)
.setOperatorKind(OperatorKind.ASSIGNMENT_BITWISE_EXCLUSIVE_OR_AND_ASSIGNMENT)
.setType(lhsExpr.type())
.build();
}

public static AssignmentOperationExpr multiplyAndAssignmentWithExprs(Expr lhsExpr, Expr rhsExpr) {
return builder()
.setLhsExpr(lhsExpr)
.setRhsExpr(rhsExpr)
.setOperatorKind(OperatorKind.ASSIGNMENT_MULTIPLY_AND_ASSIGNMENT)
.setType(lhsExpr.type())
.build();
}

private static Builder builder() {
return new AutoValue_AssignmentOperationExpr.Builder();
}

@AutoValue.Builder
abstract static class Builder {
// Private setter.
abstract Builder setLhsExpr(Expr expr);

// Private setter.
abstract Builder setRhsExpr(Expr expr);

// Private setter.
abstract Builder setOperatorKind(OperatorKind operator);

// Private setter.
abstract Builder setType(TypeNode type);

abstract AssignmentOperationExpr autoBuild();

private AssignmentOperationExpr build() {
AssignmentOperationExpr assignmentOperationExpr = autoBuild();
TypeNode lhsExprType = assignmentOperationExpr.lhsExpr().type();
TypeNode rhsExprType = assignmentOperationExpr.rhsExpr().type();
OperatorKind operator = assignmentOperationExpr.operatorKind();
final String errorMsg =
String.format(
"Assignment operator %s can not be applied to %s, %s.",
operator, lhsExprType.toString(), rhsExprType.toString());
if (operator.equals(OperatorKind.ASSIGNMENT_MULTIPLY_AND_ASSIGNMENT)) {
Preconditions.checkState(
isValidMultiplyAndAssignmentType(lhsExprType, rhsExprType), errorMsg);
}
if (operator.equals(OperatorKind.ASSIGNMENT_BITWISE_EXCLUSIVE_OR_AND_ASSIGNMENT)) {
Preconditions.checkState(
isValidBitwiseExclusiveOrAndAssignmentType(lhsExprType, rhsExprType), errorMsg);
}
return assignmentOperationExpr;
}

private boolean isValidMultiplyAndAssignmentType(TypeNode lhsType, TypeNode rhsType) {
if (TypeNode.isNumericType(lhsType) && !TypeNode.isBoxedType(lhsType)) {
return TypeNode.isNumericType(rhsType);
}
if (lhsType.equals(TypeNode.INT_OBJECT)) {
return TypeNode.isNumericType(rhsType)
&& !(rhsType.equals(TypeNode.LONG)
|| rhsType.equals(TypeNode.FLOAT)
|| rhsType.equals(TypeNode.DOUBLE));
}
if (lhsType.equals(TypeNode.LONG_OBJECT)) {
return TypeNode.isNumericType(rhsType)
&& !(rhsType.equals(TypeNode.FLOAT) || rhsType.equals(TypeNode.DOUBLE));
}
if (lhsType.equals(TypeNode.FLOAT_OBJECT)) {
return TypeNode.isNumericType(rhsType) && !rhsType.equals(TypeNode.DOUBLE);
}
if (lhsType.equals(TypeNode.DOUBLE_OBJECT)) {
return TypeNode.isNumericType(rhsType);
}
return false;
}

// TODO(summerji): Complete the type-checking for ^= and unit test.
private boolean isValidBitwiseExclusiveOrAndAssignmentType(TypeNode lhsType, TypeNode rhsType) {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface AstNodeVisitor {

public void visit(LogicalOperationExpr logicalOperationExpr);

public void visit(AssignmentOperationExpr assignmentOperationExpr);

/** =============================== COMMENT =============================== */
public void visit(LineComment lineComment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public enum OperatorKind {
ARITHMETIC_ADDITION,
LOGICAL_AND,
LOGICAL_OR,
ASSIGNMENT_BITWISE_EXCLUSIVE_OR_AND_ASSIGNMENT,
ASSIGNMENT_MULTIPLY_AND_ASSIGNMENT,
RELATIONAL_EQUAL_TO,
RELATIONAL_NOT_EQUAL_TO,
UNARY_LOGICAL_NOT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ public static boolean isNumericType(TypeNode type) {
|| type.equals(TypeNode.DOUBLE)
|| type.equals(TypeNode.SHORT)
|| type.equals(TypeNode.FLOAT)
|| type.equals(TypeNode.CHAR);
|| type.equals(TypeNode.CHAR)
|| type.equals(TypeNode.BYTE);
}

public static boolean isBoxedType(TypeNode type) {
return isReferenceType(type) && BOXED_TYPE_MAP.containsValue(type);
}

public boolean isPrimitiveType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.AssignmentOperationExpr;
import com.google.api.generator.engine.ast.AstNodeVisitor;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
Expand Down Expand Up @@ -233,6 +234,12 @@ public void visit(LogicalOperationExpr logicalOperationExpr) {
logicalOperationExpr.rhsExpr().accept(this);
}

@Override
public void visit(AssignmentOperationExpr assignmentOperationExpr) {
assignmentOperationExpr.lhsExpr().accept(this);
assignmentOperationExpr.rhsExpr().accept(this);
}

/** =============================== STATEMENTS =============================== */
@Override
public void visit(ExprStatement exprStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.engine.ast.AnonymousClassExpr;
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.AssignmentOperationExpr;
import com.google.api.generator.engine.ast.AstNodeVisitor;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
Expand Down Expand Up @@ -113,6 +114,8 @@ public class JavaWriterVisitor implements AstNodeVisitor {
private static final String OPERATOR_LOGICAL_NOT = "!";
private static final String OPERATOR_LOGICAL_AND = "&&";
private static final String OPERATOR_LOGICAL_OR = "||";
private static final String OPERATOR_BITWISE_EXCLUSIVE_OR_AND_ASSIGNMENT = "^=";
private static final String OPERATOR_MULTIPLE_AND_ASSIGNMENT = "*=";

private final StringBuffer buffer = new StringBuffer();
private final ImportWriterVisitor importWriterVisitor = new ImportWriterVisitor();
Expand Down Expand Up @@ -419,6 +422,15 @@ public void visit(LogicalOperationExpr logicalOperationExpr) {
logicalOperationExpr.rhsExpr().accept(this);
}

@Override
public void visit(AssignmentOperationExpr assignmentOperationExpr) {
assignmentOperationExpr.lhsExpr().accept(this);
space();
operator(assignmentOperationExpr.operatorKind());
space();
assignmentOperationExpr.rhsExpr().accept(this);
}

/** =============================== STATEMENTS =============================== */
@Override
public void visit(ExprStatement exprStatement) {
Expand Down Expand Up @@ -902,6 +914,15 @@ private void semicolon() {

private void operator(OperatorKind kind) {
switch (kind) {
case ARITHMETIC_ADDITION:
buffer.append(OPERATOR_ADDITION);
break;
case ASSIGNMENT_BITWISE_EXCLUSIVE_OR_AND_ASSIGNMENT:
buffer.append(OPERATOR_BITWISE_EXCLUSIVE_OR_AND_ASSIGNMENT);
break;
case ASSIGNMENT_MULTIPLY_AND_ASSIGNMENT:
buffer.append(OPERATOR_MULTIPLE_AND_ASSIGNMENT);
break;
case RELATIONAL_EQUAL_TO:
buffer.append(OPERATOR_EQUAL_TO);
break;
Expand All @@ -914,9 +935,6 @@ private void operator(OperatorKind kind) {
case UNARY_LOGICAL_NOT:
buffer.append(OPERATOR_LOGICAL_NOT);
break;
case ARITHMETIC_ADDITION:
buffer.append(OPERATOR_ADDITION);
break;
case LOGICAL_AND:
buffer.append(OPERATOR_LOGICAL_AND);
break;
Expand Down
Loading