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;