1
0
forked from GitHub/gf-core

added Expr.apply

This commit is contained in:
krasimir
2017-05-10 14:41:55 +00:00
parent a9a6505eaf
commit 788124a96c
2 changed files with 48 additions and 2 deletions

View File

@@ -1226,8 +1226,32 @@ Java_org_grammaticalframework_pgf_Expr_initStringLit(JNIEnv* env, jclass clazz,
return expr;
}
JNIEXPORT jlong JNICALL
Java_org_grammaticalframework_pgf_Expr_initApp__Lorg_grammaticalframework_pgf_Expr_2_3Lorg_grammaticalframework_pgf_Expr_2J
(JNIEnv* env, jclass clazz, jobject jfun, jobjectArray args, jlong jpool)
{
GuPool* pool = l2p(jpool);
PgfExpr expr = gu_variant_from_ptr(get_ref(env, jfun));
size_t n_args = (*env)->GetArrayLength(env, args);
for (size_t i = 0; i < n_args; i++) {
PgfExpr fun = expr;
PgfExpr arg = gu_variant_from_ptr(get_ref(env, (*env)->GetObjectArrayElement(env, args, i)));
PgfExprApp* e =
gu_new_variant(PGF_EXPR_APP,
PgfExprApp,
&expr, pool);
e->fun = fun;
e->arg = arg;
}
return expr;
}
JNIEXPORT jlong JNICALL
Java_org_grammaticalframework_pgf_Expr_initApp(JNIEnv* env, jclass clazz, jstring jfun, jobjectArray args, jlong jpool)
Java_org_grammaticalframework_pgf_Expr_initApp__Ljava_lang_String_2_3Lorg_grammaticalframework_pgf_Expr_2J
(JNIEnv* env, jclass clazz, jstring jfun, jobjectArray args, jlong jpool)
{
GuPool* pool = l2p(jpool);
PgfExpr expr;

View File

@@ -44,10 +44,31 @@ public class Expr implements Serializable {
}
this.pool = new Pool();
this.master = Arrays.copyOf(args, args.length);
this.master = args;
this.ref = initApp(fun, args, pool.ref);
}
/** Constructs an expression which is an application
* of the first expression to a list of arguments.
*/
public static Expr apply(Expr fun, Expr... args) {
if (fun == null)
throw new IllegalArgumentException("fun == null");
for (int i = 0; i < args.length; i++) {
if (args[i] == null)
throw new IllegalArgumentException("the "+(i+1)+"th argument is null");
}
Object[] master = new Object[args.length+1];
master[0] = fun;
for (int i = 0; i < args.length; i++) {
master[i+1] = args[i].master;
}
Pool pool = new Pool();
return new Expr(pool, master, initApp(fun, args, pool.ref));
}
/** Returns the expression as a string in the GF syntax */
public String toString() {
return showExpr(ref);
@@ -64,6 +85,7 @@ public class Expr implements Serializable {
private static native String showExpr(long ref);
private static native long initStringLit(String s, long pool);
private static native long initApp(Expr fun, Expr[] args, long pool);
private static native long initApp(String fun, Expr[] args, long pool);
private void writeObject(ObjectOutputStream out) throws IOException {