Very basic java interop has been added

This commit is contained in:
Sameer Rahmani 2019-12-24 17:31:45 +00:00
parent 3f3f109c12
commit f3ac693da3
4 changed files with 79 additions and 2 deletions

View File

@ -9,6 +9,9 @@ import serene.simple.builtin.AFn;
import static java.util.Arrays.asList;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ListNode<T extends Node> extends Node implements Iterable<T> {
public static final ListNode<?> EMPTY = new ListNode<>();
@ -47,13 +50,18 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
@Override
public Object eval(BaseScope scope) {
Object f = this.first().eval(scope);
SymbolNode firstElement = (SymbolNode) this.first();
List<Object> args = new ArrayList<Object>();
for (T node : this.rest()) {
args.add(node.eval(scope));
}
if (firstElement.name.startsWith(".")) {
return this.evalInterop(scope, firstElement, args.subList(1, args.size()));
}
Object f = firstElement.eval(scope);
if (f instanceof AFn) {
return evalBuiltin(scope, (AFn) f, args);
}
@ -71,6 +79,59 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
return fn.apply(args.toArray());
}
public Object evalInterop(BaseScope scope, SymbolNode firstElement, List<Object> rest)
throws SereneException {
String mName = firstElement.name.substring(1, firstElement.name.length());
Object target = this.rest().first().eval(scope);
if (mName.startsWith("-")) {
return this.evalInteropProperty(
scope,
target,
mName.substring(1, firstElement.name.length()));
}
try {
System.out.println(target.getClass().getName());
Method f = target.getClass().getMethod(mName);
return f.invoke(target, rest.toArray());
}
catch(NoSuchMethodException e) {
throw new SereneException(
String.format(
"Can't find method '%s' on object '%s'.",
mName,
this.rest().first()));
}
catch (InvocationTargetException e) {
Throwable cause = e.getCause();
throw new SereneException(
String.format(
"Invocation of '%s' failed because of: %s%n",
mName, cause.getMessage()));
}
catch (IllegalAccessException e) {
throw new SereneException(
String.format("Illegal access from '%s'", mName));
}
}
public Object evalInteropProperty(BaseScope scope, Object target, String propertyName)
throws SereneException {
try {
Class<?> targetClass = target.getClass();
return targetClass.getField(propertyName);
}
catch(NoSuchFieldException e) {
throw new SereneException(
String.format(
"Can't find field '%s' on object '%s'.",
propertyName,
target.toString()));
}
}
public T first() {
if (this != EMPTY) {
return this.first;

View File

@ -3,6 +3,7 @@ package serene.simple;
import java.util.HashMap;
import serene.simple.builtin.PrintlnFn;
import serene.simple.builtin.QuitFn;
import serene.simple.builtin.PlusFn;
public class RootScope extends BaseScope {
@ -10,6 +11,7 @@ public class RootScope extends BaseScope {
private final HashMap<String, Object> symbolsMapping = new HashMap<String, Object>() {{
put("println", new PrintlnFn());
put("quit", new QuitFn());
put("+", new PlusFn());
}};
// "+", PlusFn,
// "-", MinusFn,

View File

@ -0,0 +1,14 @@
package serene.simple.builtin;
import serene.simple.BaseScope;
public class PlusFn extends AFn {
public String fnName() {
return "+";
};
public Object eval(BaseScope scope) {
return 0;
}
}

View File

@ -5,7 +5,7 @@ import serene.simple.BaseScope;
public class QuitFn extends AFn {
public String fnName() {
return "println";
return "quit";
};
public Object eval(BaseScope scope) {