-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AST support for lambdas (#736)
- Loading branch information
Showing
8 changed files
with
465 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/google/api/generator/engine/ast/LambdaExpr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
src/test/java/com/google/api/generator/engine/ast/LambdaExprTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.