Skip to content

Commit

Permalink
feat: add AST support for lambdas (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung committed May 26, 2021
1 parent f5c7b86 commit 9ced678
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 0 deletions.
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
@@ -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;
}
}
}
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
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();
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);
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
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
@@ -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());
}
}

0 comments on commit 9ced678

Please sign in to comment.