From 55d30d70f5ac00945b019648c3d41928c42f38ca Mon Sep 17 00:00:00 2001 From: krangelov Date: Sat, 4 Sep 2021 05:59:11 +0200 Subject: [PATCH] added PgfDBUnmarshaller --- src/runtime/c/pgf/expr.cxx | 117 ++++++++++++++++++++++++++++++++++++- src/runtime/c/pgf/expr.h | 22 ++++++- src/runtime/c/pgf/pgf.cxx | 6 +- 3 files changed, 139 insertions(+), 6 deletions(-) diff --git a/src/runtime/c/pgf/expr.cxx b/src/runtime/c/pgf/expr.cxx index a54729f37..bbb4cfb8f 100644 --- a/src/runtime/c/pgf/expr.cxx +++ b/src/runtime/c/pgf/expr.cxx @@ -1,8 +1,5 @@ #include "pgf/data.h" -PGF_INTERNAL -PgfDBMarshaller db_marshaller; - PgfLiteral PgfDBMarshaller::match_lit(PgfUnmarshaller *u, PgfLiteral l) { switch (ref::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 eabs = + DB::malloc(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::tagged(eabs); +} + +PgfExpr PgfDBUnmarshaller::eapp(PgfExpr fun, PgfExpr arg) +{ + ref eapp = DB::malloc(); + eapp->fun = m->match_expr(this, fun); + eapp->arg = m->match_expr(this, arg); + return ref::tagged(eapp); +} + +PgfExpr PgfDBUnmarshaller::elit(PgfLiteral lit) +{ + ref elit = DB::malloc(); + elit->lit = m->match_lit(this, lit); + return ref::tagged(elit); +} + +PgfExpr PgfDBUnmarshaller::emeta(PgfMetaId meta_id) +{ + ref emeta = DB::malloc(); + emeta->id = meta_id; + return ref::tagged(emeta); +} + +PgfExpr PgfDBUnmarshaller::efun(PgfText *name) +{ + ref efun = + DB::malloc(sizeof(PgfExprFun)+name->size+1); + memcpy(&efun->name, name, sizeof(PgfText)+name->size+1); + return ref::tagged(efun); +} + +PgfExpr PgfDBUnmarshaller::evar(int index) +{ + ref evar = DB::malloc(); + evar->var = index; + return ref::tagged(evar); +} + +PgfExpr PgfDBUnmarshaller::etyped(PgfExpr expr, PgfType ty) +{ + ref etyped = DB::malloc(); + etyped->expr = m->match_expr(this, expr); + etyped->type = m->match_type(this, ty); + return ref::tagged(etyped); +} + +PgfExpr PgfDBUnmarshaller::eimplarg(PgfExpr expr) +{ + ref eimpl = current_db->malloc(); + eimpl->expr = m->match_expr(this, expr); + return ref::tagged(eimpl); +} + +PgfLiteral PgfDBUnmarshaller::lint(size_t size, uintmax_t *val) +{ + ref lit_int = + DB::malloc(sizeof(PgfLiteralInt)+size*sizeof(uintmax_t)); + lit_int->size = size; + memcpy(&lit_int->val, val, size*sizeof(uintmax_t)); + return ref::tagged(lit_int); +} + +PgfLiteral PgfDBUnmarshaller::lflt(double val) +{ + ref lit_flt = DB::malloc(); + lit_flt->val = val; + return ref::tagged(lit_flt); +} + +PgfLiteral PgfDBUnmarshaller::lstr(PgfText *val) +{ + ref lit_str = + DB::malloc(sizeof(PgfLiteralStr)+val->size+1); + memcpy(&lit_str->val, val, sizeof(PgfText)+val->size+1); + return ref::tagged(lit_str); +} + +PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos, + PgfText *cat, + int n_exprs, PgfExpr *exprs) +{ + ref ty = + DB::malloc(sizeof(PgfDTyp)+cat->size+1); + memcpy(&ty->name, cat, sizeof(PgfText)+cat->size+1); + ty->hypos = vector_new(n_hypos); + for (size_t i = 0; i < n_hypos; i++) { + ref hypo = vector_elem(ty->hypos,i); + hypo->bind_type = hypos[i].bind_type; + hypo->cid = DB::malloc(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(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::untagged(x)); +} + PgfExprParser::PgfExprParser(PgfText *input, PgfUnmarshaller *unmarshaller) { inp = input; diff --git a/src/runtime/c/pgf/expr.h b/src/runtime/c/pgf/expr.h index d75a83810..ca035f5a1 100644 --- a/src/runtime/c/pgf/expr.h +++ b/src/runtime/c/pgf/expr.h @@ -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; diff --git a/src/runtime/c/pgf/pgf.cxx b/src/runtime/c/pgf/pgf.cxx index a0e161f25..46afd78d5 100644 --- a/src/runtime/c/pgf/pgf.cxx +++ b/src/runtime/c/pgf/pgf.cxx @@ -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