From a77ae5da300473b315f47202319505ca7c804e62 Mon Sep 17 00:00:00 2001 From: krasimir Date: Wed, 10 May 2017 14:41:55 +0000 Subject: added Expr.apply --- .../java/org/grammaticalframework/pgf/Expr.java | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/runtime/java/org') diff --git a/src/runtime/java/org/grammaticalframework/pgf/Expr.java b/src/runtime/java/org/grammaticalframework/pgf/Expr.java index 0195f8019..6541db140 100644 --- a/src/runtime/java/org/grammaticalframework/pgf/Expr.java +++ b/src/runtime/java/org/grammaticalframework/pgf/Expr.java @@ -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 { -- cgit v1.2.3