1
0
forked from GitHub/gf-core

added createCategory, dropCategory

This commit is contained in:
krangelov
2021-09-09 17:33:25 +02:00
parent a44787fc4e
commit f7aad0c0e0
12 changed files with 141 additions and 25 deletions

View File

@@ -988,7 +988,7 @@ PGF_INTERNAL
void PgfDB::link_transient_revision(ref<PgfPGF> pgf)
{
pgf->next = current_db->ms->transient_revisions;
if (current_db->ms->transient_revisions == 0)
if (current_db->ms->transient_revisions != 0)
current_db->ms->transient_revisions->prev = pgf;
current_db->ms->transient_revisions = pgf;
}

View File

@@ -194,9 +194,9 @@ PgfLiteral PgfDBUnmarshaller::lstr(PgfText *val)
return ref<PgfLiteralStr>::tagged(lit_str);
}
PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos,
PgfType PgfDBUnmarshaller::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
int n_exprs, PgfExpr *exprs)
size_t n_exprs, PgfExpr *exprs)
{
ref<PgfDTyp> ty =
PgfDB::malloc<PgfDTyp>(cat->size+1);
@@ -205,8 +205,7 @@ PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos,
for (size_t i = 0; i < n_hypos; i++) {
ref<PgfHypo> hypo = vector_elem(ty->hypos,i);
hypo->bind_type = hypos[i].bind_type;
hypo->cid = PgfDB::malloc<PgfText>(hypos[i].cid->size+1);
memcpy(hypo->cid, hypos[i].cid, sizeof(PgfText)+hypos[i].cid->size+1);
hypo->cid = textdup_db(hypos[i].cid);
hypo->type = m->match_type(this, hypos[i].type);
}
ty->exprs = vector_new<PgfExpr>(n_exprs);

View File

@@ -116,9 +116,9 @@ struct PGF_INTERNAL_DECL PgfDBUnmarshaller : public PgfUnmarshaller {
virtual PgfLiteral lint(size_t size, uintmax_t *val);
virtual PgfLiteral lflt(double val);
virtual PgfLiteral lstr(PgfText *val);
virtual PgfType dtyp(int n_hypos, PgfTypeHypo *hypos,
virtual PgfType dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
int n_exprs, PgfExpr *exprs);
size_t n_exprs, PgfExpr *exprs);
virtual void free_ref(object x);
};

View File

@@ -453,9 +453,7 @@ PgfRevision pgf_clone_revision(PgfDB *db, PgfRevision revision,
if (pgf->gflags != 0)
pgf->gflags->ref_count++;
new_pgf->abstract.name =
PgfDB::malloc<PgfText>(pgf->abstract.name->size+1);
memcpy(new_pgf->abstract.name, pgf->abstract.name, sizeof(PgfText)+pgf->abstract.name->size+1);
new_pgf->abstract.name = textdup_db(&(*pgf->abstract.name));
new_pgf->abstract.aflags = pgf->abstract.aflags;
if (pgf->abstract.aflags != 0)
@@ -495,7 +493,8 @@ void pgf_commit_revision(PgfDB *db, PgfRevision revision,
PgfDB::unlink_transient_revision(new_pgf);
PgfDB::set_revision(new_pgf);
PgfDB::link_transient_revision(old_pgf);
if (old_pgf != 0)
PgfDB::link_transient_revision(old_pgf);
} PGF_API_END
}
@@ -557,3 +556,51 @@ void pgf_drop_function(PgfDB *db, PgfRevision revision,
pgf->abstract.funs = funs;
} PGF_API_END
}
PGF_API
void pgf_create_category(PgfDB *db, PgfRevision revision,
PgfText *name,
size_t n_hypos, PgfTypeHypo *context, prob_t prob,
PgfMarshaller *m,
PgfExn *err)
{
PGF_API_BEGIN {
DB_scope scope(db, WRITER_SCOPE);
PgfDBUnmarshaller u(m);
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
ref<PgfAbsCat> abscat = PgfDB::malloc<PgfAbsCat>(name->size+1);
abscat->context = vector_new<PgfHypo>(n_hypos);
abscat->prob = prob;
memcpy(&abscat->name, name, sizeof(PgfText)+name->size+1);
for (size_t i = 0; i < n_hypos; i++) {
vector_elem(abscat->context, i)->bind_type = context[i].bind_type;
vector_elem(abscat->context, i)->cid = textdup_db(context[i].cid);
vector_elem(abscat->context, i)->type = m->match_type(&u, context[i].type);
}
Namespace<PgfAbsCat> cats =
namespace_insert(pgf->abstract.cats, abscat);
namespace_release(pgf->abstract.cats);
pgf->abstract.cats = cats;
} PGF_API_END
}
PGF_API
void pgf_drop_category(PgfDB *db, PgfRevision revision,
PgfText *name,
PgfExn *err)
{
PGF_API_BEGIN {
DB_scope scope(db, WRITER_SCOPE);
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
Namespace<PgfAbsCat> cats =
namespace_delete(pgf->abstract.cats, name);
namespace_release(pgf->abstract.cats);
pgf->abstract.cats = cats;
} PGF_API_END
}

