mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
added PGF.inferExpr and Type.toString in the Java binding
This commit is contained in:
@@ -172,7 +172,7 @@ Java_org_grammaticalframework_pgf_PGF_getFunctionType(JNIEnv* env, jobject self,
|
||||
return NULL;
|
||||
|
||||
jclass type_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Type");
|
||||
jmethodID constrId = (*env)->GetMethodID(env, type_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;J)V");
|
||||
jmethodID constrId = (*env)->GetMethodID(env, type_class, "<init>", "(Ljava/lang/Object;J)V");
|
||||
jobject jtype = (*env)->NewObject(env, type_class, constrId, self, p2l(tp));
|
||||
|
||||
return jtype;
|
||||
@@ -1191,6 +1191,24 @@ Java_org_grammaticalframework_pgf_Type_getCategory(JNIEnv* env, jobject self)
|
||||
return gu2j_string(env, tp->cid);
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_org_grammaticalframework_pgf_Type_toString(JNIEnv* env, jobject self)
|
||||
{
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
pgf_print_type(get_ref(env, self), NULL, 0, out, err);
|
||||
|
||||
GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
|
||||
jstring jstr = gu2j_string(env, str);
|
||||
|
||||
gu_pool_free(tmp_pool);
|
||||
return jstr;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat)
|
||||
{
|
||||
@@ -1247,3 +1265,50 @@ Java_org_grammaticalframework_pgf_PGF_compute(JNIEnv* env, jobject self, jobject
|
||||
|
||||
return jexpr;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_org_grammaticalframework_pgf_PGF_inferExpr(JNIEnv* env, jobject self, jobject jexpr)
|
||||
{
|
||||
GuPool* pool = gu_new_pool();
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
PgfExpr expr =
|
||||
gu_variant_from_ptr((void*) get_ref(env, jexpr));
|
||||
PgfType* tp =
|
||||
pgf_infer_expr(get_ref(env, self), &expr, err, pool);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else if (gu_exn_caught(err, PgfTypeError)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/TypeError", msg);
|
||||
} else {
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The type cannot be inferred");
|
||||
}
|
||||
gu_pool_free(tmp_pool);
|
||||
gu_pool_free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gu_pool_free(tmp_pool);
|
||||
|
||||
jclass pool_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Pool");
|
||||
jmethodID pool_constrId = (*env)->GetMethodID(env, pool_class, "<init>", "(J)V");
|
||||
jobject jpool = (*env)->NewObject(env, pool_class, pool_constrId, p2l(pool));
|
||||
|
||||
jclass expr_class = (*env)->GetObjectClass(env, jexpr);
|
||||
jmethodID expr_constrId = (*env)->GetMethodID(env, expr_class, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Ljava/lang/Object;J)V");
|
||||
jexpr = (*env)->NewObject(env, expr_class, expr_constrId, jpool, NULL, p2l(gu_variant_to_ptr(expr)));
|
||||
|
||||
jclass type_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Type");
|
||||
jmethodID type_constrId = (*env)->GetMethodID(env, type_class, "<init>", "(Ljava/lang/Object;J)V");
|
||||
jobject jtype = (*env)->NewObject(env, type_class, type_constrId, jpool, p2l(tp));
|
||||
|
||||
jclass typed_expr_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/TypedExpr");
|
||||
jmethodID typed_expr_constrId = (*env)->GetMethodID(env, typed_expr_class, "<init>", "(Lorg/grammaticalframework/pgf/Expr;Lorg/grammaticalframework/pgf/Type;)V");
|
||||
jobject jtyped_expr = (*env)->NewObject(env, typed_expr_class, typed_expr_constrId, jexpr, jtype);
|
||||
|
||||
return jtyped_expr;
|
||||
}
|
||||
|
||||
@@ -58,6 +58,10 @@ public class PGF {
|
||||
*/
|
||||
public native Expr compute(Expr expr);
|
||||
|
||||
/** Takes an expression and returns a refined version
|
||||
* of the expression together with its type */
|
||||
public native TypedExpr inferExpr(Expr expr) throws TypeError;
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// private stuff
|
||||
private Pool pool;
|
||||
|
||||
@@ -12,15 +12,17 @@ public class Type {
|
||||
* If the represented type is A1 -> A2 -> ... An -> B, then
|
||||
* the hypotheses represent the types in A1, A2 ... An. */
|
||||
public native Hypo[] getHypos();
|
||||
|
||||
|
||||
public native String toString();
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// private stuff
|
||||
|
||||
private PGF gr;
|
||||
|
||||
private Object master;
|
||||
private long ref;
|
||||
|
||||
private Type(PGF gr, long ref) {
|
||||
this.gr = gr;
|
||||
this.ref = ref;
|
||||
|
||||
private Type(Object master, long ref) {
|
||||
this.master = master;
|
||||
this.ref = ref;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
public class TypeError extends Exception {
|
||||
public TypeError(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
24
src/runtime/java/org/grammaticalframework/pgf/TypedExpr.java
Normal file
24
src/runtime/java/org/grammaticalframework/pgf/TypedExpr.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package org.grammaticalframework.pgf;
|
||||
|
||||
/** This is just a pair of an expression and its type */
|
||||
public class TypedExpr {
|
||||
private Expr expr;
|
||||
private Type type;
|
||||
|
||||
public TypedExpr(Expr expr, Type type) {
|
||||
this.expr = expr;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Expr getExpr() {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "<"+expr.toString()+" : "+type.toString()+">";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user