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

[ggj][ast] feat: enable postfix operator for ExprStatement #282

Merged
merged 4 commits into from
Sep 14, 2020
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
Expand Up @@ -53,9 +53,11 @@ public ExprStatement build() {
|| (expr instanceof ReferenceConstructorExpr)
|| (expr instanceof AssignmentExpr)
|| (expr instanceof ThrowExpr)
|| (expr instanceof ReturnExpr),
|| (expr instanceof ReturnExpr)
|| (expr instanceof UnaryOperationExpr
&& ((UnaryOperationExpr) expr).isPostfixIncrement()),
Copy link
Contributor

Choose a reason for hiding this comment

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

curious why unary operation expr has to be post-fix?

Copy link
Contributor Author

@miraleung miraleung Sep 11, 2020

Choose a reason for hiding this comment

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

i++; is a valid standalone statement, but the second one in boolean x = true; !x; is not.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for explainning.

"Expression statements must be either a method invocation, assignment, throw, "
+ "this/super constructor, or return expression");
+ "this/super constructor, return, or unary post-fix operation expression");
}
return exprStatement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}

public boolean isPostfixIncrement() {
return operatorKind().equals(OperatorKind.UNARY_POST_INCREMENT);
}

public static UnaryOperationExpr postfixIncrementWithExpr(Expr expr) {
return builder()
.setExpr(expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public void validExprStatement_return() {
ReturnExpr.withExpr(ValueExpr.withValue(StringObjectValue.withValue("asdf"))));
}

@Test
public void validExprStatement_unaryOperation() {
assertValidExprStatement(
UnaryOperationExpr.postfixIncrementWithExpr(
VariableExpr.withVariable(
Variable.builder().setType(TypeNode.INT).setName("i").build())));
}

@Test
public void invalidExprStatement_variable() {
Variable variable = Variable.builder().setType(TypeNode.INT).setName("libraryClient").build();
Expand All @@ -85,6 +93,15 @@ public void invalidExprStatement_value() {
assertInvalidExprStatement(valueExpr);
}

@Test
public void invalidExprStatement_logicalNotUnaryOperator() {
Expr logicalNotExpr =
UnaryOperationExpr.logicalNotWithExpr(
VariableExpr.withVariable(
Variable.builder().setType(TypeNode.BOOLEAN).setName("foo").build()));
assertInvalidExprStatement(logicalNotExpr);
}

private static void assertInvalidExprStatement(Expr expr) {
assertThrows(
IllegalStateException.class,
Expand Down