mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-23 01:52:50 -06:00
merge the states for reasoning and evaluation into a single structure
This commit is contained in:
@@ -1,17 +1,17 @@
|
|||||||
#include "pgf/pgf.h"
|
#include "pgf/pgf.h"
|
||||||
#include "pgf/data.h"
|
#include "pgf/data.h"
|
||||||
#include "pgf/evaluator.h"
|
#include "pgf/reasoner.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define PGF_ARGS_DELTA 5
|
#define PGF_ARGS_DELTA 5
|
||||||
|
|
||||||
static inline PgfClosure*
|
static inline PgfClosure*
|
||||||
pgf_mk_pap(PgfEvalState* state, PgfClosure* fun,
|
pgf_mk_pap(PgfReasoner* rs, PgfClosure* fun,
|
||||||
size_t n_args, PgfClosure** args)
|
size_t n_args, PgfClosure** args)
|
||||||
{
|
{
|
||||||
if (n_args > 0) {
|
if (n_args > 0) {
|
||||||
PgfValuePAP* val = gu_new_flex(state->pool, PgfValuePAP, args, n_args);
|
PgfValuePAP* val = gu_new_flex(rs->pool, PgfValuePAP, args, n_args);
|
||||||
val->header.code = state->eval_gates->evaluate_value_pap;
|
val->header.code = rs->eval_gates->evaluate_value_pap;
|
||||||
val->fun = fun;
|
val->fun = fun;
|
||||||
val->n_args = n_args*sizeof(PgfClosure*);
|
val->n_args = n_args*sizeof(PgfClosure*);
|
||||||
for (size_t i = 0; i < n_args; i++) {
|
for (size_t i = 0; i < n_args; i++) {
|
||||||
@@ -23,7 +23,7 @@ pgf_mk_pap(PgfEvalState* state, PgfClosure* fun,
|
|||||||
}
|
}
|
||||||
|
|
||||||
PgfClosure*
|
PgfClosure*
|
||||||
pgf_evaluate_expr_thunk(PgfEvalState* state, PgfExprThunk* thunk)
|
pgf_evaluate_expr_thunk(PgfReasoner* rs, PgfExprThunk* thunk)
|
||||||
{
|
{
|
||||||
PgfEnv* env = thunk->env;
|
PgfEnv* env = thunk->env;
|
||||||
PgfExpr expr = thunk->expr;
|
PgfExpr expr = thunk->expr;
|
||||||
@@ -39,7 +39,7 @@ repeat:;
|
|||||||
PgfExprAbs* eabs = ei.data;
|
PgfExprAbs* eabs = ei.data;
|
||||||
|
|
||||||
if (n_args > 0) {
|
if (n_args > 0) {
|
||||||
PgfEnv* new_env = gu_new(PgfEnv, state->pool);
|
PgfEnv* new_env = gu_new(PgfEnv, rs->pool);
|
||||||
new_env->next = env;
|
new_env->next = env;
|
||||||
new_env->closure = args[--n_args];
|
new_env->closure = args[--n_args];
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ repeat:;
|
|||||||
expr = eabs->body;
|
expr = eabs->body;
|
||||||
goto repeat;
|
goto repeat;
|
||||||
} else {
|
} else {
|
||||||
thunk->header.code = state->eval_gates->evaluate_value_lambda;
|
thunk->header.code = rs->eval_gates->evaluate_value_lambda;
|
||||||
thunk->expr = eabs->body;
|
thunk->expr = eabs->body;
|
||||||
res = &thunk->header;
|
res = &thunk->header;
|
||||||
}
|
}
|
||||||
@@ -56,8 +56,8 @@ repeat:;
|
|||||||
case PGF_EXPR_APP: {
|
case PGF_EXPR_APP: {
|
||||||
PgfExprApp* eapp = ei.data;
|
PgfExprApp* eapp = ei.data;
|
||||||
PgfExprThunk* thunk =
|
PgfExprThunk* thunk =
|
||||||
gu_new(PgfExprThunk, state->pool);
|
gu_new(PgfExprThunk, rs->pool);
|
||||||
thunk->header.code = state->eval_gates->evaluate_expr_thunk;
|
thunk->header.code = rs->eval_gates->evaluate_expr_thunk;
|
||||||
thunk->env = env;
|
thunk->env = env;
|
||||||
thunk->expr = eapp->arg;
|
thunk->expr = eapp->arg;
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ repeat:;
|
|||||||
case PGF_EXPR_LIT: {
|
case PGF_EXPR_LIT: {
|
||||||
PgfExprLit* elit = ei.data;
|
PgfExprLit* elit = ei.data;
|
||||||
PgfValueLit* val = (PgfValueLit*) thunk;
|
PgfValueLit* val = (PgfValueLit*) thunk;
|
||||||
val->header.code = state->eval_gates->evaluate_value_lit;
|
val->header.code = rs->eval_gates->evaluate_value_lit;
|
||||||
val->lit = elit->lit;
|
val->lit = elit->lit;
|
||||||
res = &val->header;
|
res = &val->header;
|
||||||
break;
|
break;
|
||||||
@@ -81,28 +81,28 @@ repeat:;
|
|||||||
PgfExprMeta* emeta = ei.data;
|
PgfExprMeta* emeta = ei.data;
|
||||||
|
|
||||||
PgfValueMeta* val =
|
PgfValueMeta* val =
|
||||||
gu_new(PgfValueMeta, state->pool);
|
gu_new(PgfValueMeta, rs->pool);
|
||||||
val->header.code = state->eval_gates->evaluate_meta;
|
val->header.code = rs->eval_gates->evaluate_meta;
|
||||||
val->env = env;
|
val->env = env;
|
||||||
val->id = emeta->id;
|
val->id = emeta->id;
|
||||||
res = pgf_mk_pap(state, &val->header, n_args, args);
|
res = pgf_mk_pap(rs, &val->header, n_args, args);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PGF_EXPR_FUN: {
|
case PGF_EXPR_FUN: {
|
||||||
PgfExprFun* efun = ei.data;
|
PgfExprFun* efun = ei.data;
|
||||||
|
|
||||||
PgfAbsFun* absfun =
|
PgfAbsFun* absfun =
|
||||||
gu_seq_binsearch(state->pgf->abstract.funs, pgf_absfun_order, PgfAbsFun, efun->fun);
|
gu_seq_binsearch(rs->abstract->funs, pgf_absfun_order, PgfAbsFun, efun->fun);
|
||||||
gu_assert(absfun != NULL);
|
gu_assert(absfun != NULL);
|
||||||
|
|
||||||
if (absfun->closure.code != NULL) {
|
if (absfun->closure.code != NULL) {
|
||||||
res = pgf_mk_pap(state, (PgfClosure*) &absfun->closure, n_args, args);
|
res = pgf_mk_pap(rs, (PgfClosure*) &absfun->closure, n_args, args);
|
||||||
} else {
|
} else {
|
||||||
size_t arity = absfun->arity;
|
size_t arity = absfun->arity;
|
||||||
|
|
||||||
if (n_args == arity) {
|
if (n_args == arity) {
|
||||||
PgfValue* val = gu_new_flex(state->pool, PgfValue, args, arity);
|
PgfValue* val = gu_new_flex(rs->pool, PgfValue, args, arity);
|
||||||
val->header.code = state->eval_gates->evaluate_value;
|
val->header.code = rs->eval_gates->evaluate_value;
|
||||||
val->con = (PgfClosure*) &absfun->closure;
|
val->con = (PgfClosure*) &absfun->closure;
|
||||||
|
|
||||||
for (size_t i = 0; i < arity; i++) {
|
for (size_t i = 0; i < arity; i++) {
|
||||||
@@ -112,10 +112,10 @@ repeat:;
|
|||||||
} else {
|
} else {
|
||||||
gu_assert(n_args < arity);
|
gu_assert(n_args < arity);
|
||||||
|
|
||||||
PgfExprThunk* lambda = gu_new(PgfExprThunk, state->pool);
|
PgfExprThunk* lambda = gu_new(PgfExprThunk, rs->pool);
|
||||||
lambda->header.code = state->eval_gates->evaluate_value_lambda;
|
lambda->header.code = rs->eval_gates->evaluate_value_lambda;
|
||||||
lambda->env = NULL;
|
lambda->env = NULL;
|
||||||
res = pgf_mk_pap(state, &lambda->header, n_args, args);
|
res = pgf_mk_pap(rs, &lambda->header, n_args, args);
|
||||||
|
|
||||||
for (size_t i = 0; i < arity; i++) {
|
for (size_t i = 0; i < arity; i++) {
|
||||||
PgfExpr new_expr, arg;
|
PgfExpr new_expr, arg;
|
||||||
@@ -123,13 +123,13 @@ repeat:;
|
|||||||
PgfExprVar *evar =
|
PgfExprVar *evar =
|
||||||
gu_new_variant(PGF_EXPR_VAR,
|
gu_new_variant(PGF_EXPR_VAR,
|
||||||
PgfExprVar,
|
PgfExprVar,
|
||||||
&arg, state->pool);
|
&arg, rs->pool);
|
||||||
evar->var = arity-i-1;
|
evar->var = arity-i-1;
|
||||||
|
|
||||||
PgfExprApp *eapp =
|
PgfExprApp *eapp =
|
||||||
gu_new_variant(PGF_EXPR_APP,
|
gu_new_variant(PGF_EXPR_APP,
|
||||||
PgfExprApp,
|
PgfExprApp,
|
||||||
&new_expr, state->pool);
|
&new_expr, rs->pool);
|
||||||
eapp->fun = expr;
|
eapp->fun = expr;
|
||||||
eapp->arg = arg;
|
eapp->arg = arg;
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ repeat:;
|
|||||||
PgfExprAbs *eabs =
|
PgfExprAbs *eabs =
|
||||||
gu_new_variant(PGF_EXPR_ABS,
|
gu_new_variant(PGF_EXPR_ABS,
|
||||||
PgfExprAbs,
|
PgfExprAbs,
|
||||||
&new_expr, state->pool);
|
&new_expr, rs->pool);
|
||||||
eabs->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
eabs->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
||||||
eabs->id = "_";
|
eabs->id = "_";
|
||||||
eabs->body = expr;
|
eabs->body = expr;
|
||||||
@@ -162,7 +162,7 @@ repeat:;
|
|||||||
while (i > 0) {
|
while (i > 0) {
|
||||||
tmp_env = tmp_env->next;
|
tmp_env = tmp_env->next;
|
||||||
if (tmp_env == NULL) {
|
if (tmp_env == NULL) {
|
||||||
GuExnData* err_data = gu_raise(state->err, PgfExn);
|
GuExnData* err_data = gu_raise(rs->err, PgfExn);
|
||||||
if (err_data) {
|
if (err_data) {
|
||||||
err_data->data = "invalid de Bruijn index";
|
err_data->data = "invalid de Bruijn index";
|
||||||
}
|
}
|
||||||
@@ -171,7 +171,7 @@ repeat:;
|
|||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = pgf_mk_pap(state, tmp_env->closure, n_args, args);
|
res = pgf_mk_pap(rs, tmp_env->closure, n_args, args);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PGF_EXPR_TYPED: {
|
case PGF_EXPR_TYPED: {
|
||||||
@@ -193,24 +193,24 @@ repeat:;
|
|||||||
}
|
}
|
||||||
|
|
||||||
PgfClosure*
|
PgfClosure*
|
||||||
pgf_evaluate_lambda_application(PgfEvalState* state, PgfExprThunk* lambda,
|
pgf_evaluate_lambda_application(PgfReasoner* rs, PgfExprThunk* lambda,
|
||||||
PgfClosure* arg)
|
PgfClosure* arg)
|
||||||
{
|
{
|
||||||
PgfEnv* new_env = gu_new(PgfEnv, state->pool);
|
PgfEnv* new_env = gu_new(PgfEnv, rs->pool);
|
||||||
new_env->next = lambda->env;
|
new_env->next = lambda->env;
|
||||||
new_env->closure = arg;
|
new_env->closure = arg;
|
||||||
|
|
||||||
PgfExprThunk* thunk = gu_new(PgfExprThunk, state->pool);
|
PgfExprThunk* thunk = gu_new(PgfExprThunk, rs->pool);
|
||||||
thunk->header.code = state->eval_gates->evaluate_expr_thunk;
|
thunk->header.code = rs->eval_gates->evaluate_expr_thunk;
|
||||||
thunk->env = new_env;
|
thunk->env = new_env;
|
||||||
thunk->expr = lambda->expr;
|
thunk->expr = lambda->expr;
|
||||||
return pgf_evaluate_expr_thunk(state, thunk);
|
return pgf_evaluate_expr_thunk(rs, thunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PgfExpr
|
static PgfExpr
|
||||||
pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
pgf_value2expr(PgfReasoner* rs, int level, PgfClosure* clos)
|
||||||
{
|
{
|
||||||
clos = state->eval_gates->enter(state, clos);
|
clos = rs->eval_gates->enter(rs, clos);
|
||||||
if (clos == NULL)
|
if (clos == NULL)
|
||||||
return gu_null_variant;
|
return gu_null_variant;
|
||||||
|
|
||||||
@@ -218,20 +218,20 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
size_t n_args = 0;
|
size_t n_args = 0;
|
||||||
PgfClosure** args;
|
PgfClosure** args;
|
||||||
|
|
||||||
if (clos->code == state->eval_gates->evaluate_value) {
|
if (clos->code == rs->eval_gates->evaluate_value) {
|
||||||
PgfValue* val = (PgfValue*) clos;
|
PgfValue* val = (PgfValue*) clos;
|
||||||
PgfAbsFun* absfun = gu_container(val->con, PgfAbsFun, closure);
|
PgfAbsFun* absfun = gu_container(val->con, PgfAbsFun, closure);
|
||||||
|
|
||||||
expr = absfun->ep.expr;
|
expr = absfun->ep.expr;
|
||||||
n_args = absfun->arity;
|
n_args = absfun->arity;
|
||||||
args = val->args;
|
args = val->args;
|
||||||
} else if (clos->code == state->eval_gates->evaluate_value_lit) {
|
} else if (clos->code == rs->eval_gates->evaluate_value_lit) {
|
||||||
PgfValueLit* val = (PgfValueLit*) clos;
|
PgfValueLit* val = (PgfValueLit*) clos;
|
||||||
|
|
||||||
PgfExprLit *elit =
|
PgfExprLit *elit =
|
||||||
gu_new_variant(PGF_EXPR_LIT,
|
gu_new_variant(PGF_EXPR_LIT,
|
||||||
PgfExprLit,
|
PgfExprLit,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
|
|
||||||
GuVariantInfo i = gu_variant_open(val->lit);
|
GuVariantInfo i = gu_variant_open(val->lit);
|
||||||
switch (i.tag) {
|
switch (i.tag) {
|
||||||
@@ -242,7 +242,7 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
gu_new_flex_variant(PGF_LITERAL_STR,
|
gu_new_flex_variant(PGF_LITERAL_STR,
|
||||||
PgfLiteralStr,
|
PgfLiteralStr,
|
||||||
val, strlen(lstr->val)+1,
|
val, strlen(lstr->val)+1,
|
||||||
&elit->lit, pool);
|
&elit->lit, rs->out_pool);
|
||||||
strcpy(new_lstr->val, lstr->val);
|
strcpy(new_lstr->val, lstr->val);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -252,7 +252,7 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
PgfLiteralInt* new_lint =
|
PgfLiteralInt* new_lint =
|
||||||
gu_new_variant(PGF_LITERAL_INT,
|
gu_new_variant(PGF_LITERAL_INT,
|
||||||
PgfLiteralInt,
|
PgfLiteralInt,
|
||||||
&elit->lit, pool);
|
&elit->lit, rs->out_pool);
|
||||||
new_lint->val = lint->val;
|
new_lint->val = lint->val;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -262,24 +262,24 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
PgfLiteralFlt* new_lflt =
|
PgfLiteralFlt* new_lflt =
|
||||||
gu_new_variant(PGF_LITERAL_FLT,
|
gu_new_variant(PGF_LITERAL_FLT,
|
||||||
PgfLiteralFlt,
|
PgfLiteralFlt,
|
||||||
&elit->lit, pool);
|
&elit->lit, rs->out_pool);
|
||||||
new_lflt->val = lflt->val;
|
new_lflt->val = lflt->val;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
gu_impossible();
|
gu_impossible();
|
||||||
}
|
}
|
||||||
} else if (clos->code == state->eval_gates->evaluate_value_pap) {
|
} else if (clos->code == rs->eval_gates->evaluate_value_pap) {
|
||||||
PgfValuePAP *pap = (PgfValuePAP*) clos;
|
PgfValuePAP *pap = (PgfValuePAP*) clos;
|
||||||
|
|
||||||
PgfValueGen* gen =
|
PgfValueGen* gen =
|
||||||
gu_new(PgfValueGen, state->pool);
|
gu_new(PgfValueGen, rs->pool);
|
||||||
gen->header.code = state->eval_gates->evaluate_gen;
|
gen->header.code = rs->eval_gates->evaluate_gen;
|
||||||
gen->level = level;
|
gen->level = level;
|
||||||
|
|
||||||
size_t n_args = pap->n_args/sizeof(PgfClosure*);
|
size_t n_args = pap->n_args/sizeof(PgfClosure*);
|
||||||
PgfValuePAP* new_pap = gu_new_flex(state->pool, PgfValuePAP, args, n_args+1);
|
PgfValuePAP* new_pap = gu_new_flex(rs->pool, PgfValuePAP, args, n_args+1);
|
||||||
new_pap->header.code = state->eval_gates->evaluate_value_pap;
|
new_pap->header.code = rs->eval_gates->evaluate_value_pap;
|
||||||
new_pap->fun = pap->fun;
|
new_pap->fun = pap->fun;
|
||||||
new_pap->n_args = pap->n_args+sizeof(PgfClosure*);
|
new_pap->n_args = pap->n_args+sizeof(PgfClosure*);
|
||||||
new_pap->args[0] = &gen->header;
|
new_pap->args[0] = &gen->header;
|
||||||
@@ -290,30 +290,30 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
PgfExprAbs *eabs =
|
PgfExprAbs *eabs =
|
||||||
gu_new_variant(PGF_EXPR_ABS,
|
gu_new_variant(PGF_EXPR_ABS,
|
||||||
PgfExprAbs,
|
PgfExprAbs,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
eabs->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
eabs->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
||||||
eabs->id = gu_format_string(pool, "v%d", level);
|
eabs->id = gu_format_string(rs->out_pool, "v%d", level);
|
||||||
eabs->body = pgf_value2expr(state, level+1, &new_pap->header, pool);
|
eabs->body = pgf_value2expr(rs, level+1, &new_pap->header);
|
||||||
} else if (clos->code == state->eval_gates->evaluate_value_const) {
|
} else if (clos->code == rs->eval_gates->evaluate_value_const) {
|
||||||
PgfValuePAP* val = (PgfValuePAP*) clos;
|
PgfValuePAP* val = (PgfValuePAP*) clos;
|
||||||
|
|
||||||
if (val->fun->code == state->eval_gates->evaluate_meta) {
|
if (val->fun->code == rs->eval_gates->evaluate_meta) {
|
||||||
PgfValueMeta* fun = (PgfValueMeta*) val->fun;
|
PgfValueMeta* fun = (PgfValueMeta*) val->fun;
|
||||||
|
|
||||||
PgfExprMeta *emeta =
|
PgfExprMeta *emeta =
|
||||||
gu_new_variant(PGF_EXPR_META,
|
gu_new_variant(PGF_EXPR_META,
|
||||||
PgfExprMeta,
|
PgfExprMeta,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
emeta->id = fun->id;
|
emeta->id = fun->id;
|
||||||
} else if (val->fun->code == state->eval_gates->evaluate_gen) {
|
} else if (val->fun->code == rs->eval_gates->evaluate_gen) {
|
||||||
PgfValueGen* fun = (PgfValueGen*) val->fun;
|
PgfValueGen* fun = (PgfValueGen*) val->fun;
|
||||||
|
|
||||||
PgfExprVar *evar =
|
PgfExprVar *evar =
|
||||||
gu_new_variant(PGF_EXPR_VAR,
|
gu_new_variant(PGF_EXPR_VAR,
|
||||||
PgfExprVar,
|
PgfExprVar,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
evar->var = level - fun->level - 1;
|
evar->var = level - fun->level - 1;
|
||||||
} else if (val->fun->code == state->eval_gates->evaluate_sum) {
|
} else if (val->fun->code == rs->eval_gates->evaluate_sum) {
|
||||||
PgfValueSum* sum = (PgfValueSum*) val->fun;
|
PgfValueSum* sum = (PgfValueSum*) val->fun;
|
||||||
|
|
||||||
PgfExpr e1,e2;
|
PgfExpr e1,e2;
|
||||||
@@ -321,19 +321,19 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
gu_new_flex_variant(PGF_EXPR_FUN,
|
gu_new_flex_variant(PGF_EXPR_FUN,
|
||||||
PgfExprFun,
|
PgfExprFun,
|
||||||
fun, 2,
|
fun, 2,
|
||||||
&e1, pool);
|
&e1, rs->out_pool);
|
||||||
strcpy(efun->fun, "+");
|
strcpy(efun->fun, "+");
|
||||||
|
|
||||||
PgfExprLit *elit =
|
PgfExprLit *elit =
|
||||||
gu_new_variant(PGF_EXPR_LIT,
|
gu_new_variant(PGF_EXPR_LIT,
|
||||||
PgfExprLit,
|
PgfExprLit,
|
||||||
&e2, pool);
|
&e2, rs->out_pool);
|
||||||
elit->lit = sum->lit;
|
elit->lit = sum->lit;
|
||||||
|
|
||||||
PgfExprApp* eapp =
|
PgfExprApp* eapp =
|
||||||
gu_new_variant(PGF_EXPR_APP,
|
gu_new_variant(PGF_EXPR_APP,
|
||||||
PgfExprApp,
|
PgfExprApp,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
eapp->fun = e1;
|
eapp->fun = e1;
|
||||||
eapp->arg = e2;
|
eapp->arg = e2;
|
||||||
|
|
||||||
@@ -344,14 +344,14 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
|
|
||||||
PgfExpr fun = expr;
|
PgfExpr fun = expr;
|
||||||
PgfExpr arg =
|
PgfExpr arg =
|
||||||
pgf_value2expr(state, level, con, pool);
|
pgf_value2expr(rs, level, con);
|
||||||
if (gu_variant_is_null(arg))
|
if (gu_variant_is_null(arg))
|
||||||
return gu_null_variant;
|
return gu_null_variant;
|
||||||
|
|
||||||
PgfExprApp* e =
|
PgfExprApp* e =
|
||||||
gu_new_variant(PGF_EXPR_APP,
|
gu_new_variant(PGF_EXPR_APP,
|
||||||
PgfExprApp,
|
PgfExprApp,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
e->fun = fun;
|
e->fun = fun;
|
||||||
e->arg = arg;
|
e->arg = arg;
|
||||||
}
|
}
|
||||||
@@ -369,14 +369,14 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
for (size_t i = 0; i < n_args; i++) {
|
for (size_t i = 0; i < n_args; i++) {
|
||||||
PgfExpr fun = expr;
|
PgfExpr fun = expr;
|
||||||
PgfExpr arg =
|
PgfExpr arg =
|
||||||
pgf_value2expr(state, level, args[i], pool);
|
pgf_value2expr(rs, level, args[i]);
|
||||||
if (gu_variant_is_null(arg))
|
if (gu_variant_is_null(arg))
|
||||||
return gu_null_variant;
|
return gu_null_variant;
|
||||||
|
|
||||||
PgfExprApp* e =
|
PgfExprApp* e =
|
||||||
gu_new_variant(PGF_EXPR_APP,
|
gu_new_variant(PGF_EXPR_APP,
|
||||||
PgfExprApp,
|
PgfExprApp,
|
||||||
&expr, pool);
|
&expr, rs->out_pool);
|
||||||
e->fun = fun;
|
e->fun = fun;
|
||||||
e->arg = arg;
|
e->arg = arg;
|
||||||
}
|
}
|
||||||
@@ -387,47 +387,33 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
|||||||
PgfExpr
|
PgfExpr
|
||||||
pgf_compute(PgfPGF* pgf, PgfExpr expr, GuExn* err, GuPool* pool, GuPool* out_pool)
|
pgf_compute(PgfPGF* pgf, PgfExpr expr, GuExn* err, GuPool* pool, GuPool* out_pool)
|
||||||
{
|
{
|
||||||
size_t n_cafs =
|
PgfReasoner* rs =
|
||||||
(pgf->abstract.eval_gates->cafs == NULL)
|
pgf_new_reasoner(pgf, err, pool, out_pool);
|
||||||
? 0 : gu_seq_length(pgf->abstract.eval_gates->cafs);
|
|
||||||
|
|
||||||
PgfEvalState* state =
|
|
||||||
gu_new_flex(pool, PgfEvalState, cafs, n_cafs);
|
|
||||||
state->pgf = pgf;
|
|
||||||
state->eval_gates = pgf->abstract.eval_gates;
|
|
||||||
state->pool = pool;
|
|
||||||
state->err = err;
|
|
||||||
|
|
||||||
PgfFunction* cafs = gu_seq_data(state->eval_gates->cafs);
|
|
||||||
for (size_t i = 0; i < n_cafs; i++) {
|
|
||||||
state->cafs[i].header.code = cafs[i];
|
|
||||||
state->cafs[i].val = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
PgfExprThunk* thunk =
|
PgfExprThunk* thunk =
|
||||||
gu_new(PgfExprThunk, pool);
|
gu_new(PgfExprThunk, pool);
|
||||||
thunk->header.code = state->eval_gates->evaluate_expr_thunk;
|
thunk->header.code = rs->eval_gates->evaluate_expr_thunk;
|
||||||
thunk->env = NULL;
|
thunk->env = NULL;
|
||||||
thunk->expr = expr;
|
thunk->expr = expr;
|
||||||
|
|
||||||
return pgf_value2expr(state, 0, &thunk->header, out_pool);
|
return pgf_value2expr(rs, 0, &thunk->header);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pgf_evaluate_accum_init_int(PgfEvalState* state,
|
pgf_evaluate_accum_init_int(PgfReasoner* rs,
|
||||||
PgfEvalAccum* accum, int val)
|
PgfEvalAccum* accum, int val)
|
||||||
{
|
{
|
||||||
PgfLiteralInt *lit_int =
|
PgfLiteralInt *lit_int =
|
||||||
gu_new_variant(PGF_LITERAL_INT,
|
gu_new_variant(PGF_LITERAL_INT,
|
||||||
PgfLiteralInt,
|
PgfLiteralInt,
|
||||||
&accum->lit,
|
&accum->lit,
|
||||||
state->pool);
|
rs->pool);
|
||||||
lit_int->val = val;
|
lit_int->val = val;
|
||||||
accum->consts = NULL;
|
accum->consts = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pgf_evaluate_accum_init_str(PgfEvalState* state,
|
pgf_evaluate_accum_init_str(PgfReasoner* rs,
|
||||||
PgfEvalAccum* accum, GuString val)
|
PgfEvalAccum* accum, GuString val)
|
||||||
{
|
{
|
||||||
if (val == NULL)
|
if (val == NULL)
|
||||||
@@ -437,23 +423,23 @@ pgf_evaluate_accum_init_str(PgfEvalState* state,
|
|||||||
gu_new_flex_variant(PGF_LITERAL_STR,
|
gu_new_flex_variant(PGF_LITERAL_STR,
|
||||||
PgfLiteralStr,
|
PgfLiteralStr,
|
||||||
val, strlen(val)+1,
|
val, strlen(val)+1,
|
||||||
&accum->lit, state->pool);
|
&accum->lit, rs->pool);
|
||||||
strcpy((char*) lit_str->val, (char*) val);
|
strcpy((char*) lit_str->val, (char*) val);
|
||||||
accum->consts = NULL;
|
accum->consts = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pgf_evaluate_accum_init_flt(PgfEvalState* state,
|
pgf_evaluate_accum_init_flt(PgfReasoner* rs,
|
||||||
PgfEvalAccum* accum, float val)
|
PgfEvalAccum* accum, float val)
|
||||||
{
|
{
|
||||||
PgfLiteralFlt *lit_flt =
|
PgfLiteralFlt *lit_flt =
|
||||||
gu_new_variant(PGF_LITERAL_FLT,
|
gu_new_variant(PGF_LITERAL_FLT,
|
||||||
PgfLiteralFlt,
|
PgfLiteralFlt,
|
||||||
&accum->lit,
|
&accum->lit,
|
||||||
state->pool);
|
rs->pool);
|
||||||
lit_flt->val = val;
|
lit_flt->val = val;
|
||||||
accum->enter_stack_ptr = state->enter_stack_ptr;
|
accum->enter_stack_ptr = rs->enter_stack_ptr;
|
||||||
state->enter_stack_ptr = ((void*)accum)-sizeof(void*)*2;
|
rs->enter_stack_ptr = ((void*)accum)-sizeof(void*)*2;
|
||||||
accum->consts = NULL;
|
accum->consts = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -480,19 +466,19 @@ pgf_evaluate_accum_add_helper(PgfEvalAccum* accum, PgfLiteral lit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pgf_evaluate_accum_add(PgfEvalState* state,
|
pgf_evaluate_accum_add(PgfReasoner* rs,
|
||||||
PgfEvalAccum* accum, PgfClosure* closure)
|
PgfEvalAccum* accum, PgfClosure* closure)
|
||||||
{
|
{
|
||||||
if (closure->code == state->eval_gates->evaluate_value_lit) {
|
if (closure->code == rs->eval_gates->evaluate_value_lit) {
|
||||||
PgfValueLit* val = (PgfValueLit*) closure;
|
PgfValueLit* val = (PgfValueLit*) closure;
|
||||||
pgf_evaluate_accum_add_helper(accum, val->lit);
|
pgf_evaluate_accum_add_helper(accum, val->lit);
|
||||||
} else if (closure->code == state->eval_gates->evaluate_value_const) {
|
} else if (closure->code == rs->eval_gates->evaluate_value_const) {
|
||||||
if (accum->consts == NULL)
|
if (accum->consts == NULL)
|
||||||
accum->consts = gu_new_buf(PgfClosure*, state->pool);
|
accum->consts = gu_new_buf(PgfClosure*, rs->pool);
|
||||||
|
|
||||||
PgfValuePAP* pap = (PgfValuePAP*) closure;
|
PgfValuePAP* pap = (PgfValuePAP*) closure;
|
||||||
|
|
||||||
if (pap->fun->code == state->eval_gates->evaluate_sum) {
|
if (pap->fun->code == rs->eval_gates->evaluate_sum) {
|
||||||
PgfValueSum* val = (PgfValueSum*) ((PgfValuePAP*) closure)->fun;
|
PgfValueSum* val = (PgfValueSum*) ((PgfValuePAP*) closure)->fun;
|
||||||
pgf_evaluate_accum_add_helper(accum, val->lit);
|
pgf_evaluate_accum_add_helper(accum, val->lit);
|
||||||
|
|
||||||
@@ -510,18 +496,18 @@ pgf_evaluate_accum_add(PgfEvalState* state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
PgfClosure*
|
PgfClosure*
|
||||||
pgf_evaluate_accum_done(PgfEvalState* state, PgfEvalAccum* accum)
|
pgf_evaluate_accum_done(PgfReasoner* rs, PgfEvalAccum* accum)
|
||||||
{
|
{
|
||||||
state->enter_stack_ptr = accum->enter_stack_ptr;
|
rs->enter_stack_ptr = accum->enter_stack_ptr;
|
||||||
|
|
||||||
if (accum->consts == NULL) {
|
if (accum->consts == NULL) {
|
||||||
PgfValueLit* val = gu_new(PgfValueLit, state->pool);
|
PgfValueLit* val = gu_new(PgfValueLit, rs->pool);
|
||||||
val->header.code = state->eval_gates->evaluate_value_lit;
|
val->header.code = rs->eval_gates->evaluate_value_lit;
|
||||||
val->lit = accum->lit;
|
val->lit = accum->lit;
|
||||||
return &val->header;
|
return &val->header;
|
||||||
} else {
|
} else {
|
||||||
PgfValueSum* val = gu_new(PgfValueSum, state->pool);
|
PgfValueSum* val = gu_new(PgfValueSum, rs->pool);
|
||||||
val->header.code = state->eval_gates->evaluate_sum;
|
val->header.code = rs->eval_gates->evaluate_sum;
|
||||||
val->lit = accum->lit;
|
val->lit = accum->lit;
|
||||||
val->consts = accum->consts;
|
val->consts = accum->consts;
|
||||||
return &val->header;
|
return &val->header;
|
||||||
|
|||||||
@@ -1,127 +0,0 @@
|
|||||||
#ifndef PGF_EVALUATOR_H_
|
|
||||||
#define PGF_EVALUATOR_H_
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfFunction code;
|
|
||||||
} PgfClosure;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfClosure* val;
|
|
||||||
} PgfIndirection;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfPGF* pgf;
|
|
||||||
PgfEvalGates* eval_gates; // cached from pgf->abstr->eval_gates
|
|
||||||
GuPool* pool;
|
|
||||||
GuExn* err;
|
|
||||||
void* enter_stack_ptr;
|
|
||||||
void* tmp; // for temporary register spills
|
|
||||||
PgfIndirection cafs[]; // derived from gu_seq_data(pgf->abstr->eval_gates->cafs)
|
|
||||||
} PgfEvalState;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfLiteral lit;
|
|
||||||
GuBuf* consts;
|
|
||||||
void* enter_stack_ptr;
|
|
||||||
} PgfEvalAccum;
|
|
||||||
|
|
||||||
typedef struct PgfEnv PgfEnv;
|
|
||||||
|
|
||||||
struct PgfEnv {
|
|
||||||
PgfEnv* next;
|
|
||||||
PgfClosure* closure;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfEnv* env;
|
|
||||||
PgfExpr expr;
|
|
||||||
} PgfExprThunk;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfClosure* con;
|
|
||||||
PgfClosure* args[];
|
|
||||||
} PgfValue;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
int level;
|
|
||||||
} PgfValueGen;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfEnv* env;
|
|
||||||
PgfMetaId id;
|
|
||||||
} PgfValueMeta;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfLiteral lit;
|
|
||||||
} PgfValueLit;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfLiteral lit;
|
|
||||||
GuBuf* consts;
|
|
||||||
} PgfValueSum;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
PgfClosure header;
|
|
||||||
PgfClosure* fun;
|
|
||||||
size_t n_args;
|
|
||||||
PgfClosure* args[];
|
|
||||||
} PgfValuePAP;
|
|
||||||
|
|
||||||
struct PgfEvalGates {
|
|
||||||
PgfFunction evaluate_expr_thunk;
|
|
||||||
PgfFunction evaluate_indirection;
|
|
||||||
PgfFunction evaluate_value;
|
|
||||||
PgfFunction evaluate_value_lit;
|
|
||||||
PgfFunction evaluate_value_pap;
|
|
||||||
PgfFunction evaluate_value_lambda;
|
|
||||||
PgfFunction evaluate_value_const;
|
|
||||||
PgfFunction evaluate_meta;
|
|
||||||
PgfFunction evaluate_gen;
|
|
||||||
PgfFunction evaluate_sum;
|
|
||||||
PgfFunction evaluate_caf;
|
|
||||||
|
|
||||||
PgfFunction update_closure;
|
|
||||||
PgfFunction update_pap;
|
|
||||||
|
|
||||||
PgfFunction mk_const;
|
|
||||||
|
|
||||||
PgfClosure* (*enter)(PgfEvalState* state, PgfClosure* closure);
|
|
||||||
|
|
||||||
GuFinalizer fin;
|
|
||||||
GuSeq* cafs;
|
|
||||||
};
|
|
||||||
|
|
||||||
PgfClosure*
|
|
||||||
pgf_evaluate_expr_thunk(PgfEvalState* state, PgfExprThunk* thunk);
|
|
||||||
|
|
||||||
PgfClosure*
|
|
||||||
pgf_evaluate_lambda_application(PgfEvalState* state, PgfExprThunk* lambda,
|
|
||||||
PgfClosure* arg);
|
|
||||||
|
|
||||||
void
|
|
||||||
pgf_evaluate_accum_init_int(PgfEvalState* state,
|
|
||||||
PgfEvalAccum* accum, int val);
|
|
||||||
|
|
||||||
void
|
|
||||||
pgf_evaluate_accum_init_str(PgfEvalState* state,
|
|
||||||
PgfEvalAccum* accum, GuString val);
|
|
||||||
|
|
||||||
void
|
|
||||||
pgf_evaluate_accum_init_flt(PgfEvalState* state,
|
|
||||||
PgfEvalAccum* accum, float val);
|
|
||||||
|
|
||||||
void
|
|
||||||
pgf_evaluate_accum_add(PgfEvalState* state,
|
|
||||||
PgfEvalAccum* accum, PgfClosure* closure);
|
|
||||||
|
|
||||||
PgfClosure*
|
|
||||||
pgf_evaluate_accum_done(PgfEvalState* state, PgfEvalAccum* accum);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "evaluator.h"
|
#include "reasoner.h"
|
||||||
#include "hopu.h"
|
#include "hopu.h"
|
||||||
|
|
||||||
void pgf_pattern_unify(PgfEvalState* state, PgfClosure* c1, PgfClosure* c2)
|
void pgf_pattern_unify(PgfReasoner* rs, PgfClosure* c1, PgfClosure* c2)
|
||||||
{
|
{
|
||||||
c1 = state->eval_gates->enter(state, c1);
|
c1 = rs->eval_gates->enter(rs, c1);
|
||||||
c2 = state->eval_gates->enter(state, c2);
|
c2 = rs->eval_gates->enter(rs, c2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef HOPU_H
|
#ifndef HOPU_H
|
||||||
#define HOPU_H
|
#define HOPU_H
|
||||||
|
|
||||||
void pgf_pattern_unify(PgfEvalState* state, PgfClosure* c1, PgfClosure* c2);
|
void pgf_pattern_unify(PgfReasoner* rs, PgfClosure* c1, PgfClosure* c2);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <gu/utf8.h>
|
#include <gu/utf8.h>
|
||||||
#include <pgf/data.h>
|
#include <pgf/data.h>
|
||||||
#include <pgf/reasoner.h>
|
#include <pgf/reasoner.h>
|
||||||
#include <pgf/evaluator.h>
|
|
||||||
#include <pgf/reader.h>
|
#include <pgf/reader.h>
|
||||||
#include "lightning.h"
|
#include "lightning.h"
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
@@ -387,7 +386,7 @@ pgf_jit_gates(PgfReader* rdr)
|
|||||||
int closure_arg = jit_arg_p();
|
int closure_arg = jit_arg_p();
|
||||||
jit_getarg_p(JIT_VSTATE, es_arg);
|
jit_getarg_p(JIT_VSTATE, es_arg);
|
||||||
jit_getarg_p(JIT_VCLOS, closure_arg);
|
jit_getarg_p(JIT_VCLOS, closure_arg);
|
||||||
jit_stxi_p(offsetof(PgfEvalState, enter_stack_ptr), JIT_VSTATE, JIT_SP);
|
jit_stxi_p(offsetof(PgfReasoner, enter_stack_ptr), JIT_VSTATE, JIT_SP);
|
||||||
jit_ldr_p(JIT_R0, JIT_VCLOS);
|
jit_ldr_p(JIT_R0, JIT_VCLOS);
|
||||||
jit_callr(JIT_R0);
|
jit_callr(JIT_R0);
|
||||||
jit_movr_p(JIT_RET, JIT_VHEAP);
|
jit_movr_p(JIT_RET, JIT_VHEAP);
|
||||||
@@ -430,7 +429,7 @@ pgf_jit_gates(PgfReader* rdr)
|
|||||||
jit_prepare(2);
|
jit_prepare(2);
|
||||||
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
||||||
jit_pusharg_ui(JIT_R0);
|
jit_pusharg_ui(JIT_R0);
|
||||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||||
jit_pusharg_p(JIT_R0);
|
jit_pusharg_p(JIT_R0);
|
||||||
jit_finish(gu_malloc);
|
jit_finish(gu_malloc);
|
||||||
jit_popr_i(JIT_R1);
|
jit_popr_i(JIT_R1);
|
||||||
@@ -531,7 +530,7 @@ pgf_jit_gates(PgfReader* rdr)
|
|||||||
jit_prepare(2);
|
jit_prepare(2);
|
||||||
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
||||||
jit_pusharg_ui(JIT_R0);
|
jit_pusharg_ui(JIT_R0);
|
||||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||||
jit_pusharg_p(JIT_R0);
|
jit_pusharg_p(JIT_R0);
|
||||||
jit_finish(gu_malloc);
|
jit_finish(gu_malloc);
|
||||||
jit_popr_i(JIT_R1);
|
jit_popr_i(JIT_R1);
|
||||||
@@ -563,15 +562,15 @@ pgf_jit_gates(PgfReader* rdr)
|
|||||||
jit_pushr_p(JIT_R0);
|
jit_pushr_p(JIT_R0);
|
||||||
jit_jmpi(gates->mk_const);
|
jit_jmpi(gates->mk_const);
|
||||||
jit_patch(ref2);
|
jit_patch(ref2);
|
||||||
jit_ldxi_p(JIT_R1, JIT_VSTATE, offsetof(PgfEvalState,enter_stack_ptr));
|
jit_ldxi_p(JIT_R1, JIT_VSTATE, offsetof(PgfReasoner,enter_stack_ptr));
|
||||||
ref2 = jit_bner_p(jit_forward(), JIT_FP, JIT_R1);
|
ref2 = jit_bner_p(jit_forward(), JIT_FP, JIT_R1);
|
||||||
jit_stxi_p(offsetof(PgfEvalState,tmp), JIT_VSTATE, JIT_R0);
|
jit_stxi_p(offsetof(PgfReasoner,tmp), JIT_VSTATE, JIT_R0);
|
||||||
jit_subr_p(JIT_R0, JIT_FP, JIT_SP);
|
jit_subr_p(JIT_R0, JIT_FP, JIT_SP);
|
||||||
jit_pushr_i(JIT_R0);
|
jit_pushr_i(JIT_R0);
|
||||||
jit_prepare(2);
|
jit_prepare(2);
|
||||||
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
||||||
jit_pusharg_ui(JIT_R0);
|
jit_pusharg_ui(JIT_R0);
|
||||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||||
jit_pusharg_p(JIT_R0);
|
jit_pusharg_p(JIT_R0);
|
||||||
jit_finish(gu_malloc);
|
jit_finish(gu_malloc);
|
||||||
jit_movr_p(JIT_VHEAP, JIT_RET);
|
jit_movr_p(JIT_VHEAP, JIT_RET);
|
||||||
@@ -588,7 +587,7 @@ pgf_jit_gates(PgfReader* rdr)
|
|||||||
jit_subi_i(JIT_R1, JIT_R1, sizeof(void*));
|
jit_subi_i(JIT_R1, JIT_R1, sizeof(void*));
|
||||||
jit_jmpi(next);
|
jit_jmpi(next);
|
||||||
jit_patch(ref);
|
jit_patch(ref);
|
||||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,tmp));
|
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,tmp));
|
||||||
jit_jmpr(JIT_R0);
|
jit_jmpr(JIT_R0);
|
||||||
jit_patch(ref2);
|
jit_patch(ref2);
|
||||||
jit_ldxi_p(JIT_VCLOS, JIT_FP, sizeof(void*));
|
jit_ldxi_p(JIT_VCLOS, JIT_FP, sizeof(void*));
|
||||||
@@ -662,7 +661,7 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
|||||||
absfun->closure.code = abstr->eval_gates->evaluate_caf;
|
absfun->closure.code = abstr->eval_gates->evaluate_caf;
|
||||||
size_t caf_id = rdr->jit_state->n_cafs++;
|
size_t caf_id = rdr->jit_state->n_cafs++;
|
||||||
absfun->closure.caf_offset =
|
absfun->closure.caf_offset =
|
||||||
offsetof(PgfEvalState,cafs)+
|
offsetof(PgfReasoner,cafs)+
|
||||||
caf_id*sizeof(PgfIndirection);
|
caf_id*sizeof(PgfIndirection);
|
||||||
gu_seq_set(abstr->eval_gates->cafs,
|
gu_seq_set(abstr->eval_gates->cafs,
|
||||||
PgfFunction,
|
PgfFunction,
|
||||||
@@ -807,7 +806,7 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
|||||||
jit_prepare(2);
|
jit_prepare(2);
|
||||||
jit_movi_ui(JIT_R0, size*sizeof(void*));
|
jit_movi_ui(JIT_R0, size*sizeof(void*));
|
||||||
jit_pusharg_ui(JIT_R0);
|
jit_pusharg_ui(JIT_R0);
|
||||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||||
jit_pusharg_p(JIT_R0);
|
jit_pusharg_p(JIT_R0);
|
||||||
jit_finish(gu_malloc);
|
jit_finish(gu_malloc);
|
||||||
jit_retval_p(JIT_VHEAP);
|
jit_retval_p(JIT_VHEAP);
|
||||||
|
|||||||
@@ -94,12 +94,12 @@ pgf_compute(PgfPGF* pgf, PgfExpr expr, GuExn* err,
|
|||||||
GuPool* pool, GuPool* out_pool);
|
GuPool* pool, GuPool* out_pool);
|
||||||
|
|
||||||
PgfExprEnum*
|
PgfExprEnum*
|
||||||
pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuPool* pool);
|
pgf_generate_all(PgfPGF* pgf, PgfCId cat,
|
||||||
|
GuExn* err, GuPool* pool, GuPool* out_pool);
|
||||||
|
|
||||||
PgfExprEnum*
|
PgfExprEnum*
|
||||||
pgf_parse(PgfConcr* concr, PgfCId cat, GuString sentence,
|
pgf_parse(PgfConcr* concr, PgfCId cat, GuString sentence,
|
||||||
GuExn* err,
|
GuExn* err, GuPool* pool, GuPool* out_pool);
|
||||||
GuPool* pool, GuPool* out_pool);
|
|
||||||
|
|
||||||
typedef struct PgfMorphoCallback PgfMorphoCallback;
|
typedef struct PgfMorphoCallback PgfMorphoCallback;
|
||||||
struct PgfMorphoCallback {
|
struct PgfMorphoCallback {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
#include "evaluator.h"
|
#include "reasoner.h"
|
||||||
#include "reader.h"
|
#include "reader.h"
|
||||||
|
|
||||||
#include <gu/defs.h>
|
#include <gu/defs.h>
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ pgf_reader_done(PgfReader* rdr, PgfPGF* pgf);
|
|||||||
|
|
||||||
|
|
||||||
// JIT specific interface
|
// JIT specific interface
|
||||||
#ifdef PGF_EVALUATOR_H_
|
#ifdef PGF_REASONER_H_
|
||||||
|
|
||||||
typedef struct PgfJitState PgfJitState;
|
typedef struct PgfJitState PgfJitState;
|
||||||
|
|
||||||
|
|||||||
@@ -58,18 +58,6 @@ typedef struct {
|
|||||||
size_t choice;
|
size_t choice;
|
||||||
} PgfCombine2State;
|
} PgfCombine2State;
|
||||||
|
|
||||||
typedef GuStringMap PgfAbswersMap;
|
|
||||||
|
|
||||||
struct PgfReasoner {
|
|
||||||
GuPool* pool;
|
|
||||||
GuPool* tmp_pool;
|
|
||||||
PgfAbstr* abstract;
|
|
||||||
PgfAbswersMap* table;
|
|
||||||
GuBuf* pqueue;
|
|
||||||
GuBuf* exprs;
|
|
||||||
PgfExprEnum en;
|
|
||||||
};
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
cmp_expr_state(GuOrder* self, const void* a, const void* b)
|
cmp_expr_state(GuOrder* self, const void* a, const void* b)
|
||||||
{
|
{
|
||||||
@@ -136,16 +124,16 @@ pgf_print_expr_state(PgfExprState* st,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static PgfExprState*
|
static PgfExprState*
|
||||||
pgf_combine1_to_expr(PgfCombine1State* st, GuPool* tmp_pool) {
|
pgf_combine1_to_expr(PgfCombine1State* st, GuPool* pool, GuPool* out_pool) {
|
||||||
PgfExprProb* ep =
|
PgfExprProb* ep =
|
||||||
gu_buf_get(st->exprs, PgfExprProb*, st->choice);
|
gu_buf_get(st->exprs, PgfExprProb*, st->choice);
|
||||||
|
|
||||||
PgfExprState* nst = gu_new(PgfExprState, tmp_pool);
|
PgfExprState* nst = gu_new(PgfExprState, pool);
|
||||||
nst->base.continuation = st->parent->base.continuation;
|
nst->base.continuation = st->parent->base.continuation;
|
||||||
nst->base.prob = st->base.prob;
|
nst->base.prob = st->base.prob;
|
||||||
nst->answers = st->parent->answers;
|
nst->answers = st->parent->answers;
|
||||||
nst->expr =
|
nst->expr =
|
||||||
gu_new_variant_i(tmp_pool, PGF_EXPR_APP,
|
gu_new_variant_i(out_pool, PGF_EXPR_APP,
|
||||||
PgfExprApp,
|
PgfExprApp,
|
||||||
.fun = st->parent->expr,
|
.fun = st->parent->expr,
|
||||||
.arg = ep->expr);
|
.arg = ep->expr);
|
||||||
@@ -159,7 +147,7 @@ pgf_combine1_to_expr(PgfCombine1State* st, GuPool* tmp_pool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PgfExprState*
|
static PgfExprState*
|
||||||
pgf_combine2_to_expr(PgfCombine2State* st, GuPool* tmp_pool)
|
pgf_combine2_to_expr(PgfCombine2State* st, GuPool* pool, GuPool* out_pool)
|
||||||
{
|
{
|
||||||
PgfExprState* parent =
|
PgfExprState* parent =
|
||||||
gu_buf_get(st->parents, PgfExprState*, st->choice);
|
gu_buf_get(st->parents, PgfExprState*, st->choice);
|
||||||
@@ -167,12 +155,12 @@ pgf_combine2_to_expr(PgfCombine2State* st, GuPool* tmp_pool)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
PgfExprState* nst =
|
PgfExprState* nst =
|
||||||
gu_new(PgfExprState, tmp_pool);
|
gu_new(PgfExprState, pool);
|
||||||
nst->base.continuation = parent->base.continuation;
|
nst->base.continuation = parent->base.continuation;
|
||||||
nst->base.prob = st->base.prob;
|
nst->base.prob = st->base.prob;
|
||||||
nst->answers = parent->answers;
|
nst->answers = parent->answers;
|
||||||
nst->expr =
|
nst->expr =
|
||||||
gu_new_variant_i(tmp_pool, PGF_EXPR_APP,
|
gu_new_variant_i(out_pool, PGF_EXPR_APP,
|
||||||
PgfExprApp,
|
PgfExprApp,
|
||||||
.fun = parent->expr,
|
.fun = parent->expr,
|
||||||
.arg = st->ep->expr);
|
.arg = st->ep->expr);
|
||||||
@@ -190,7 +178,7 @@ static void
|
|||||||
pgf_print_combine1_state(PgfCombine1State* st,
|
pgf_print_combine1_state(PgfCombine1State* st,
|
||||||
GuOut* out, GuExn* err, GuPool* tmp_pool)
|
GuOut* out, GuExn* err, GuPool* tmp_pool)
|
||||||
{
|
{
|
||||||
PgfExprState* nst = pgf_combine1_to_expr(st, tmp_pool);
|
PgfExprState* nst = pgf_combine1_to_expr(st, tmp_pool, tmp_pool);
|
||||||
pgf_print_expr_state(nst, out, err, tmp_pool);
|
pgf_print_expr_state(nst, out, err, tmp_pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,7 +195,7 @@ pgf_print_combine2_state(PgfCombine2State* st,
|
|||||||
static void
|
static void
|
||||||
pgf_combine1(PgfReasoner* rs, PgfCombine1State* st)
|
pgf_combine1(PgfReasoner* rs, PgfCombine1State* st)
|
||||||
{
|
{
|
||||||
PgfExprState* nst = pgf_combine1_to_expr(st, rs->tmp_pool);
|
PgfExprState* nst = pgf_combine1_to_expr(st, rs->pool, rs->out_pool);
|
||||||
nst->base.continuation(rs, &nst->base);
|
nst->base.continuation(rs, &nst->base);
|
||||||
|
|
||||||
st->choice++;
|
st->choice++;
|
||||||
@@ -228,9 +216,9 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
|||||||
|
|
||||||
PgfAnswers* answers = gu_map_get(rs->table, cat, PgfAnswers*);
|
PgfAnswers* answers = gu_map_get(rs->table, cat, PgfAnswers*);
|
||||||
if (answers == NULL) {
|
if (answers == NULL) {
|
||||||
answers = gu_new(PgfAnswers, rs->tmp_pool);
|
answers = gu_new(PgfAnswers, rs->pool);
|
||||||
answers->parents = gu_new_buf(PgfExprState*, rs->tmp_pool);
|
answers->parents = gu_new_buf(PgfExprState*, rs->pool);
|
||||||
answers->exprs = gu_new_buf(PgfExprProb*, rs->tmp_pool);
|
answers->exprs = gu_new_buf(PgfExprProb*, rs->pool);
|
||||||
answers->outside_prob = parent->base.prob;
|
answers->outside_prob = parent->base.prob;
|
||||||
|
|
||||||
gu_map_put(rs->table, cat, PgfAnswers*, answers);
|
gu_map_put(rs->table, cat, PgfAnswers*, answers);
|
||||||
@@ -239,7 +227,7 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
|||||||
gu_buf_push(answers->parents, PgfExprState*, parent);
|
gu_buf_push(answers->parents, PgfExprState*, parent);
|
||||||
|
|
||||||
if (gu_buf_length(answers->parents) == 1) {
|
if (gu_buf_length(answers->parents) == 1) {
|
||||||
PgfExprState* st = gu_new(PgfExprState, rs->tmp_pool);
|
PgfExprState* st = gu_new(PgfExprState, rs->pool);
|
||||||
st->base.continuation = (PgfPredicate) absfun->predicate;
|
st->base.continuation = (PgfPredicate) absfun->predicate;
|
||||||
st->base.prob = answers->outside_prob + absfun->ep.prob;
|
st->base.prob = answers->outside_prob + absfun->ep.prob;
|
||||||
st->answers = answers;
|
st->answers = answers;
|
||||||
@@ -256,7 +244,7 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
|||||||
PgfExprProb* ep =
|
PgfExprProb* ep =
|
||||||
gu_buf_get(answers->exprs, PgfExprProb*, 0);
|
gu_buf_get(answers->exprs, PgfExprProb*, 0);
|
||||||
|
|
||||||
PgfCombine1State* nst = gu_new(PgfCombine1State, rs->tmp_pool);
|
PgfCombine1State* nst = gu_new(PgfCombine1State, rs->pool);
|
||||||
nst->base.continuation = (PgfPredicate) pgf_combine1;
|
nst->base.continuation = (PgfPredicate) pgf_combine1;
|
||||||
nst->base.prob = parent->base.prob + ep->prob;
|
nst->base.prob = parent->base.prob + ep->prob;
|
||||||
nst->exprs = answers->exprs;
|
nst->exprs = answers->exprs;
|
||||||
@@ -274,7 +262,7 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
|||||||
void
|
void
|
||||||
pgf_reasoner_try_else(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun)
|
pgf_reasoner_try_else(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun)
|
||||||
{
|
{
|
||||||
PgfExprState *st = gu_new(PgfExprState, rs->tmp_pool);
|
PgfExprState *st = gu_new(PgfExprState, rs->pool);
|
||||||
st->base.continuation = (PgfPredicate) absfun->predicate;
|
st->base.continuation = (PgfPredicate) absfun->predicate;
|
||||||
st->base.prob = prev->answers->outside_prob + absfun->ep.prob;
|
st->base.prob = prev->answers->outside_prob + absfun->ep.prob;
|
||||||
st->answers = prev->answers;
|
st->answers = prev->answers;
|
||||||
@@ -290,7 +278,7 @@ pgf_reasoner_try_else(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun)
|
|||||||
static void
|
static void
|
||||||
pgf_combine2(PgfReasoner* rs, PgfCombine2State* st)
|
pgf_combine2(PgfReasoner* rs, PgfCombine2State* st)
|
||||||
{
|
{
|
||||||
PgfExprState* nst = pgf_combine2_to_expr(st, rs->tmp_pool);
|
PgfExprState* nst = pgf_combine2_to_expr(st, rs->pool, rs->out_pool);
|
||||||
if (nst != NULL) {
|
if (nst != NULL) {
|
||||||
nst->base.continuation(rs, &nst->base);
|
nst->base.continuation(rs, &nst->base);
|
||||||
}
|
}
|
||||||
@@ -309,12 +297,12 @@ pgf_combine2(PgfReasoner* rs, PgfCombine2State* st)
|
|||||||
void
|
void
|
||||||
pgf_reasoner_complete(PgfReasoner* rs, PgfExprState* st)
|
pgf_reasoner_complete(PgfReasoner* rs, PgfExprState* st)
|
||||||
{
|
{
|
||||||
PgfExprProb* ep = gu_new(PgfExprProb, rs->pool);
|
PgfExprProb* ep = gu_new(PgfExprProb, rs->out_pool);
|
||||||
ep->prob = st->base.prob - st->answers->outside_prob;
|
ep->prob = st->base.prob - st->answers->outside_prob;
|
||||||
ep->expr = st->expr;
|
ep->expr = st->expr;
|
||||||
gu_buf_push(st->answers->exprs, PgfExprProb*, ep);
|
gu_buf_push(st->answers->exprs, PgfExprProb*, ep);
|
||||||
|
|
||||||
PgfCombine2State* nst = gu_new(PgfCombine2State, rs->tmp_pool);
|
PgfCombine2State* nst = gu_new(PgfCombine2State, rs->pool);
|
||||||
nst->base.continuation = (PgfPredicate) pgf_combine2;
|
nst->base.continuation = (PgfPredicate) pgf_combine2;
|
||||||
nst->base.prob = st->base.prob;
|
nst->base.prob = st->base.prob;
|
||||||
nst->parents = st->answers->parents;
|
nst->parents = st->answers->parents;
|
||||||
@@ -337,9 +325,6 @@ pgf_reasoner_try_constant(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun
|
|||||||
static PgfExprProb*
|
static PgfExprProb*
|
||||||
pgf_reasoner_next(PgfReasoner* rs)
|
pgf_reasoner_next(PgfReasoner* rs)
|
||||||
{
|
{
|
||||||
if (rs->tmp_pool == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
size_t n_exprs = gu_buf_length(rs->exprs);
|
size_t n_exprs = gu_buf_length(rs->exprs);
|
||||||
|
|
||||||
while (gu_buf_length(rs->pqueue) > 0) {
|
while (gu_buf_length(rs->pqueue) > 0) {
|
||||||
@@ -363,9 +348,6 @@ pgf_reasoner_next(PgfReasoner* rs)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gu_pool_free(rs->tmp_pool);
|
|
||||||
rs->tmp_pool = NULL;
|
|
||||||
rs->pqueue = NULL;
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,21 +358,42 @@ pgf_reasoner_enum_next(GuEnum* self, void* to, GuPool* pool)
|
|||||||
*(PgfExprProb**)to = pgf_reasoner_next(pr);
|
*(PgfExprProb**)to = pgf_reasoner_next(pr);
|
||||||
}
|
}
|
||||||
|
|
||||||
PgfExprEnum*
|
PgfReasoner*
|
||||||
pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuPool* pool)
|
pgf_new_reasoner(PgfPGF* pgf, GuExn* err, GuPool* pool, GuPool* out_pool)
|
||||||
{
|
{
|
||||||
PgfReasoner* rs = gu_new(PgfReasoner, pool);
|
size_t n_cafs =
|
||||||
rs->pool = pool;
|
(pgf->abstract.eval_gates->cafs == NULL)
|
||||||
rs->tmp_pool = gu_new_pool(),
|
? 0 : gu_seq_length(pgf->abstract.eval_gates->cafs);
|
||||||
rs->abstract = &pgf->abstract,
|
|
||||||
rs->table = gu_new_string_map(PgfAnswers*, &gu_null_struct, rs->tmp_pool),
|
|
||||||
|
|
||||||
rs->pqueue = gu_new_buf(PgfReasonerState*, rs->tmp_pool);
|
PgfReasoner* rs = gu_new_flex(pool, PgfReasoner, cafs, n_cafs);
|
||||||
rs->exprs = gu_new_buf(PgfExprProb*, rs->tmp_pool);
|
rs->pool = pool,
|
||||||
|
rs->out_pool = out_pool;
|
||||||
|
rs->err = err;
|
||||||
|
rs->abstract = &pgf->abstract,
|
||||||
|
rs->table = gu_new_string_map(PgfAnswers*, &gu_null_struct, rs->pool),
|
||||||
|
|
||||||
|
rs->eval_gates = pgf->abstract.eval_gates;
|
||||||
|
|
||||||
|
rs->pqueue = gu_new_buf(PgfReasonerState*, rs->pool);
|
||||||
|
rs->exprs = gu_new_buf(PgfExprProb*, rs->pool);
|
||||||
rs->en.next = pgf_reasoner_enum_next;
|
rs->en.next = pgf_reasoner_enum_next;
|
||||||
|
|
||||||
PgfAnswers* answers = gu_new(PgfAnswers, rs->tmp_pool);
|
PgfFunction* cafs = gu_seq_data(rs->eval_gates->cafs);
|
||||||
answers->parents = gu_new_buf(PgfExprState*, rs->tmp_pool);
|
for (size_t i = 0; i < n_cafs; i++) {
|
||||||
|
rs->cafs[i].header.code = cafs[i];
|
||||||
|
rs->cafs[i].val = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rs;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExprEnum*
|
||||||
|
pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuExn* err, GuPool* pool, GuPool* out_pool)
|
||||||
|
{
|
||||||
|
PgfReasoner* rs = pgf_new_reasoner(pgf, err, pool, out_pool);
|
||||||
|
|
||||||
|
PgfAnswers* answers = gu_new(PgfAnswers, rs->pool);
|
||||||
|
answers->parents = gu_new_buf(PgfExprState*, rs->pool);
|
||||||
answers->exprs = rs->exprs;
|
answers->exprs = rs->exprs;
|
||||||
answers->outside_prob = 0;
|
answers->outside_prob = 0;
|
||||||
gu_map_put(rs->table, cat, PgfAnswers*, answers);
|
gu_map_put(rs->table, cat, PgfAnswers*, answers);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef PGF_REASONER_H_
|
#ifndef PGF_REASONER_H_
|
||||||
#define PGF_REASONER_H_
|
#define PGF_REASONER_H_
|
||||||
|
|
||||||
|
typedef GuStringMap PgfAbswersMap;
|
||||||
|
|
||||||
typedef struct PgfReasoner PgfReasoner;
|
typedef struct PgfReasoner PgfReasoner;
|
||||||
typedef struct PgfReasonerState PgfReasonerState;
|
typedef struct PgfReasonerState PgfReasonerState;
|
||||||
typedef struct PgfExprState PgfExprState;
|
typedef struct PgfExprState PgfExprState;
|
||||||
@@ -19,4 +21,135 @@ pgf_reasoner_complete(PgfReasoner* rs, PgfExprState* st);
|
|||||||
void
|
void
|
||||||
pgf_reasoner_try_constant(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun);
|
pgf_reasoner_try_constant(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfFunction code;
|
||||||
|
} PgfClosure;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfClosure* val;
|
||||||
|
} PgfIndirection;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfLiteral lit;
|
||||||
|
GuBuf* consts;
|
||||||
|
void* enter_stack_ptr;
|
||||||
|
} PgfEvalAccum;
|
||||||
|
|
||||||
|
struct PgfReasoner {
|
||||||
|
GuPool* pool;
|
||||||
|
GuPool* out_pool;
|
||||||
|
PgfAbstr* abstract;
|
||||||
|
PgfAbswersMap* table;
|
||||||
|
GuBuf* pqueue;
|
||||||
|
GuBuf* exprs;
|
||||||
|
PgfEvalGates* eval_gates; // cached from pgf->abstr->eval_gates
|
||||||
|
GuExn* err;
|
||||||
|
void* enter_stack_ptr;
|
||||||
|
void* tmp; // for temporary register spills
|
||||||
|
PgfExprEnum en;
|
||||||
|
PgfIndirection cafs[]; // derived from gu_seq_data(pgf->abstr->eval_gates->cafs)
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct PgfEnv PgfEnv;
|
||||||
|
|
||||||
|
struct PgfEnv {
|
||||||
|
PgfEnv* next;
|
||||||
|
PgfClosure* closure;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfEnv* env;
|
||||||
|
PgfExpr expr;
|
||||||
|
} PgfExprThunk;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfClosure* con;
|
||||||
|
PgfClosure* args[];
|
||||||
|
} PgfValue;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
int level;
|
||||||
|
} PgfValueGen;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfEnv* env;
|
||||||
|
PgfMetaId id;
|
||||||
|
} PgfValueMeta;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfLiteral lit;
|
||||||
|
} PgfValueLit;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfLiteral lit;
|
||||||
|
GuBuf* consts;
|
||||||
|
} PgfValueSum;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfClosure header;
|
||||||
|
PgfClosure* fun;
|
||||||
|
size_t n_args;
|
||||||
|
PgfClosure* args[];
|
||||||
|
} PgfValuePAP;
|
||||||
|
|
||||||
|
struct PgfEvalGates {
|
||||||
|
PgfFunction evaluate_expr_thunk;
|
||||||
|
PgfFunction evaluate_indirection;
|
||||||
|
PgfFunction evaluate_value;
|
||||||
|
PgfFunction evaluate_value_lit;
|
||||||
|
PgfFunction evaluate_value_pap;
|
||||||
|
PgfFunction evaluate_value_lambda;
|
||||||
|
PgfFunction evaluate_value_const;
|
||||||
|
PgfFunction evaluate_meta;
|
||||||
|
PgfFunction evaluate_gen;
|
||||||
|
PgfFunction evaluate_sum;
|
||||||
|
PgfFunction evaluate_caf;
|
||||||
|
|
||||||
|
PgfFunction update_closure;
|
||||||
|
PgfFunction update_pap;
|
||||||
|
|
||||||
|
PgfFunction mk_const;
|
||||||
|
|
||||||
|
PgfClosure* (*enter)(PgfReasoner* rs, PgfClosure* closure);
|
||||||
|
|
||||||
|
GuFinalizer fin;
|
||||||
|
GuSeq* cafs;
|
||||||
|
};
|
||||||
|
|
||||||
|
PgfReasoner*
|
||||||
|
pgf_new_reasoner(PgfPGF* pgf, GuExn* err, GuPool* pool, GuPool* out_pool);
|
||||||
|
|
||||||
|
PgfClosure*
|
||||||
|
pgf_evaluate_expr_thunk(PgfReasoner* rs, PgfExprThunk* thunk);
|
||||||
|
|
||||||
|
PgfClosure*
|
||||||
|
pgf_evaluate_lambda_application(PgfReasoner* rs, PgfExprThunk* lambda,
|
||||||
|
PgfClosure* arg);
|
||||||
|
|
||||||
|
void
|
||||||
|
pgf_evaluate_accum_init_int(PgfReasoner* rs,
|
||||||
|
PgfEvalAccum* accum, int val);
|
||||||
|
|
||||||
|
void
|
||||||
|
pgf_evaluate_accum_init_str(PgfReasoner* rs,
|
||||||
|
PgfEvalAccum* accum, GuString val);
|
||||||
|
|
||||||
|
void
|
||||||
|
pgf_evaluate_accum_init_flt(PgfReasoner* rs,
|
||||||
|
PgfEvalAccum* accum, float val);
|
||||||
|
|
||||||
|
void
|
||||||
|
pgf_evaluate_accum_add(PgfReasoner* rs,
|
||||||
|
PgfEvalAccum* accum, PgfClosure* closure);
|
||||||
|
|
||||||
|
PgfClosure*
|
||||||
|
pgf_evaluate_accum_done(PgfReasoner* rs, PgfEvalAccum* accum);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -113,7 +113,8 @@ generateAll p cat =
|
|||||||
do genPl <- gu_new_pool
|
do genPl <- gu_new_pool
|
||||||
exprPl <- gu_new_pool
|
exprPl <- gu_new_pool
|
||||||
enum <- withCString cat $ \cat ->
|
enum <- withCString cat $ \cat ->
|
||||||
pgf_generate_all (pgf p) cat genPl
|
exn <- gu_new_exn genPl
|
||||||
|
pgf_generate_all (pgf p) cat exn genPl exprPl
|
||||||
genFPl <- newForeignPtr gu_pool_finalizer genPl
|
genFPl <- newForeignPtr gu_pool_finalizer genPl
|
||||||
exprFPl <- newForeignPtr gu_pool_finalizer exprPl
|
exprFPl <- newForeignPtr gu_pool_finalizer exprPl
|
||||||
fromPgfExprEnum enum genFPl (p,exprFPl)
|
fromPgfExprEnum enum genFPl (p,exprFPl)
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ foreign import ccall "pgf/expr.h pgf_print_expr"
|
|||||||
pgf_print_expr :: PgfExpr -> Ptr PgfPrintContext -> CInt -> Ptr GuOut -> Ptr GuExn -> IO ()
|
pgf_print_expr :: PgfExpr -> Ptr PgfPrintContext -> CInt -> Ptr GuOut -> Ptr GuExn -> IO ()
|
||||||
|
|
||||||
foreign import ccall "pgf/pgf.h pgf_generate_all"
|
foreign import ccall "pgf/pgf.h pgf_generate_all"
|
||||||
pgf_generate_all :: Ptr PgfPGF -> CString -> Ptr GuPool -> IO (Ptr GuEnum)
|
pgf_generate_all :: Ptr PgfPGF -> CString -> Ptr GuExn -> Ptr GuPool -> Ptr GuPool -> IO (Ptr GuEnum)
|
||||||
|
|
||||||
foreign import ccall "pgf/pgf.h pgf_print"
|
foreign import ccall "pgf/pgf.h pgf_print"
|
||||||
pgf_print :: Ptr PgfPGF -> Ptr GuOut -> Ptr GuExn -> IO ()
|
pgf_print :: Ptr PgfPGF -> Ptr GuOut -> Ptr GuExn -> IO ()
|
||||||
|
|||||||
@@ -1154,11 +1154,12 @@ JNIEXPORT jobject JNICALL
|
|||||||
Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat)
|
Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat)
|
||||||
{
|
{
|
||||||
GuPool* pool = gu_new_pool();
|
GuPool* pool = gu_new_pool();
|
||||||
|
GuPool* out_pool = gu_new_pool();
|
||||||
GuString startCat = j2gu_string(env, jstartCat, pool);
|
GuString startCat = j2gu_string(env, jstartCat, pool);
|
||||||
|
GuExn* err = gu_exn(pool);
|
||||||
|
|
||||||
GuEnum* res =
|
GuEnum* res =
|
||||||
pgf_generate_all(get_ref(env, jpgf), startCat, pool);
|
pgf_generate_all(get_ref(env, jpgf), startCat, err, pool, out_pool);
|
||||||
if (res == NULL) {
|
if (res == NULL) {
|
||||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The generation failed");
|
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The generation failed");
|
||||||
gu_pool_free(pool);
|
gu_pool_free(pool);
|
||||||
@@ -1167,7 +1168,7 @@ Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass claz
|
|||||||
|
|
||||||
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
|
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
|
||||||
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V");
|
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V");
|
||||||
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(pool), p2l(res));
|
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(out_pool), p2l(res));
|
||||||
|
|
||||||
return jexpiter;
|
return jexpiter;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2484,24 +2484,26 @@ PGF_generateAll(PGFObject* self, PyObject *args, PyObject *keywds)
|
|||||||
pyres->source = (PyObject*) self;
|
pyres->source = (PyObject*) self;
|
||||||
Py_INCREF(self);
|
Py_INCREF(self);
|
||||||
|
|
||||||
|
GuPool* out_pool = gu_new_pool();
|
||||||
|
|
||||||
|
PyObject* py_pool = PyPool_New(out_pool);
|
||||||
|
pyres->container = PyTuple_Pack(2, pyres->source, py_pool);
|
||||||
|
Py_DECREF(py_pool);
|
||||||
|
|
||||||
pyres->pool = gu_new_pool();
|
pyres->pool = gu_new_pool();
|
||||||
pyres->max_count = max_count;
|
pyres->max_count = max_count;
|
||||||
pyres->counter = 0;
|
pyres->counter = 0;
|
||||||
pyres->fetch = Iter_fetch_expr;
|
pyres->fetch = Iter_fetch_expr;
|
||||||
pyres->container = (PyObject*) pyres;
|
|
||||||
|
|
||||||
GuPool *tmp_pool = gu_local_pool();
|
GuExn* err = gu_exn(pyres->pool);
|
||||||
|
|
||||||
pyres->res =
|
pyres->res =
|
||||||
pgf_generate_all(self->pgf, catname, pyres->pool);
|
pgf_generate_all(self->pgf, catname, err, pyres->pool, out_pool);
|
||||||
if (pyres->res == NULL) {
|
if (pyres->res == NULL) {
|
||||||
Py_DECREF(pyres);
|
Py_DECREF(pyres);
|
||||||
gu_pool_free(tmp_pool);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
gu_pool_free(tmp_pool);
|
|
||||||
|
|
||||||
return pyres;
|
return pyres;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user