java interop has been improved

This commit is contained in:
Sameer Rahmani 2019-12-24 19:05:18 +00:00
parent f3ac693da3
commit bf21c741ec
3 changed files with 17 additions and 3 deletions

View File

@ -1,2 +1,4 @@
repl: repl:
gradle compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main gradle compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main
docs:
npx docco src/**/*.java

View File

@ -89,14 +89,14 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
return this.evalInteropProperty( return this.evalInteropProperty(
scope, scope,
target, target,
mName.substring(1, firstElement.name.length())); mName.substring(1, mName.length()));
} }
try { try {
System.out.println(target.getClass().getName());
Method f = target.getClass().getMethod(mName); Method f = target.getClass().getMethod(mName);
return f.invoke(target, rest.toArray()); return f.invoke(target, rest.toArray());
} }
catch(NoSuchMethodException e) { catch(NoSuchMethodException e) {
throw new SereneException( throw new SereneException(
String.format( String.format(
@ -119,8 +119,9 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
public Object evalInteropProperty(BaseScope scope, Object target, String propertyName) public Object evalInteropProperty(BaseScope scope, Object target, String propertyName)
throws SereneException { throws SereneException {
try { try {
Class<?> targetClass = target.getClass(); Class<?> targetClass = this.getClassOf(target);
return targetClass.getField(propertyName); return targetClass.getField(propertyName);
} }
catch(NoSuchFieldException e) { catch(NoSuchFieldException e) {
@ -186,4 +187,12 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
return output + ")"; return output + ")";
} }
private Class<?> getClassOf(Object target) {
if (target instanceof Class) {
return (Class<?>) target;
}
return target.getClass();
}
} }

View File

@ -12,6 +12,9 @@ public class RootScope extends BaseScope {
put("println", new PrintlnFn()); put("println", new PrintlnFn());
put("quit", new QuitFn()); put("quit", new QuitFn());
put("+", new PlusFn()); put("+", new PlusFn());
put("System", System.class);
put("Boolean", Boolean.class);
put("String", String.class);
}}; }};
// "+", PlusFn, // "+", PlusFn,
// "-", MinusFn, // "-", MinusFn,