mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-21 00:52:51 -06:00
libpgf: implementation for built in literal categories
This commit is contained in:
@@ -209,6 +209,109 @@ gu_str_string(const char* str, GuPool* pool)
|
|||||||
#endif
|
#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
|
GuWord
|
||||||
gu_string_hash(GuString s)
|
gu_string_hash(GuString s)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -63,6 +63,12 @@ gu_format_string(GuPool* pool, const char* fmt, ...);
|
|||||||
GuString
|
GuString
|
||||||
gu_str_string(const char* str, GuPool* pool);
|
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_
|
#endif // GU_STRING_H_
|
||||||
|
|
||||||
#if defined(GU_HASH_H_) && !defined(GU_STRING_H_HASH_)
|
#if defined(GU_HASH_H_) && !defined(GU_STRING_H_HASH_)
|
||||||
|
|||||||
@@ -4,27 +4,6 @@
|
|||||||
#include <gu/variant.h>
|
#include <gu/variant.h>
|
||||||
#include <gu/assert.h>
|
#include <gu/assert.h>
|
||||||
|
|
||||||
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
|
bool
|
||||||
pgf_tokens_equal(PgfTokens t1, PgfTokens t2)
|
pgf_tokens_equal(PgfTokens t1, PgfTokens t2)
|
||||||
{
|
{
|
||||||
@@ -145,11 +124,6 @@ GU_DEFINE_TYPE(
|
|||||||
GU_CONSTRUCTOR_S(
|
GU_CONSTRUCTOR_S(
|
||||||
PGF_PRODUCTION_COERCE, PgfProductionCoerce,
|
PGF_PRODUCTION_COERCE, PgfProductionCoerce,
|
||||||
GU_MEMBER(PgfProductionCoerce, coerce, PgfCCatId)),
|
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(
|
GU_CONSTRUCTOR_S(
|
||||||
PGF_PRODUCTION_META, PgfProductionMeta,
|
PGF_PRODUCTION_META, PgfProductionMeta,
|
||||||
GU_MEMBER(PgfProductionMeta, args, PgfPArgs)));
|
GU_MEMBER(PgfProductionMeta, args, PgfPArgs)));
|
||||||
@@ -192,12 +166,16 @@ GU_DEFINE_TYPE(
|
|||||||
// Distinct type so we can give it special treatment in the reader
|
// Distinct type so we can give it special treatment in the reader
|
||||||
GU_DEFINE_TYPE(PgfEquationsM, GuSeq, gu_type(PgfEquation));
|
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(
|
GU_DEFINE_TYPE(
|
||||||
PgfFunDecl, struct,
|
PgfFunDecl, struct,
|
||||||
GU_MEMBER_P(PgfFunDecl, type, PgfType),
|
GU_MEMBER_P(PgfFunDecl, type, PgfType),
|
||||||
GU_MEMBER(PgfFunDecl, arity, int),
|
GU_MEMBER(PgfFunDecl, arity, int),
|
||||||
GU_MEMBER(PgfFunDecl, defns, PgfEquationsM),
|
GU_MEMBER(PgfFunDecl, defns, PgfEquationsM),
|
||||||
GU_MEMBER(PgfFunDecl, prob, double));
|
GU_MEMBER(PgfFunDecl, ep, PgfExprProb));
|
||||||
|
|
||||||
GU_DEFINE_TYPE(
|
GU_DEFINE_TYPE(
|
||||||
PgfCatFun, struct,
|
PgfCatFun, struct,
|
||||||
|
|||||||
@@ -124,13 +124,22 @@ struct PgfPGF {
|
|||||||
|
|
||||||
extern GU_DECLARE_TYPE(PgfPGF, struct);
|
extern GU_DECLARE_TYPE(PgfPGF, struct);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double prob;
|
||||||
|
PgfExpr expr;
|
||||||
|
} PgfExprProb;
|
||||||
|
|
||||||
|
extern GU_DECLARE_TYPE(PgfExprProb, struct);
|
||||||
|
|
||||||
struct PgfFunDecl {
|
struct PgfFunDecl {
|
||||||
PgfType* type;
|
PgfType* type;
|
||||||
int arity; // Only for computational defs?
|
int arity;
|
||||||
PgfEquationsM defns; // maybe null
|
PgfEquationsM defns; // maybe null
|
||||||
double prob;
|
PgfExprProb ep;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern GU_DECLARE_TYPE(PgfFunDecl, struct);
|
||||||
|
|
||||||
struct PgfCatFun {
|
struct PgfCatFun {
|
||||||
double prob;
|
double prob;
|
||||||
PgfCId fun;
|
PgfCId fun;
|
||||||
@@ -160,7 +169,7 @@ struct PgfCncCat {
|
|||||||
|
|
||||||
struct PgfCncFun {
|
struct PgfCncFun {
|
||||||
PgfCId name;
|
PgfCId name;
|
||||||
PgfFunDecl *absfun;
|
PgfExprProb *ep;
|
||||||
int funid;
|
int funid;
|
||||||
GuLength n_lins;
|
GuLength n_lins;
|
||||||
PgfSeqId lins[];
|
PgfSeqId lins[];
|
||||||
@@ -260,7 +269,6 @@ typedef struct PgfSymbolKP
|
|||||||
typedef enum {
|
typedef enum {
|
||||||
PGF_PRODUCTION_APPLY,
|
PGF_PRODUCTION_APPLY,
|
||||||
PGF_PRODUCTION_COERCE,
|
PGF_PRODUCTION_COERCE,
|
||||||
PGF_PRODUCTION_CONST,
|
|
||||||
PGF_PRODUCTION_META
|
PGF_PRODUCTION_META
|
||||||
} PgfProductionTag;
|
} PgfProductionTag;
|
||||||
|
|
||||||
@@ -307,9 +315,6 @@ extern GU_DECLARE_TYPE(PgfBindType, enum);
|
|||||||
extern GU_DECLARE_TYPE(PgfLiteral, GuVariant);
|
extern GU_DECLARE_TYPE(PgfLiteral, GuVariant);
|
||||||
|
|
||||||
|
|
||||||
PgfCCatId
|
|
||||||
pgf_literal_cat(PgfLiteral lit);
|
|
||||||
|
|
||||||
// PgfPatt
|
// PgfPatt
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|||||||
@@ -311,7 +311,7 @@ pgf_print_literal(PgfLiteral lit,
|
|||||||
}
|
}
|
||||||
case PGF_LITERAL_FLT: {
|
case PGF_LITERAL_FLT: {
|
||||||
PgfLiteralFlt* lit = ei.data;
|
PgfLiteralFlt* lit = ei.data;
|
||||||
gu_printf(wtr, err, "%f", lit->val);
|
gu_printf(wtr, err, "%lf", lit->val);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -338,6 +338,29 @@ finish:
|
|||||||
return ret;
|
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*
|
static PgfCCat*
|
||||||
pgf_lzn_infer(PgfLzn* lzn, PgfExpr expr, GuPool* pool, PgfCncTree* ctree_out)
|
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,
|
PgfCncTreeLit,
|
||||||
.lit = elit->lit);
|
.lit = elit->lit);
|
||||||
}
|
}
|
||||||
ret = pgf_literal_cat(elit->lit);
|
ret = pgf_literal_cat(lzn, elit->lit);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// XXX: should we do something here?
|
// 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 = {
|
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
|
void
|
||||||
|
|||||||
@@ -20,21 +20,12 @@ struct PgfParse {
|
|||||||
int max_fid;
|
int max_fid;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
double prob;
|
|
||||||
PgfExpr expr;
|
|
||||||
} PgfExprProb;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PgfExprProb ep;
|
PgfExprProb ep;
|
||||||
PgfPArgs args;
|
PgfPArgs args;
|
||||||
size_t arg_idx;
|
size_t arg_idx;
|
||||||
} PgfExprState;
|
} PgfExprState;
|
||||||
|
|
||||||
static GU_DEFINE_TYPE(PgfExprProb, struct,
|
|
||||||
GU_MEMBER(PgfExprProb, prob, double),
|
|
||||||
GU_MEMBER(PgfExprProb, expr, PgfExpr));
|
|
||||||
|
|
||||||
typedef GuMap PgfExprCache;
|
typedef GuMap PgfExprCache;
|
||||||
static GU_DEFINE_TYPE(PgfExprCache, GuMap,
|
static GU_DEFINE_TYPE(PgfExprCache, GuMap,
|
||||||
gu_type(PgfCCat), NULL,
|
gu_type(PgfCCat), NULL,
|
||||||
@@ -103,10 +94,16 @@ GU_DEFINE_TYPE(PgfTransitions, GuStringMap,
|
|||||||
|
|
||||||
typedef struct PgfParsing PgfParsing;
|
typedef struct PgfParsing PgfParsing;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PgfTokens tokens;
|
||||||
|
PgfExprProb ep;
|
||||||
|
} PgfLiteralCandidate;
|
||||||
|
|
||||||
typedef const struct PgfLexCallback PgfLexCallback;
|
typedef const struct PgfLexCallback PgfLexCallback;
|
||||||
|
|
||||||
struct PgfLexCallback {
|
struct PgfLexCallback {
|
||||||
void (*lex)(PgfLexCallback* self, PgfToken tok, PgfItem* item);
|
void (*lex)(PgfLexCallback* self, PgfToken tok, PgfItem* item);
|
||||||
|
GuEnum *(*lit)(PgfLexCallback* self, PgfCCat* cat);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PgfParsing {
|
struct PgfParsing {
|
||||||
@@ -133,7 +130,7 @@ pgf_print_production(int fid, PgfProduction prod, GuWriter *wtr, GuExn* err)
|
|||||||
case PGF_PRODUCTION_APPLY: {
|
case PGF_PRODUCTION_APPLY: {
|
||||||
PgfProductionApply* papp = i.data;
|
PgfProductionApply* papp = i.data;
|
||||||
gu_printf(wtr,err,"F%d(",papp->fun->funid);
|
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,")[");
|
gu_printf(wtr,err,")[");
|
||||||
size_t n_args = gu_seq_length(papp->args);
|
size_t n_args = gu_seq_length(papp->args);
|
||||||
for (size_t j = 0; j < n_args; j++) {
|
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;
|
PgfProductionApply* papp = i.data;
|
||||||
PgfCncFun* fun = papp->fun;
|
PgfCncFun* fun = papp->fun;
|
||||||
gu_printf(wtr, err, "F%d(", fun->funid);
|
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, ")[");
|
gu_printf(wtr, err, ")[");
|
||||||
for (size_t i = 0; i < gu_seq_length(item->args); i++) {
|
for (size_t i = 0; i < gu_seq_length(item->args); i++) {
|
||||||
PgfPArg arg = gu_seq_get(item->args, PgfPArg, 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;
|
PgfItem* item = NULL;
|
||||||
|
|
||||||
if (!gu_variant_is_null(cont->curr_sym)) {
|
if (!gu_variant_is_null(cont->curr_sym)) {
|
||||||
gu_assert(gu_variant_tag(cont->curr_sym) == PGF_SYMBOL_CAT);
|
switch (gu_variant_tag(cont->curr_sym)) {
|
||||||
|
case PGF_SYMBOL_CAT: {
|
||||||
PgfSymbolCat* scat = gu_variant_data(cont->curr_sym);
|
PgfSymbolCat* scat = gu_variant_data(cont->curr_sym);
|
||||||
|
|
||||||
item = pgf_item_update_arg(cont, scat->d, cat, parsing->pool);
|
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 {
|
} else {
|
||||||
item = pgf_item_copy(cont, parsing->pool);
|
item = pgf_item_copy(cont, parsing->pool);
|
||||||
size_t nargs = gu_seq_length(cont->args);
|
size_t nargs = gu_seq_length(cont->args);
|
||||||
@@ -772,9 +779,66 @@ pgf_parsing_symbol(PgfParsing* parsing, PgfItem* item, PgfSymbol sym) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PGF_SYMBOL_LIT:
|
case PGF_SYMBOL_LIT: {
|
||||||
// XXX TODO proper support
|
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;
|
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:
|
case PGF_SYMBOL_VAR:
|
||||||
// XXX TODO proper support
|
// XXX TODO proper support
|
||||||
break;
|
break;
|
||||||
@@ -864,10 +928,29 @@ pgf_new_parse(PgfConcr* concr, int max_fid, GuPool* pool)
|
|||||||
return parse;
|
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 {
|
typedef struct {
|
||||||
PgfLexCallback fn;
|
PgfLexCallback fn;
|
||||||
PgfToken tok;
|
PgfToken tok;
|
||||||
PgfItemBuf* agenda;
|
PgfItemBuf* agenda;
|
||||||
|
GuPool *pool;
|
||||||
} PgfParseTokenCallback;
|
} PgfParseTokenCallback;
|
||||||
|
|
||||||
static
|
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 {
|
typedef struct {
|
||||||
GuMapItor fn;
|
GuMapItor fn;
|
||||||
PgfProduction prod;
|
PgfProduction prod;
|
||||||
@@ -924,7 +1080,8 @@ pgf_parse_token(PgfParse* parse, PgfToken tok, bool robust, GuPool* pool)
|
|||||||
{
|
{
|
||||||
PgfItemBuf* agenda = gu_new_buf(PgfItem*, 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();
|
GuPool* tmp_pool = gu_new_pool();
|
||||||
PgfParsing* parsing = pgf_new_parsing(parse->concr, &clo1.fn, parse->max_fid, pool, tmp_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) {
|
switch (pi.tag) {
|
||||||
case PGF_PRODUCTION_APPLY: {
|
case PGF_PRODUCTION_APPLY: {
|
||||||
PgfProductionApply* papp = pi.data;
|
PgfProductionApply* papp = pi.data;
|
||||||
PgfExpr expr = gu_new_variant_i(pool, PGF_EXPR_FUN,
|
PgfExpr expr = papp->fun->ep->expr;
|
||||||
PgfExprFun,
|
|
||||||
.fun = papp->fun->name);
|
|
||||||
size_t n_args = gu_seq_length(papp->args);
|
size_t n_args = gu_seq_length(papp->args);
|
||||||
for (size_t i = 0; i < n_args; i++) {
|
for (size_t i = 0; i < n_args; i++) {
|
||||||
PgfPArg* parg = gu_seq_index(papp->args, PgfPArg, 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);
|
*(PgfExpr*)to = pgf_parse_result_next(pr, pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static PgfLexCallback lex_callback_noop =
|
||||||
void pgf_noop(PgfLexCallback* self, PgfToken tok, PgfItem* item)
|
{ pgf_lex_noop, pgf_lit_noop };
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
PgfExprEnum*
|
PgfExprEnum*
|
||||||
pgf_parse_result(PgfParse* parse, GuPool* pool)
|
pgf_parse_result(PgfParse* parse, GuPool* pool)
|
||||||
{
|
{
|
||||||
PgfLexCallback fn = { pgf_noop };
|
|
||||||
|
|
||||||
GuPool* tmp_pool = gu_new_pool();
|
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);
|
size_t n_items = gu_buf_length(parse->agenda);
|
||||||
for (size_t i = 0; i < n_items; i++) {
|
for (size_t i = 0; i < n_items; i++) {
|
||||||
PgfItem* item = gu_buf_get(parse->agenda, PgfItem*, 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: {
|
case PGF_PRODUCTION_APPLY: {
|
||||||
PgfProductionApply* papp = pi.data;
|
PgfProductionApply* papp = pi.data;
|
||||||
|
|
||||||
gu_assert(papp->fun->absfun != NULL);
|
|
||||||
|
|
||||||
PgfExprState *st = gu_new(PgfExprState, tmp_pool);
|
PgfExprState *st = gu_new(PgfExprState, tmp_pool);
|
||||||
st->ep.prob = - log(papp->fun->absfun->prob);
|
st->ep = *papp->fun->ep;
|
||||||
PgfExprFun *expr_fun =
|
|
||||||
gu_new_variant(PGF_EXPR_FUN,
|
|
||||||
PgfExprFun,
|
|
||||||
&st->ep.expr, out_pool);
|
|
||||||
expr_fun->fun = papp->fun->name;
|
|
||||||
st->args = papp->args;
|
st->args = papp->args;
|
||||||
st->arg_idx = 0;
|
st->arg_idx = 0;
|
||||||
|
|
||||||
gu_buf_heap_push(pqueue, &pgf_expr_prob_order, &st);
|
gu_buf_heap_push(pqueue, &pgf_expr_prob_order, &st);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1274,10 +1421,11 @@ pgf_parse_best_ccat_result(
|
|||||||
PgfExpr
|
PgfExpr
|
||||||
pgf_parse_best_result(PgfParse* parse, GuPool* pool)
|
pgf_parse_best_result(PgfParse* parse, GuPool* pool)
|
||||||
{
|
{
|
||||||
PgfLexCallback fn = { pgf_noop };
|
|
||||||
|
|
||||||
GuPool* tmp_pool = gu_new_pool();
|
GuPool* tmp_pool = gu_new_pool();
|
||||||
PgfParsing* parsing = pgf_new_parsing(parse->concr, &fn, parse->max_fid,
|
PgfParsing* parsing =
|
||||||
|
pgf_new_parsing(parse->concr,
|
||||||
|
&lex_callback_noop,
|
||||||
|
parse->max_fid,
|
||||||
pool, tmp_pool);
|
pool, tmp_pool);
|
||||||
size_t n_items = gu_buf_length(parse->agenda);
|
size_t n_items = gu_buf_length(parse->agenda);
|
||||||
for (size_t i = 0; i < n_items; i++) {
|
for (size_t i = 0; i < n_items; i++) {
|
||||||
@@ -1441,7 +1589,7 @@ pgf_parser_bu_item(PgfConcr* concr, PgfItem* item,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PGF_SYMBOL_LIT:
|
case PGF_SYMBOL_LIT:
|
||||||
// XXX TODO proper support
|
// Nothing to be done here
|
||||||
break;
|
break;
|
||||||
case PGF_SYMBOL_VAR:
|
case PGF_SYMBOL_VAR:
|
||||||
// XXX TODO proper support
|
// XXX TODO proper support
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include <gu/bits.h>
|
#include <gu/bits.h>
|
||||||
#include <gu/exn.h>
|
#include <gu/exn.h>
|
||||||
#include <gu/utf8.h>
|
#include <gu/utf8.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#define GU_LOG_ENABLE
|
#define GU_LOG_ENABLE
|
||||||
#include <gu/log.h>
|
#include <gu/log.h>
|
||||||
@@ -550,25 +551,31 @@ pgf_read_to_GuSeq(GuType* type, PgfReader* rdr, void* to)
|
|||||||
gu_exit("<-");
|
gu_exit("<-");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void*
|
||||||
pgf_read_to_PgfEquationsM(GuType* type, PgfReader* rdr, void* to)
|
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);
|
uint8_t tag = pgf_read_u8(rdr);
|
||||||
gu_return_on_exn(rdr->err,);
|
gu_return_on_exn(rdr->err, NULL);
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
case 0:
|
case 0:
|
||||||
*sto = gu_null_seq;
|
absfun->defns = gu_null_seq;
|
||||||
break;
|
break;
|
||||||
case 1: {
|
case 1: {
|
||||||
GuLength length = pgf_read_len(rdr);
|
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);
|
absfun->defns = gu_new_seq(PgfEquation*, length, rdr->opool);
|
||||||
PgfEquation** data = gu_seq_data(seq);
|
PgfEquation** data = gu_seq_data(absfun->defns);
|
||||||
for (size_t i = 0; i < length; i++) {
|
for (size_t i = 0; i < length; i++) {
|
||||||
GuLength n_patts = pgf_read_len(rdr);
|
GuLength n_patts = pgf_read_len(rdr);
|
||||||
gu_return_on_exn(rdr->err, );
|
gu_return_on_exn(rdr->err, NULL);
|
||||||
|
|
||||||
PgfEquation *equ =
|
PgfEquation *equ =
|
||||||
gu_malloc(rdr->opool,
|
gu_malloc(rdr->opool,
|
||||||
@@ -576,14 +583,13 @@ pgf_read_to_PgfEquationsM(GuType* type, PgfReader* rdr, void* to)
|
|||||||
equ->n_patts = n_patts;
|
equ->n_patts = n_patts;
|
||||||
for (GuLength j = 0; j < n_patts; j++) {
|
for (GuLength j = 0; j < n_patts; j++) {
|
||||||
pgf_read_to(rdr, gu_type(PgfPatt), &equ->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);
|
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;
|
data[i] = equ;
|
||||||
}
|
}
|
||||||
*sto = seq;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -591,6 +597,17 @@ pgf_read_to_PgfEquationsM(GuType* type, PgfReader* rdr, void* to)
|
|||||||
.type = type, .tag = tag);
|
.type = type, .tag = tag);
|
||||||
break;
|
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
|
static void
|
||||||
@@ -763,8 +780,10 @@ pgf_read_new_PgfConcr(GuType* type, PgfReader* rdr, GuPool* pool,
|
|||||||
for (int funid = 0; funid < n_funs; funid++) {
|
for (int funid = 0; funid < n_funs; funid++) {
|
||||||
PgfCncFun* cncfun = gu_list_index(concr->cncfuns, funid);
|
PgfCncFun* cncfun = gu_list_index(concr->cncfuns, funid);
|
||||||
cncfun->funid = funid;
|
cncfun->funid = funid;
|
||||||
cncfun->absfun =
|
|
||||||
|
PgfFunDecl* absfun =
|
||||||
gu_map_get(rdr->curr_abstr->funs, &cncfun->name, PgfFunDecl*);
|
gu_map_get(rdr->curr_abstr->funs, &cncfun->name, PgfFunDecl*);
|
||||||
|
cncfun->ep = (absfun == NULL) ? NULL : &absfun->ep;
|
||||||
}
|
}
|
||||||
|
|
||||||
return concr;
|
return concr;
|
||||||
@@ -840,7 +859,6 @@ pgf_read_to_table = GU_TYPETABLE(
|
|||||||
PGF_READ_TO(GuString),
|
PGF_READ_TO(GuString),
|
||||||
PGF_READ_TO(double),
|
PGF_READ_TO(double),
|
||||||
PGF_READ_TO(pointer),
|
PGF_READ_TO(pointer),
|
||||||
PGF_READ_TO(PgfEquationsM),
|
|
||||||
PGF_READ_TO(GuSeq),
|
PGF_READ_TO(GuSeq),
|
||||||
PGF_READ_TO(PgfCCatId),
|
PGF_READ_TO(PgfCCatId),
|
||||||
PGF_READ_TO(PgfCCat),
|
PGF_READ_TO(PgfCCat),
|
||||||
@@ -862,6 +880,7 @@ pgf_read_new_table = GU_TYPETABLE(
|
|||||||
PGF_READ_NEW(struct),
|
PGF_READ_NEW(struct),
|
||||||
PGF_READ_NEW(GuMap),
|
PGF_READ_NEW(GuMap),
|
||||||
PGF_READ_NEW(GuList),
|
PGF_READ_NEW(GuList),
|
||||||
|
PGF_READ_NEW(PgfFunDecl),
|
||||||
PGF_READ_NEW(PgfCCat),
|
PGF_READ_NEW(PgfCCat),
|
||||||
PGF_READ_NEW(PgfCncCat),
|
PGF_READ_NEW(PgfCncCat),
|
||||||
PGF_READ_NEW(PgfConcr)
|
PGF_READ_NEW(PgfConcr)
|
||||||
|
|||||||
Reference in New Issue
Block a user