Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Lambda Support
  • Loading branch information
Net-0 committed Aug 17, 2024
commit f7fc761b01886c5dce60f4798400dee78d817dd8
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/bsh/BSHFormalParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void insureParsed()
Evaluate the types.
Note that type resolution does not require the interpreter instance.
*/
public Object eval( CallStack callstack, Interpreter interpreter )
public Class<?>[] eval( CallStack callstack, Interpreter interpreter )
throws EvalError
{
if ( paramTypes != null )
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/bsh/BSHLambdaExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package bsh;

class BSHLambdaExpression extends SimpleNode {

String singleParamName;

private boolean initializedValues = false;
private Modifiers[] paramsModifiers;
private Class<?>[] paramsTypes;
private String[] paramsNames;
private Node body;

public BSHLambdaExpression(int i) {
super(i);
}

private void initValues(CallStack callstack, Interpreter interpreter) throws EvalError {
if (this.initializedValues) return;
if (this.jjtGetNumChildren() == 2) {
BSHFormalParameters parameters = (BSHFormalParameters) this.jjtGetChild(0);
this.paramsTypes = parameters.eval(callstack, interpreter);
this.paramsModifiers = parameters.getParamModifiers();
this.paramsNames = parameters.getParamNames();
this.body = this.jjtGetChild(1);
} else {
this.paramsTypes = new Class[] { null };
this.paramsModifiers = new Modifiers[] { null };
this.paramsNames = new String[] { this.singleParamName };
this.body = this.jjtGetChild(0);
}
this.initializedValues = true;
}

@Override
public Object eval(CallStack callstack, Interpreter interpreter) throws EvalError {
this.initValues(callstack, interpreter);
return BshLambda.fromLambdaExpression(this, callstack.top(), this.paramsModifiers, this.paramsTypes, this.paramsNames, this.body);
}

}
14 changes: 9 additions & 5 deletions src/main/java/bsh/BSHPrimaryExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ private Object eval( boolean toLHS,
if ( isArrayExpression && null != cached )
return cached;

Object obj = jjtGetChild(0);

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

obj = primarySuffix.doSuffix(obj, toLHS, callstack, interpreter);
}

/*
If the result is a Node eval() it to an object or LHS
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/bsh/BSHPrimarySuffix.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class BSHPrimarySuffix extends SimpleNode
INDEX = 1,
NAME = 2,
PROPERTY = 3,
NEW = 4;
NEW = 4,
METHODREF = 5;

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

case NEW:
return doNewInner(obj, toLHS, callstack, interpreter);

case METHODREF:
return doMethodRef ( obj );

default:
throw new InterpreterError( "Unknown suffix type" );
}
Expand Down Expand Up @@ -375,6 +380,10 @@ private Object doProperty( boolean toLHS,
}
}

private BshLambda doMethodRef(Object obj) {
return BshLambda.fromMethodReference(this, obj, field);
}

@Override
public String toString() {
if (operation == INDEX)
Expand Down
Loading