1
0
forked from GitHub/gf-core

added PgfDBUnmarshaller

This commit is contained in:
krangelov
2021-09-04 05:59:11 +02:00
parent b4838649f5
commit 55d30d70f5
3 changed files with 139 additions and 6 deletions

View File

@@ -1,8 +1,5 @@
#include "pgf/data.h"
PGF_INTERNAL
PgfDBMarshaller db_marshaller;
PgfLiteral PgfDBMarshaller::match_lit(PgfUnmarshaller *u, PgfLiteral l)
{
switch (ref<PgfLiteral>::get_tag(l)) {
@@ -111,6 +108,120 @@ PgfType PgfDBMarshaller::match_type(PgfUnmarshaller *u, PgfType ty)
return res;
}
PgfExpr PgfDBUnmarshaller::eabs(PgfBindType bind_type, PgfText *name, PgfExpr body)
{
ref<PgfExprAbs> eabs =
DB::malloc<PgfExprAbs>(sizeof(PgfExprAbs)+name->size+1);
eabs->bind_type = bind_type;
eabs->body = m->match_expr(this, body);
memcpy(&eabs->name, name, sizeof(PgfText)+name->size+1);
return ref<PgfExprAbs>::tagged(eabs);
}
PgfExpr PgfDBUnmarshaller::eapp(PgfExpr fun, PgfExpr arg)
{
ref<PgfExprApp> eapp = DB::malloc<PgfExprApp>();
eapp->fun = m->match_expr(this, fun);
eapp->arg = m->match_expr(this, arg);
return ref<PgfExprApp>::tagged(eapp);
}
PgfExpr PgfDBUnmarshaller::elit(PgfLiteral lit)
{
ref<PgfExprLit> elit = DB::malloc<PgfExprLit>();
elit->lit = m->match_lit(this, lit);
return ref<PgfExprLit>::tagged(elit);
}
PgfExpr PgfDBUnmarshaller::emeta(PgfMetaId meta_id)
{
ref<PgfExprMeta> emeta = DB::malloc<PgfExprMeta>();
emeta->id = meta_id;
return ref<PgfExprMeta>::tagged(emeta);
}
PgfExpr PgfDBUnmarshaller::efun(PgfText *name)
{
ref<PgfExprFun> efun =
DB::malloc<PgfExprFun>(sizeof(PgfExprFun)+name->size+1);
memcpy(&efun->name, name, sizeof(PgfText)+name->size+1);
return ref<PgfExprFun>::tagged(efun);
}
PgfExpr PgfDBUnmarshaller::evar(int index)
{
ref<PgfExprVar> evar = DB::malloc<PgfExprVar>();
evar->var = index;
return ref<PgfExprVar>::tagged(evar);
}
PgfExpr PgfDBUnmarshaller::etyped(PgfExpr expr, PgfType ty)
{
ref<PgfExprTyped> etyped = DB::malloc<PgfExprTyped>();
etyped->expr = m->match_expr(this, expr);
etyped->type = m->match_type(this, ty);
return ref<PgfExprTyped>::tagged(etyped);
}
PgfExpr PgfDBUnmarshaller::eimplarg(PgfExpr expr)
{
ref<PgfExprImplArg> eimpl = current_db->malloc<PgfExprImplArg>();
eimpl->expr = m->match_expr(this, expr);
return ref<PgfExprImplArg>::tagged(eimpl);
}
PgfLiteral PgfDBUnmarshaller::lint(size_t size, uintmax_t *val)
{
ref<PgfLiteralInt> lit_int =
DB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+size*sizeof(uintmax_t));
lit_int->size = size;
memcpy(&lit_int->val, val, size*sizeof(uintmax_t));
return ref<PgfLiteralInt>::tagged(lit_int);
}
PgfLiteral PgfDBUnmarshaller::lflt(double val)
{
ref<PgfLiteralFlt> lit_flt = DB::malloc<PgfLiteralFlt>();
lit_flt->val = val;
return ref<PgfLiteralFlt>::tagged(lit_flt);
}
PgfLiteral PgfDBUnmarshaller::lstr(PgfText *val)
{
ref<PgfLiteralStr> lit_str =
DB::malloc<PgfLiteralStr>(sizeof(PgfLiteralStr)+val->size+1);
memcpy(&lit_str->val, val, sizeof(PgfText)+val->size+1);
return ref<PgfLiteralStr>::tagged(lit_str);
}
PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
int n_exprs, PgfExpr *exprs)
{
ref<PgfDTyp> ty =
DB::malloc<PgfDTyp>(sizeof(PgfDTyp)+cat->size+1);
memcpy(&ty->name, cat, sizeof(PgfText)+cat->size+1);
ty->hypos = vector_new<PgfHypo>(n_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 = DB::malloc<PgfText>(sizeof(PgfText)+hypos[i].cid->size+1);
memcpy(&hypo->cid->text, hypos[i].cid, sizeof(PgfText)+hypos[i].cid->size+1);
hypo->type = m->match_type(this, hypos[i].type);
}
ty->exprs = vector_new<PgfExpr>(n_exprs);
for (size_t i = 0; i < n_exprs; i++) {
vector_elem(ty->exprs,i) = exprs[i];
}
return ty.as_object();
}
void PgfDBUnmarshaller::free_ref(object x)
{
DB::free(ref<void>::untagged(x));
}
PgfExprParser::PgfExprParser(PgfText *input, PgfUnmarshaller *unmarshaller)
{
inp = input;

View File

@@ -100,7 +100,27 @@ struct PGF_INTERNAL_DECL PgfDBMarshaller : public PgfMarshaller {
virtual object match_type(PgfUnmarshaller *u, PgfType ty);
};
extern PGF_INTERNAL_DECL PgfDBMarshaller db_marshaller;
struct PGF_INTERNAL_DECL PgfDBUnmarshaller : public PgfUnmarshaller {
PgfMarshaller *m;
PgfDBUnmarshaller(PgfMarshaller *marshaller) { m = marshaller; }
virtual PgfExpr eabs(PgfBindType bind_type, PgfText *name, PgfExpr body);
virtual PgfExpr eapp(PgfExpr fun, PgfExpr arg);
virtual PgfExpr elit(PgfLiteral lit);
virtual PgfExpr emeta(PgfMetaId meta_id);
virtual PgfExpr efun(PgfText *name);
virtual PgfExpr evar(int index);
virtual PgfExpr etyped(PgfExpr expr, PgfType ty);
virtual PgfExpr eimplarg(PgfExpr expr);
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,
PgfText *cat,
int n_exprs, PgfExpr *exprs);
virtual void free_ref(object x);
};
typedef struct PgfBind {
PgfBindType bind_type;

View File

@@ -213,12 +213,14 @@ PgfTypeHypo *pgf_category_context(PgfPGF *pgf, PgfText *catname, size_t *n_hypos
return NULL;
}
PgfDBMarshaller m;
PgfTypeHypo *hypos = (PgfTypeHypo *)
malloc(abscat->context->len * sizeof(PgfTypeHypo));
for (size_t i = 0; i < abscat->context->len; i++) {
hypos[i].bind_type = abscat->context->data[i].bind_type;
hypos[i].cid = textdup(abscat->context->data[i].cid);
hypos[i].type = db_marshaller.match_type(u, abscat->context->data[i].type.as_object());
hypos[i].type = m.match_type(u, abscat->context->data[i].type.as_object());
}
*n_hypos = abscat->context->len;
@@ -288,7 +290,7 @@ PgfType pgf_function_type(PgfPGF *pgf, PgfText *funname, PgfUnmarshaller *u)
if (absfun == 0)
return 0;
return db_marshaller.match_type(u, absfun->type.as_object());
return PgfDBMarshaller().match_type(u, absfun->type.as_object());
}
PGF_API