Skip to content

Commit 28e4f75

Browse files
committed
Lambda Support
1 parent 8e62e24 commit 28e4f75

15 files changed

Lines changed: 2309 additions & 142 deletions

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@
319319
<version>1.3</version>
320320
<scope>test</scope>
321321
</dependency>
322+
<dependency>
323+
<groupId>org.mockito</groupId>
324+
<artifactId>mockito-inline</artifactId>
325+
<version>5.2.0</version>
326+
<scope>test</scope>
327+
</dependency>
322328

323329
<dependency>
324330
<groupId>javax.servlet</groupId>

src/main/java/bsh/BSHFormalParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void insureParsed()
9898
Evaluate the types.
9999
Note that type resolution does not require the interpreter instance.
100100
*/
101-
public Object eval( CallStack callstack, Interpreter interpreter )
101+
public Class<?>[] eval( CallStack callstack, Interpreter interpreter )
102102
throws EvalError
103103
{
104104
if ( paramTypes != null )
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package bsh;
2+
3+
class BSHLambdaExpression extends SimpleNode {
4+
5+
String singleParamName;
6+
7+
private boolean initializedValues = false;
8+
private Modifiers[] paramsModifiers;
9+
private Class<?>[] paramsTypes;
10+
private String[] paramsNames;
11+
private Node body;
12+
13+
public BSHLambdaExpression(int i) {
14+
super(i);
15+
}
16+
17+
private void initValues(CallStack callstack, Interpreter interpreter) throws EvalError {
18+
if (this.initializedValues) return;
19+
if (this.jjtGetNumChildren() == 2) {
20+
BSHFormalParameters parameters = (BSHFormalParameters) this.jjtGetChild(0);
21+
this.paramsTypes = parameters.eval(callstack, interpreter);
22+
this.paramsModifiers = parameters.getParamModifiers();
23+
this.paramsNames = parameters.getParamNames();
24+
this.body = this.jjtGetChild(1);
25+
} else {
26+
this.paramsTypes = new Class[] { null };
27+
this.paramsModifiers = new Modifiers[] { null };
28+
this.paramsNames = new String[] { this.singleParamName };
29+
this.body = this.jjtGetChild(0);
30+
}
31+
this.initializedValues = true;
32+
}
33+
34+
@Override
35+
public Object eval(CallStack callstack, Interpreter interpreter) throws EvalError {
36+
this.initValues(callstack, interpreter);
37+
return BshLambda.fromLambdaExpression(this, callstack.top(), this.paramsModifiers, this.paramsTypes, this.paramsNames, this.body);
38+
}
39+
40+
}

src/main/java/bsh/BSHPrimaryExpression.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ private Object eval( boolean toLHS,
9696
if ( isArrayExpression && null != cached )
9797
return cached;
9898

99-
Object obj = jjtGetChild(0);
100-
101-
for( int i=1; i < jjtGetNumChildren(); i++ )
102-
obj = ((BSHPrimarySuffix) jjtGetChild(i)).doSuffix(
103-
obj, toLHS, callstack, interpreter);
99+
Node[] children = this.jjtGetChildren();
100+
Object obj = children[0];
101+
for (int i = 1; i < children.length; i++) {
102+
BSHPrimarySuffix primarySuffix = (BSHPrimarySuffix) jjtGetChild(i);
103+
if (primarySuffix.operation == BSHPrimarySuffix.METHODREF && i != (children.length-1))
104+
throw new EvalError("Method Reference must be the last suffix!", primarySuffix, callstack);
105+
106+
obj = primarySuffix.doSuffix(obj, toLHS, callstack, interpreter);
107+
}
104108

105109
/*
106110
If the result is a Node eval() it to an object or LHS

src/main/java/bsh/BSHPrimarySuffix.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class BSHPrimarySuffix extends SimpleNode
3636
INDEX = 1,
3737
NAME = 2,
3838
PROPERTY = 3,
39-
NEW = 4;
39+
NEW = 4,
40+
METHODREF = 5;
4041

4142
public int operation;
4243
Object index;
@@ -117,6 +118,10 @@ make a static method nodeToObject() or something. The point is
117118

118119
case NEW:
119120
return doNewInner(obj, toLHS, callstack, interpreter);
121+
122+
case METHODREF:
123+
return doMethodRef ( obj );
124+
120125
default:
121126
throw new InterpreterError( "Unknown suffix type" );
122127
}
@@ -359,6 +364,10 @@ private Object doProperty( boolean toLHS,
359364
}
360365
}
361366

367+
private BshLambda doMethodRef(Object obj) {
368+
return BshLambda.fromMethodReference(this, obj, field);
369+
}
370+
362371
@Override
363372
public String toString() {
364373
if (operation == INDEX)

0 commit comments

Comments
 (0)