Very basic java interop has been added
This commit is contained in:
parent
3f3f109c12
commit
f3ac693da3
|
@ -9,6 +9,9 @@ import serene.simple.builtin.AFn;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
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 class ListNode<T extends Node> extends Node implements Iterable<T> {
|
||||||
public static final ListNode<?> EMPTY = new ListNode<>();
|
public static final ListNode<?> EMPTY = new ListNode<>();
|
||||||
|
|
||||||
|
@ -47,13 +50,18 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(BaseScope scope) {
|
||||||
Object f = this.first().eval(scope);
|
SymbolNode firstElement = (SymbolNode) this.first();
|
||||||
List<Object> args = new ArrayList<Object>();
|
List<Object> args = new ArrayList<Object>();
|
||||||
|
|
||||||
for (T node : this.rest()) {
|
for (T node : this.rest()) {
|
||||||
args.add(node.eval(scope));
|
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) {
|
if (f instanceof AFn) {
|
||||||
return evalBuiltin(scope, (AFn) f, args);
|
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());
|
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() {
|
public T first() {
|
||||||
if (this != EMPTY) {
|
if (this != EMPTY) {
|
||||||
return this.first;
|
return this.first;
|
||||||
|
|
|
@ -3,6 +3,7 @@ package serene.simple;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import serene.simple.builtin.PrintlnFn;
|
import serene.simple.builtin.PrintlnFn;
|
||||||
import serene.simple.builtin.QuitFn;
|
import serene.simple.builtin.QuitFn;
|
||||||
|
import serene.simple.builtin.PlusFn;
|
||||||
|
|
||||||
|
|
||||||
public class RootScope extends BaseScope {
|
public class RootScope extends BaseScope {
|
||||||
|
@ -10,6 +11,7 @@ public class RootScope extends BaseScope {
|
||||||
private final HashMap<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
private final HashMap<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
||||||
put("println", new PrintlnFn());
|
put("println", new PrintlnFn());
|
||||||
put("quit", new QuitFn());
|
put("quit", new QuitFn());
|
||||||
|
put("+", new PlusFn());
|
||||||
}};
|
}};
|
||||||
// "+", PlusFn,
|
// "+", PlusFn,
|
||||||
// "-", MinusFn,
|
// "-", MinusFn,
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ import serene.simple.BaseScope;
|
||||||
|
|
||||||
public class QuitFn extends AFn {
|
public class QuitFn extends AFn {
|
||||||
public String fnName() {
|
public String fnName() {
|
||||||
return "println";
|
return "quit";
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(BaseScope scope) {
|
||||||
|
|
Loading…
Reference in New Issue