View File

@@ -164,9 +164,9 @@ struct PgfUnmarshaller {
virtual PgfLiteral lint(size_t size, uintmax_t *v)=0;
virtual PgfLiteral lflt(double v)=0;
virtual PgfLiteral lstr(PgfText *v)=0;
virtual PgfType dtyp(int n_hypos, PgfTypeHypo *hypos,
virtual PgfType dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
int n_exprs, PgfExpr *exprs)=0;
size_t n_exprs, PgfExpr *exprs)=0;
virtual void free_ref(object x)=0;
};
@@ -347,4 +347,16 @@ void pgf_drop_function(PgfDB *db, PgfRevision revision,
PgfText *name,
PgfExn *err);
PGF_API_DECL
void pgf_create_category(PgfDB *db, PgfRevision revision,
PgfText *name,
size_t n_hypos, PgfTypeHypo *context, prob_t prob,
PgfMarshaller *m,
PgfExn *err);
PGF_API_DECL
void pgf_drop_category(PgfDB *db, PgfRevision revision,
PgfText *name,
PgfExn *err);
#endif // PGF_H_

View File

@@ -380,9 +380,9 @@ PgfLiteral PgfPrinter::lstr(PgfText *v)
return 0;
}
PgfType PgfPrinter::dtyp(int n_hypos, PgfTypeHypo *hypos,
PgfType PgfPrinter::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
int n_exprs, PgfExpr *exprs)
size_t n_exprs, PgfExpr *exprs)
{
bool p = (prio > 0 && n_hypos > 0) ||
(prio > 3 && n_exprs > 0);

View File

@@ -61,9 +61,9 @@ public:
virtual PgfLiteral lint(size_t size, uintmax_t *v);
virtual PgfLiteral lflt(double v);
virtual PgfLiteral lstr(PgfText *v);
virtual PgfType dtyp(int n_hypos, PgfTypeHypo *hypos,
virtual PgfType dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
int n_exprs, PgfExpr *exprs);
size_t n_exprs, PgfExpr *exprs);
virtual void free_ref(object x);
};

View File

@@ -26,6 +26,14 @@ PgfText* textdup(PgfText *t1)
return t2;
}
PGF_INTERNAL
ref<PgfText> textdup_db(PgfText *t1)
{
ref<PgfText> t2 = PgfDB::malloc<PgfText>(t1->size+1);
memcpy(&(*t2), t1, sizeof(PgfText)+t1->size+1);
return t2;
}
PGF_API uint32_t
pgf_utf8_decode(const uint8_t** src_inout)
{

View File

@@ -7,6 +7,9 @@ int textcmp(PgfText *t1, PgfText *t2);
PGF_INTERNAL_DECL
PgfText* textdup(PgfText *t1);
PGF_INTERNAL_DECL
ref<PgfText> textdup_db(PgfText *t1);
PGF_API uint32_t
pgf_utf8_decode(const uint8_t** src_inout);