From ed5de8335bb1d984918702abff70aadc4d554539 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Wed, 7 Mar 2012 16:39:29 +0000 Subject: [PATCH] libpgf: implementation for built in literal categories --- src/runtime/c/gu/string.c | 103 +++++++++++++++ src/runtime/c/gu/string.h | 6 + src/runtime/c/pgf/data.c | 32 +---- src/runtime/c/pgf/data.h | 19 ++- src/runtime/c/pgf/expr.c | 2 +- src/runtime/c/pgf/linearize.c | 59 ++++++++- src/runtime/c/pgf/parser.c | 232 ++++++++++++++++++++++++++++------ src/runtime/c/pgf/reader.c | 47 +++++-- 8 files changed, 407 insertions(+), 93 deletions(-) diff --git a/src/runtime/c/gu/string.c b/src/runtime/c/gu/string.c index b24eeeb06..0af0ce7be 100644 --- a/src/runtime/c/gu/string.c +++ b/src/runtime/c/gu/string.c @@ -209,6 +209,109 @@ gu_str_string(const char* str, GuPool* pool) #endif } +bool +gu_string_to_int(GuString s, int *res) +{ + GuWord w = s.w_; + uint8_t buf[sizeof(GuWord)]; + char* src; + size_t sz; + if (w & 1) { + sz = (w & 0xff) >> 1; + gu_assert(sz <= sizeof(GuWord)); + size_t i = sz; + while (i > 0) { + w >>= 8; + buf[--i] = w & 0xff; + } + src = (char*) buf; + } else { + uint8_t* p = (void*) w; + sz = (p[0] == 0) ? ((size_t*) p)[-1] : p[0]; + src = (char*) &p[1]; + } + + size_t i = 0; + + bool neg = false; + + if (src[i] == '-') { + neg = true; + i++; + } + + if (i >= sz) + return false; + + int n = 0; + for (; i < sz; i++) { + if (src[i] < '0' || src[i] > '9') + return false; + + n = n * 10 + (src[i] - '0'); + } + + *res = neg ? -n : n; + return true; +} + +bool +gu_string_to_double(GuString s, double *res) +{ + GuWord w = s.w_; + uint8_t buf[sizeof(GuWord)]; + char* src; + size_t sz; + if (w & 1) { + sz = (w & 0xff) >> 1; + gu_assert(sz <= sizeof(GuWord)); + size_t i = sz; + while (i > 0) { + w >>= 8; + buf[--i] = w & 0xff; + } + src = (char*) buf; + } else { + uint8_t* p = (void*) w; + sz = (p[0] == 0) ? ((size_t*) p)[-1] : p[0]; + src = (char*) &p[1]; + } + + size_t i = 0; + + bool neg = false; + bool dec = false; + int exp = 1; + + if (src[i] == '-') { + neg = true; + i++; + } + + if (i >= sz) + return false; + + double d = 0; + for (; i < sz; i++) { + if (src[i] == '.') { + if (dec) return false; + + dec = true; + continue; + } + + if (src[i] < '0' || src[i] > '9') + return false; + + if (dec) exp = exp * 10; + + d = d * 10 + (src[i] - '0'); + } + + *res = (neg ? -d : d) / exp; + return true; +} + GuWord gu_string_hash(GuString s) { diff --git a/src/runtime/c/gu/string.h b/src/runtime/c/gu/string.h index d6bb11b58..b041518b3 100644 --- a/src/runtime/c/gu/string.h +++ b/src/runtime/c/gu/string.h @@ -63,6 +63,12 @@ gu_format_string(GuPool* pool, const char* fmt, ...); GuString gu_str_string(const char* str, GuPool* pool); +bool +gu_string_to_int(GuString s, int *res); + +bool +gu_string_to_double(GuString s, double *res); + #endif // GU_STRING_H_ #if defined(GU_HASH_H_) && !defined(GU_STRING_H_HASH_) diff --git a/src/runtime/c/pgf/data.c b/src/runtime/c/pgf/data.c index 710eeb952..9f82c2e96 100644 --- a/src/runtime/c/pgf/data.c +++ b/src/runtime/c/pgf/data.c @@ -4,27 +4,6 @@ #include #include -PgfCCat pgf_ccat_string = { NULL, NULL, 0, GU_NULL_SEQ, -1 }; -PgfCCat pgf_ccat_int = { NULL, NULL, 0, GU_NULL_SEQ, -2 }; -PgfCCat pgf_ccat_float = { NULL, NULL, 0, GU_NULL_SEQ, -3 }; -PgfCCat pgf_ccat_var = { NULL, NULL, 0, GU_NULL_SEQ, -4 }; - -PgfCCatId -pgf_literal_cat(PgfLiteral lit) -{ - switch (gu_variant_tag(lit)) { - case PGF_LITERAL_STR: - return &pgf_ccat_string; - case PGF_LITERAL_INT: - return &pgf_ccat_int; - case PGF_LITERAL_FLT: - return &pgf_ccat_float; - default: - gu_impossible(); - return NULL; - } -} - bool pgf_tokens_equal(PgfTokens t1, PgfTokens t2) { @@ -145,11 +124,6 @@ GU_DEFINE_TYPE( GU_CONSTRUCTOR_S( PGF_PRODUCTION_COERCE, PgfProductionCoerce, GU_MEMBER(PgfProductionCoerce, coerce, PgfCCatId)), - GU_CONSTRUCTOR_S( - PGF_PRODUCTION_CONST, PgfProductionConst, - GU_MEMBER(PgfProductionConst, expr, PgfExpr), - GU_MEMBER(PgfProductionConst, n_toks, GuLength), - GU_FLEX_MEMBER(PgfProductionConst, toks, GuString)), GU_CONSTRUCTOR_S( PGF_PRODUCTION_META, PgfProductionMeta, GU_MEMBER(PgfProductionMeta, args, PgfPArgs))); @@ -192,12 +166,16 @@ GU_DEFINE_TYPE( // Distinct type so we can give it special treatment in the reader GU_DEFINE_TYPE(PgfEquationsM, GuSeq, gu_type(PgfEquation)); +GU_DEFINE_TYPE(PgfExprProb, struct, + GU_MEMBER(PgfExprProb, prob, double), + GU_MEMBER(PgfExprProb, expr, PgfExpr)); + GU_DEFINE_TYPE( PgfFunDecl, struct, GU_MEMBER_P(PgfFunDecl, type, PgfType), GU_MEMBER(PgfFunDecl, arity, int), GU_MEMBER(PgfFunDecl, defns, PgfEquationsM), - GU_MEMBER(PgfFunDecl, prob, double)); + GU_MEMBER(PgfFunDecl, ep, PgfExprProb)); GU_DEFINE_TYPE( PgfCatFun, struct, diff --git a/src/runtime/c/pgf/data.h b/src/runtime/c/pgf/data.h index 4a8fc062b..4af0a6614 100644 --- a/src/runtime/c/pgf/data.h +++ b/src/runtime/c/pgf/data.h @@ -124,13 +124,22 @@ struct PgfPGF { extern GU_DECLARE_TYPE(PgfPGF, struct); +typedef struct { + double prob; + PgfExpr expr; +} PgfExprProb; + +extern GU_DECLARE_TYPE(PgfExprProb, struct); + struct PgfFunDecl { PgfType* type; - int arity; // Only for computational defs? + int arity; PgfEquationsM defns; // maybe null - double prob; + PgfExprProb ep; }; +extern GU_DECLARE_TYPE(PgfFunDecl, struct); + struct PgfCatFun { double prob; PgfCId fun; @@ -160,7 +169,7 @@ struct PgfCncCat { struct PgfCncFun { PgfCId name; - PgfFunDecl *absfun; + PgfExprProb *ep; int funid; GuLength n_lins; PgfSeqId lins[]; @@ -260,7 +269,6 @@ typedef struct PgfSymbolKP typedef enum { PGF_PRODUCTION_APPLY, PGF_PRODUCTION_COERCE, - PGF_PRODUCTION_CONST, PGF_PRODUCTION_META } PgfProductionTag; @@ -307,9 +315,6 @@ extern GU_DECLARE_TYPE(PgfBindType, enum); extern GU_DECLARE_TYPE(PgfLiteral, GuVariant); -PgfCCatId -pgf_literal_cat(PgfLiteral lit); - // PgfPatt typedef enum { diff --git a/src/runtime/c/pgf/expr.c b/src/runtime/c/pgf/expr.c index ad8489808..3ccb9c232 100644 --- a/src/runtime/c/pgf/expr.c +++ b/src/runtime/c/pgf/expr.c @@ -311,7 +311,7 @@ pgf_print_literal(PgfLiteral lit, } case PGF_LITERAL_FLT: { PgfLiteralFlt* lit = ei.data; - gu_printf(wtr, err, "%f", lit->val); + gu_printf(wtr, err, "%lf", lit->val); break; } default: diff --git a/src/runtime/c/pgf/linearize.c b/src/runtime/c/pgf/linearize.c index 08e182d50..15b7ba3cc 100644 --- a/src/runtime/c/pgf/linearize.c +++ b/src/runtime/c/pgf/linearize.c @@ -338,6 +338,29 @@ finish: return ret; } +PgfCCat* +pgf_literal_cat(PgfLzn* lzn, PgfLiteral lit) +{ + int fid; + + switch (gu_variant_tag(lit)) { + case PGF_LITERAL_STR: + fid = -1; + break; + case PGF_LITERAL_INT: + fid = -2; + break; + case PGF_LITERAL_FLT: + fid = -3; + break; + default: + gu_impossible(); + return NULL; + } + + return gu_map_get(lzn->concr->ccats, &fid, PgfCCat*); +} + static PgfCCat* pgf_lzn_infer(PgfLzn* lzn, PgfExpr expr, GuPool* pool, PgfCncTree* ctree_out) { @@ -357,7 +380,7 @@ pgf_lzn_infer(PgfLzn* lzn, PgfExpr expr, GuPool* pool, PgfCncTree* ctree_out) PgfCncTreeLit, .lit = elit->lit); } - ret = pgf_literal_cat(elit->lit); + ret = pgf_literal_cat(lzn, elit->lit); } default: // XXX: should we do something here? @@ -523,8 +546,40 @@ pgf_file_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens toks) } } +static void +pgf_file_lzn_expr_literal(PgfLinFuncs** funcs, PgfLiteral lit) +{ + PgfSimpleLin* flin = gu_container(funcs, PgfSimpleLin, funcs); + if (!gu_ok(flin->err)) { + return; + } + + GuVariantInfo i = gu_variant_open(lit); + switch (i.tag) { + case PGF_LITERAL_STR: { + PgfLiteralStr* lstr = i.data; + gu_string_write(lstr->val, flin->wtr, flin->err); + gu_putc(' ', flin->wtr, flin->err); + break; + } + case PGF_LITERAL_INT: { + PgfLiteralInt* lint = i.data; + gu_printf(flin->wtr, flin->err, "%d ", lint->val); + break; + } + case PGF_LITERAL_FLT: { + PgfLiteralFlt* lflt = i.data; + gu_printf(flin->wtr, flin->err, "%lf ", lflt->val); + break; + } + default: + gu_impossible(); + } +} + static PgfLinFuncs pgf_file_lin_funcs = { - .symbol_tokens = pgf_file_lzn_symbol_tokens + .symbol_tokens = pgf_file_lzn_symbol_tokens, + .expr_literal = pgf_file_lzn_expr_literal }; void diff --git a/src/runtime/c/pgf/parser.c b/src/runtime/c/pgf/parser.c index 9876a548e..8190a9df1 100644 --- a/src/runtime/c/pgf/parser.c +++ b/src/runtime/c/pgf/parser.c @@ -20,21 +20,12 @@ struct PgfParse { int max_fid; }; -typedef struct { - double prob; - PgfExpr expr; -} PgfExprProb; - typedef struct { PgfExprProb ep; PgfPArgs args; size_t arg_idx; } PgfExprState; -static GU_DEFINE_TYPE(PgfExprProb, struct, - GU_MEMBER(PgfExprProb, prob, double), - GU_MEMBER(PgfExprProb, expr, PgfExpr)); - typedef GuMap PgfExprCache; static GU_DEFINE_TYPE(PgfExprCache, GuMap, gu_type(PgfCCat), NULL, @@ -103,10 +94,16 @@ GU_DEFINE_TYPE(PgfTransitions, GuStringMap, typedef struct PgfParsing PgfParsing; +typedef struct { + PgfTokens tokens; + PgfExprProb ep; +} PgfLiteralCandidate; + typedef const struct PgfLexCallback PgfLexCallback; struct PgfLexCallback { void (*lex)(PgfLexCallback* self, PgfToken tok, PgfItem* item); + GuEnum *(*lit)(PgfLexCallback* self, PgfCCat* cat); }; struct PgfParsing { @@ -133,7 +130,7 @@ pgf_print_production(int fid, PgfProduction prod, GuWriter *wtr, GuExn* err) case PGF_PRODUCTION_APPLY: { PgfProductionApply* papp = i.data; gu_printf(wtr,err,"F%d(",papp->fun->funid); - gu_string_write(papp->fun->name, wtr, err); + pgf_print_expr(papp->fun->ep->expr, 0, wtr, err); gu_printf(wtr,err,")["); size_t n_args = gu_seq_length(papp->args); for (size_t j = 0; j < n_args; j++) { @@ -195,7 +192,7 @@ pgf_print_item(PgfItem* item, GuWriter* wtr, GuExn* err) PgfProductionApply* papp = i.data; PgfCncFun* fun = papp->fun; gu_printf(wtr, err, "F%d(", fun->funid); - gu_string_write(fun->name, wtr, err); + pgf_print_expr(fun->ep->expr, 0, wtr, err); gu_printf(wtr, err, ")["); for (size_t i = 0; i < gu_seq_length(item->args); i++) { PgfPArg arg = gu_seq_get(item->args, PgfPArg, i); @@ -445,10 +442,20 @@ pgf_parsing_combine(PgfParsing* parsing, PgfItem* cont, PgfCCat* cat) PgfItem* item = NULL; if (!gu_variant_is_null(cont->curr_sym)) { - gu_assert(gu_variant_tag(cont->curr_sym) == PGF_SYMBOL_CAT); - PgfSymbolCat* scat = gu_variant_data(cont->curr_sym); - - item = pgf_item_update_arg(cont, scat->d, cat, parsing->pool); + switch (gu_variant_tag(cont->curr_sym)) { + case PGF_SYMBOL_CAT: { + PgfSymbolCat* scat = gu_variant_data(cont->curr_sym); + item = pgf_item_update_arg(cont, scat->d, cat, parsing->pool); + break; + } + case PGF_SYMBOL_LIT: { + PgfSymbolLit* slit = gu_variant_data(cont->curr_sym); + item = pgf_item_update_arg(cont, slit->d, cat, parsing->pool); + break; + } + default: + gu_impossible(); + } } else { item = pgf_item_copy(cont, parsing->pool); size_t nargs = gu_seq_length(cont->args); @@ -772,9 +779,66 @@ pgf_parsing_symbol(PgfParsing* parsing, PgfItem* item, PgfSymbol sym) { } break; } - case PGF_SYMBOL_LIT: - // XXX TODO proper support + case PGF_SYMBOL_LIT: { + PgfSymbolLit* slit = gu_variant_data(sym); + PgfPArg* parg = gu_seq_index(item->args, PgfPArg, slit->d); + + PgfCncCat *cnccat = parg->ccat->cnccat; + + // the linearization category must be {s : Str} + gu_assert(cnccat->n_lins == 1); + gu_assert(gu_list_length(cnccat->cats) == 1); + + PgfItemBuf* conts = + pgf_parsing_get_conts(parsing->conts_map, + parg->ccat, slit->r, + parsing->pool, parsing->tmp_pool); + gu_buf_push(conts, PgfItem*, item); + if (gu_buf_length(conts) == 1) { + /* This is the first time when we encounter this + * literal category so we must call the callback */ + + GuEnum* en = parsing->callback->lit(parsing->callback, parg->ccat); + for (;;) { + PgfLiteralCandidate* candidate = + gu_next(en, PgfLiteralCandidate*, parsing->pool); + if (candidate == NULL) + break; + + PgfSymbol sym = gu_null_variant; + PgfSymbolKS* sks = + gu_new_variant(PGF_SYMBOL_KS, + PgfSymbolKS, + &sym, parsing->pool); + sks->tokens = candidate->tokens; + + PgfSequence seq = gu_new_seq(PgfSymbol, 1, parsing->pool); + gu_seq_set(seq, PgfSymbol, 0, sym); + + PgfCncFun* fun = + gu_malloc(parsing->pool, + sizeof(PgfCncFun)+ + sizeof(PgfSequence*)*cnccat->n_lins); + fun->name = gu_empty_string; + fun->ep = &candidate->ep; + fun->funid = -1; + fun->n_lins = cnccat->n_lins; + fun->lins[0] = seq; + + PgfProduction prod; + PgfProductionApply* papp = + gu_new_variant(PGF_PRODUCTION_APPLY, + PgfProductionApply, + &prod, parsing->pool); + papp->fun = fun; + papp->args = gu_new_seq(PgfPArg, 0, parsing->pool); + + pgf_parsing_production(parsing, parg->ccat, slit->r, + prod, conts); + } + } break; + } case PGF_SYMBOL_VAR: // XXX TODO proper support break; @@ -864,10 +928,29 @@ pgf_new_parse(PgfConcr* concr, int max_fid, GuPool* pool) return parse; } +static void +pgf_lex_noop(PgfLexCallback* self, PgfToken tok, PgfItem* item) +{ +} + +static void +pgf_enum_null(GuEnum* self, void* to, GuPool* pool) +{ + *((PgfLiteralCandidate**) to) = NULL; +} + +static GuEnum* +pgf_lit_noop(PgfLexCallback* self, PgfCCat* ccat) +{ + static GuEnum en = { pgf_enum_null }; + return &en; +} + typedef struct { PgfLexCallback fn; PgfToken tok; PgfItemBuf* agenda; + GuPool *pool; } PgfParseTokenCallback; static @@ -880,6 +963,79 @@ void pgf_match_token(PgfLexCallback* self, PgfToken tok, PgfItem* item) } } +typedef struct { + GuEnum en; + PgfLiteralCandidate candidate; + size_t idx; +} PgfLitEnum; + +static void +pgf_enum_lits(GuEnum* self, void* to, GuPool* pool) +{ + PgfLitEnum* en = (PgfLitEnum*) self; + + *((PgfLiteralCandidate**) to) = + (en->idx++ > 0) ? NULL : &en->candidate; +} + +static GuEnum* +pgf_match_lit(PgfLexCallback* self, PgfCCat* ccat) +{ + PgfParseTokenCallback *clo = (PgfParseTokenCallback *) self; + + PgfLiteral lit; + + switch (ccat->fid) { + case -1: { + PgfLiteralStr *lit_str = + gu_new_variant(PGF_LITERAL_STR, + PgfLiteralStr, + &lit, clo->pool); + lit_str->val = clo->tok; + break; + } + case -2: { + PgfLiteralInt *lit_int = + gu_new_variant(PGF_LITERAL_INT, + PgfLiteralInt, + &lit, clo->pool); + if (!gu_string_to_int(clo->tok, &lit_int->val)) + return pgf_lit_noop(self, ccat); + break; + } + case -3: { + PgfLiteralFlt *lit_flt = + gu_new_variant(PGF_LITERAL_FLT, + PgfLiteralFlt, + &lit, clo->pool); + if (!gu_string_to_double(clo->tok, &lit_flt->val)) + return pgf_lit_noop(self, ccat); + break; + } + default: + gu_impossible(); + } + + PgfTokens tokens = gu_new_seq(PgfToken, 1, clo->pool); + gu_seq_set(tokens, PgfToken, 0, clo->tok); + + PgfExpr expr = gu_null_variant; + PgfExprLit *expr_lit = + gu_new_variant(PGF_EXPR_LIT, + PgfExprLit, + &expr, clo->pool); + expr_lit->lit = lit; + + PgfLitEnum* en = gu_new(PgfLitEnum, clo->pool); + en->en.next = pgf_enum_lits; + en->candidate.tokens = tokens; + en->candidate.ep.prob = INFINITY; + en->candidate.ep.expr = expr; + en->idx = 0; + + return &en->en; +} + typedef struct { GuMapItor fn; PgfProduction prod; @@ -924,7 +1080,8 @@ pgf_parse_token(PgfParse* parse, PgfToken tok, bool robust, GuPool* pool) { PgfItemBuf* agenda = gu_new_buf(PgfItem*, pool); - PgfParseTokenCallback clo1 = {{ pgf_match_token }, tok, agenda}; + PgfParseTokenCallback clo1 = {{ pgf_match_token, pgf_match_lit }, + tok, agenda, pool}; GuPool* tmp_pool = gu_new_pool(); PgfParsing* parsing = pgf_new_parsing(parse->concr, &clo1.fn, parse->max_fid, pool, tmp_pool); @@ -993,9 +1150,7 @@ pgf_production_to_expr(PgfConcr* concr, PgfProduction prod, switch (pi.tag) { case PGF_PRODUCTION_APPLY: { PgfProductionApply* papp = pi.data; - PgfExpr expr = gu_new_variant_i(pool, PGF_EXPR_FUN, - PgfExprFun, - .fun = papp->fun->name); + PgfExpr expr = papp->fun->ep->expr; size_t n_args = gu_seq_length(papp->args); for (size_t i = 0; i < n_args; i++) { PgfPArg* parg = gu_seq_index(papp->args, PgfPArg, i); @@ -1105,18 +1260,18 @@ pgf_parse_result_enum_next(GuEnum* self, void* to, GuPool* pool) *(PgfExpr*)to = pgf_parse_result_next(pr, pool); } -static -void pgf_noop(PgfLexCallback* self, PgfToken tok, PgfItem* item) -{ -} +static PgfLexCallback lex_callback_noop = + { pgf_lex_noop, pgf_lit_noop }; PgfExprEnum* pgf_parse_result(PgfParse* parse, GuPool* pool) { - PgfLexCallback fn = { pgf_noop }; - GuPool* tmp_pool = gu_new_pool(); - PgfParsing* parsing = pgf_new_parsing(parse->concr, &fn, parse->max_fid, pool, tmp_pool); + PgfParsing* parsing = + pgf_new_parsing(parse->concr, + &lex_callback_noop, + parse->max_fid, + pool, tmp_pool); size_t n_items = gu_buf_length(parse->agenda); for (size_t i = 0; i < n_items; i++) { PgfItem* item = gu_buf_get(parse->agenda, PgfItem*, i); @@ -1164,18 +1319,10 @@ pgf_parse_best_result_init(PgfCCat *ccat, GuBuf *pqueue, case PGF_PRODUCTION_APPLY: { PgfProductionApply* papp = pi.data; - gu_assert(papp->fun->absfun != NULL); - PgfExprState *st = gu_new(PgfExprState, tmp_pool); - st->ep.prob = - log(papp->fun->absfun->prob); - PgfExprFun *expr_fun = - gu_new_variant(PGF_EXPR_FUN, - PgfExprFun, - &st->ep.expr, out_pool); - expr_fun->fun = papp->fun->name; + st->ep = *papp->fun->ep; st->args = papp->args; st->arg_idx = 0; - gu_buf_heap_push(pqueue, &pgf_expr_prob_order, &st); break; } @@ -1274,11 +1421,12 @@ pgf_parse_best_ccat_result( PgfExpr pgf_parse_best_result(PgfParse* parse, GuPool* pool) { - PgfLexCallback fn = { pgf_noop }; - GuPool* tmp_pool = gu_new_pool(); - PgfParsing* parsing = pgf_new_parsing(parse->concr, &fn, parse->max_fid, - pool, tmp_pool); + PgfParsing* parsing = + pgf_new_parsing(parse->concr, + &lex_callback_noop, + parse->max_fid, + pool, tmp_pool); size_t n_items = gu_buf_length(parse->agenda); for (size_t i = 0; i < n_items; i++) { PgfItem* item = gu_buf_get(parse->agenda, PgfItem*, i); @@ -1441,7 +1589,7 @@ pgf_parser_bu_item(PgfConcr* concr, PgfItem* item, break; } case PGF_SYMBOL_LIT: - // XXX TODO proper support + // Nothing to be done here break; case PGF_SYMBOL_VAR: // XXX TODO proper support diff --git a/src/runtime/c/pgf/reader.c b/src/runtime/c/pgf/reader.c index f7b6b02be..76b67abcf 100644 --- a/src/runtime/c/pgf/reader.c +++ b/src/runtime/c/pgf/reader.c @@ -28,6 +28,7 @@ #include #include #include +#include #define GU_LOG_ENABLE #include @@ -550,25 +551,31 @@ pgf_read_to_GuSeq(GuType* type, PgfReader* rdr, void* to) gu_exit("<-"); } -static void -pgf_read_to_PgfEquationsM(GuType* type, PgfReader* rdr, void* to) +static void* +pgf_read_new_PgfFunDecl(GuType* type, PgfReader* rdr, GuPool* pool, size_t* size_out) { - GuSeq* sto = to; + PgfFunDecl* absfun = gu_new(PgfFunDecl, pool); + + absfun->type = pgf_read_new(rdr, gu_type(PgfType), pool, NULL); + gu_return_on_exn(rdr->err, NULL); + + absfun->arity = pgf_read_int(rdr); + uint8_t tag = pgf_read_u8(rdr); - gu_return_on_exn(rdr->err,); + gu_return_on_exn(rdr->err, NULL); switch (tag) { case 0: - *sto = gu_null_seq; + absfun->defns = gu_null_seq; break; case 1: { GuLength length = pgf_read_len(rdr); - gu_return_on_exn(rdr->err, ); + gu_return_on_exn(rdr->err, NULL); - GuSeq seq = gu_new_seq(PgfEquation*, length, rdr->opool); - PgfEquation** data = gu_seq_data(seq); + absfun->defns = gu_new_seq(PgfEquation*, length, rdr->opool); + PgfEquation** data = gu_seq_data(absfun->defns); for (size_t i = 0; i < length; i++) { GuLength n_patts = pgf_read_len(rdr); - gu_return_on_exn(rdr->err, ); + gu_return_on_exn(rdr->err, NULL); PgfEquation *equ = gu_malloc(rdr->opool, @@ -576,14 +583,13 @@ pgf_read_to_PgfEquationsM(GuType* type, PgfReader* rdr, void* to) equ->n_patts = n_patts; for (GuLength j = 0; j < n_patts; j++) { pgf_read_to(rdr, gu_type(PgfPatt), &equ->patts[j]); - gu_return_on_exn(rdr->err, ); + gu_return_on_exn(rdr->err, NULL); } pgf_read_to(rdr, gu_type(PgfExpr), &equ->body); - gu_return_on_exn(rdr->err, ); + gu_return_on_exn(rdr->err, NULL); data[i] = equ; } - *sto = seq; break; } default: @@ -591,6 +597,17 @@ pgf_read_to_PgfEquationsM(GuType* type, PgfReader* rdr, void* to) .type = type, .tag = tag); break; } + + absfun->ep.prob = - log(gu_in_f64be(rdr->in, rdr->err)); + + PgfExprFun* expr_fun = + gu_new_variant(PGF_EXPR_FUN, + PgfExprFun, + &absfun->ep.expr, pool); + expr_fun->fun = *((PgfCId *) rdr->curr_key); + + *size_out = sizeof(PgfFunDecl); + return absfun; } static void @@ -763,8 +780,10 @@ pgf_read_new_PgfConcr(GuType* type, PgfReader* rdr, GuPool* pool, for (int funid = 0; funid < n_funs; funid++) { PgfCncFun* cncfun = gu_list_index(concr->cncfuns, funid); cncfun->funid = funid; - cncfun->absfun = + + PgfFunDecl* absfun = gu_map_get(rdr->curr_abstr->funs, &cncfun->name, PgfFunDecl*); + cncfun->ep = (absfun == NULL) ? NULL : &absfun->ep; } return concr; @@ -840,7 +859,6 @@ pgf_read_to_table = GU_TYPETABLE( PGF_READ_TO(GuString), PGF_READ_TO(double), PGF_READ_TO(pointer), - PGF_READ_TO(PgfEquationsM), PGF_READ_TO(GuSeq), PGF_READ_TO(PgfCCatId), PGF_READ_TO(PgfCCat), @@ -862,6 +880,7 @@ pgf_read_new_table = GU_TYPETABLE( PGF_READ_NEW(struct), PGF_READ_NEW(GuMap), PGF_READ_NEW(GuList), + PGF_READ_NEW(PgfFunDecl), PGF_READ_NEW(PgfCCat), PGF_READ_NEW(PgfCncCat), PGF_READ_NEW(PgfConcr)