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

feat: add AST support for lambdas [ggj] #736

Merged
merged 2 commits into from
May 26, 2021
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 @@ -65,6 +65,8 @@ public interface AstNodeVisitor {

public void visit(AssignmentOperationExpr assignmentOperationExpr);

public void visit(LambdaExpr lambdaExpr);

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

Expand Down
115 changes: 115 additions & 0 deletions src/main/java/com/google/api/generator/engine/ast/LambdaExpr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2021 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;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@AutoValue
public abstract class LambdaExpr implements Expr {
@Override
public TypeNode type() {
// TODO(v2): Support set of FunctionalInterface parameterized on the args and return type,
// which would enable assignment to an appropriate variable.
return TypeNode.VOID;
}

public abstract ImmutableList<VariableExpr> arguments();

public abstract ReturnExpr returnExpr();

public abstract ImmutableList<Statement> body();

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

public static Builder builder() {
return new AutoValue_LambdaExpr.Builder()
.setArguments(Collections.emptyList())
.setBody(Collections.emptyList());
}

@AutoValue.Builder
public abstract static class Builder {
public Builder setArguments(VariableExpr... arguments) {
return setArguments(Arrays.asList(arguments));
}

public abstract Builder setArguments(List<VariableExpr> arguments);

public abstract Builder setBody(List<Statement> body);

public abstract Builder setReturnExpr(ReturnExpr returnExpr);

public Builder setReturnExpr(Expr expr) {
return setReturnExpr(ReturnExpr.builder().setExpr(expr).build());
}

public abstract LambdaExpr autoBuild();

public LambdaExpr build() {
LambdaExpr lambdaExpr = autoBuild();
Preconditions.checkState(
!lambdaExpr.returnExpr().expr().type().equals(TypeNode.VOID),
"Lambdas cannot return void-typed expressions.");
// Must be a declaration.
lambdaExpr.arguments().stream()
.forEach(
varExpr ->
Preconditions.checkState(
varExpr.isDecl(),
String.format(
"Argument %s must be a variable declaration",
varExpr.variable().identifier())));
// No modifiers allowed.
lambdaExpr.arguments().stream()
.forEach(
varExpr ->
Preconditions.checkState(
varExpr.scope().equals(ScopeNode.LOCAL)
&& !varExpr.isStatic()
&& !varExpr.isFinal()
&& !varExpr.isVolatile(),
String.format(
"Argument %s must have local scope, and cannot have static, final, or"
+ " volatile modifiers",
varExpr.variable().identifier())));

// Check that there aren't any arguments with duplicate names.
List<String> allArgNames =
lambdaExpr.arguments().stream()
.map(v -> v.variable().identifier().name())
.collect(Collectors.toList());
Set<String> duplicateArgNames =
allArgNames.stream()
.filter(n -> Collections.frequency(allArgNames, n) > 1)
.collect(Collectors.toSet());
Preconditions.checkState(
duplicateArgNames.isEmpty(),
String.format(
"Lambda arguments cannot have duplicate names: %s", duplicateArgNames.toString()));

return lambdaExpr;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.api.generator.engine.ast.IfStatement;
import com.google.api.generator.engine.ast.InstanceofExpr;
import com.google.api.generator.engine.ast.JavaDocComment;
import com.google.api.generator.engine.ast.LambdaExpr;
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.LogicalOperationExpr;
import com.google.api.generator.engine.ast.MethodDefinition;
Expand Down Expand Up @@ -292,6 +293,13 @@ public void visit(AssignmentOperationExpr assignmentOperationExpr) {
assignmentOperationExpr.valueExpr().accept(this);
}

@Override
public void visit(LambdaExpr lambdaExpr) {
variableExpressions(lambdaExpr.arguments());
statements(lambdaExpr.body());
lambdaExpr.returnExpr().accept(this);
}

/** =============================== STATEMENTS =============================== */
@Override
public void visit(ExprStatement exprStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.api.generator.engine.ast.IfStatement;
import com.google.api.generator.engine.ast.InstanceofExpr;
import com.google.api.generator.engine.ast.JavaDocComment;
import com.google.api.generator.engine.ast.LambdaExpr;
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.LogicalOperationExpr;
import com.google.api.generator.engine.ast.MethodDefinition;
Expand Down Expand Up @@ -509,6 +510,45 @@ public void visit(AssignmentOperationExpr assignmentOperationExpr) {
assignmentOperationExpr.valueExpr().accept(this);
}

@Override
public void visit(LambdaExpr lambdaExpr) {
if (lambdaExpr.arguments().isEmpty()) {
leftParen();
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: I usually don't mind abbreviations (even though, they are against best practices in java), but for this one Paren may stand for Parent or Parenthesis, both of which are possible in a typical programming tasks. I know it was not part of this PR, but can you please rename it to left/rightParenthesis instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do in a subsequent PR.

rightParen();
} else if (lambdaExpr.arguments().size() == 1) {
// Print just the variable.
lambdaExpr.arguments().get(0).variable().identifier().accept(this);
} else {
// Stylistic choice - print the types and variable names for clarity.
leftParen();
int numArguments = lambdaExpr.arguments().size();
for (int i = 0; i < numArguments; i++) {
lambdaExpr.arguments().get(i).accept(this);
if (i < numArguments - 1) {
buffer.append(COMMA);
Copy link
Contributor

Choose a reason for hiding this comment

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

For natural constants (like 0, 1, or ',' in this case) it is basically a good practice to use them directly, no need to declare constants, otherwise it would be similar to having something like for (i = ZERO ...) in the for loop above. Or compare it to the line 532, where the lambda arrow operator is used directly (which is also absolutely fine).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would prefer sticking with the small, string-literal constants in this case to eliminate potential typos, ease maintainability, and avoid depending on the compiler to optimize away unnecessary new object creation (as such optimizations may not necessarily find all applicable instances).

space();
}
}
rightParen();
}

space();
buffer.append("->");
space();

if (lambdaExpr.body().isEmpty()) {
// Just the return expression - don't render "return".
lambdaExpr.returnExpr().expr().accept(this);
return;
}

leftBrace();
newline();
statements(lambdaExpr.body());
ExprStatement.withExpr(lambdaExpr.returnExpr()).accept(this);
rightBrace();
}

/** =============================== STATEMENTS =============================== */
@Override
public void visit(ExprStatement exprStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TESTS = [
"IfStatementTest",
"InstanceofExprTest",
"JavaDocCommentTest",
"LambdaExprTest",
"MethodDefinitionTest",
"NewObjectExprTest",
"NullObjectValueTest",
Expand Down
164 changes: 164 additions & 0 deletions src/test/java/com/google/api/generator/engine/ast/LambdaExprTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2021 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 static org.junit.Assert.assertThrows;

import java.util.Arrays;
import org.junit.Test;

public class LambdaExprTest {
@Test
public void validLambdaExpr_noArguments() {
LambdaExpr.builder()
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build();
}

@Test
public void validLambdaExpr_severalArguments() {
VariableExpr argOneVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build())
.setIsDecl(true)
.build();
VariableExpr argTwoVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg2").setType(TypeNode.STRING).build())
.setIsDecl(true)
.build();
VariableExpr argThreeVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg3").setType(TypeNode.BOOLEAN).build())
.setIsDecl(true)
.build();

LambdaExpr.builder()
.setArguments(argOneVarExpr, argTwoVarExpr, argThreeVarExpr)
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build();
}

@Test
public void validLambdaExpr_withBody() {
VariableExpr fooVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("foo").setType(TypeNode.INT).build())
.build();

ExprStatement statement =
ExprStatement.withExpr(
AssignmentExpr.builder()
.setVariableExpr(fooVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(
ValueExpr.builder()
.setValue(
PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build())
.build())
.build());
LambdaExpr.builder()
.setBody(Arrays.asList(statement))
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build();
}

@Test
public void invalidLambdaExpr_returnsVoid() {
assertThrows(
IllegalStateException.class,
() ->
LambdaExpr.builder()
.setReturnExpr(
MethodInvocationExpr.builder()
.setMethodName("foo")
.setReturnType(TypeNode.VOID)
.build())
.build());
}

@Test
public void invalidLambdaExpr_undeclaredArgVarExpr() {
VariableExpr argVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build())
.build();

assertThrows(
IllegalStateException.class,
() ->
LambdaExpr.builder()
.setArguments(argVarExpr)
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build());
}

@Test
public void invalidLambdaExpr_argVarExprWithModifiers() {
VariableExpr argVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build())
.setIsDecl(true)
.setIsStatic(true)
.build();

assertThrows(
IllegalStateException.class,
() ->
LambdaExpr.builder()
.setArguments(argVarExpr)
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build());
}

@Test
public void invalidLambdaExpr_argVarExprWithNonLocalScope() {
VariableExpr argVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build())
.setIsDecl(true)
.setScope(ScopeNode.PUBLIC)
.build();

assertThrows(
IllegalStateException.class,
() ->
LambdaExpr.builder()
.setArguments(argVarExpr)
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build());
}

@Test
public void invalidLambdaExpr_repeatedArgName() {
VariableExpr argOneVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg").setType(TypeNode.INT).build())
.setIsDecl(true)
.build();
VariableExpr argTwoVarExpr =
VariableExpr.builder()
.setVariable(Variable.builder().setName("arg").setType(TypeNode.STRING).build())
.setIsDecl(true)
.build();

assertThrows(
IllegalStateException.class,
() ->
LambdaExpr.builder()
.setArguments(argOneVarExpr, argTwoVarExpr)
.setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("foo")))
.build());
}
}
Loading