|
| 1 | +package koala.dynamicjava.tree; |
| 2 | +import edu.rice.cs.plt.tuple.Option; |
| 3 | +import koala.dynamicjava.tree.visitor.Visitor; |
| 4 | +import java.util.*; |
| 5 | +public class LambdaExpression extends PrimaryExpression { |
| 6 | +private Option<List<FormalParameter>> typedParams; |
| 7 | +private Option<List<String>> inferredParams; |
| 8 | +// A lambda expression can have either a block or an expression as its body. |
| 9 | +private Option<BlockStatement> blockBody; |
| 10 | +private Option<Expression> exprBody; |
| 11 | +public LambdaExpression(List<FormalParameter> typedParams, |
| 12 | +List<String> inferredParams, |
| 13 | +BlockStatement blockBody, |
| 14 | +Expression exprBody) { |
| 15 | +this(typedParams, inferredParams, blockBody, exprBody, SourceInfo.NONE); |
| 16 | +} |
| 17 | +/** |
| 18 | +* This class creates new lambda expression, which is newly introduced by Java 8. |
| 19 | +* |
| 20 | +* @param typedParams A list of parameters annotated with types. |
| 21 | +* @param inferredParams A list of parameters without type annotations. |
| 22 | +* @param blockBody A block, used as the body of the lambda. |
| 23 | +* @param exprBody An expression, used as the body of the lambda |
| 24 | +* @param si Source information. |
| 25 | +* @throws java.lang.IllegalArgumentException Thrown if parameters or body arguments are both null or are both |
| 26 | +* provided. |
| 27 | +*/ |
| 28 | +public LambdaExpression(List<FormalParameter> typedParams, |
| 29 | +List<String> inferredParams, |
| 30 | +BlockStatement blockBD, |
| 31 | +Expression exprBD, |
| 32 | +SourceInfo si) { |
| 33 | +super(si); |
| 34 | +if (blockBD == null && exprBD == null) { |
| 35 | +throw new IllegalArgumentException("No body was provided."); |
| 36 | +} |
| 37 | +if (typedParams != null && inferredParams != null) { |
| 38 | +throw new IllegalArgumentException("Both kinds of Parameter List was provided"); |
| 39 | +} |
| 40 | +if (blockBD != null && exprBD!= null) { |
| 41 | +throw new IllegalArgumentException("Both kinds of Bodies have to be provided."); |
| 42 | +} |
| 43 | +if (blockBD == null) { |
| 44 | +this.blockBD = Option.none(); |
| 45 | +} else { |
| 46 | +this.blockBD = Option.wrap(blockBD); |
| 47 | +} |
| 48 | +if (exprBD == null) { |
| 49 | +this.exprBD = Option.none(); |
| 50 | +} else { |
| 51 | +this.exprBD = Option.wrap(exprBD); |
| 52 | +} |
| 53 | +if (typedParams == null) { |
| 54 | +this.typedParams = Option.none(); |
| 55 | +} else { |
| 56 | +this.typedParams = Option.wrap(typedParams); |
| 57 | +} |
| 58 | +if (inferredParams == null) { |
| 59 | +this.inferredParams = Option.none(); |
| 60 | +} else { |
| 61 | +this.inferredParams = Option.wrap(inferredParams); |
| 62 | +} |
| 63 | +} |
| 64 | +@Override |
| 65 | +public <T> T acceptVisitor(Visitor<T> visitor) { |
| 66 | +return visitor.visit(this); |
| 67 | +} |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
0 commit comments