added showContext

This commit is contained in:
krangelov
2021-09-14 19:10:01 +02:00
parent 3675e5cfc6
commit a7bf47cb87
6 changed files with 51 additions and 18 deletions

View File

@@ -467,6 +467,20 @@ PgfText *pgf_print_type(PgfType ty,
return printer.get_text();
}
PGF_API
PgfText *pgf_print_context(size_t n_hypos, PgfTypeHypo *hypos,
PgfPrintContext *ctxt, int prio,
PgfMarshaller *m)
{
PgfPrinter printer(ctxt,prio,m);
for (size_t i = 0; i < n_hypos; i++) {
if (i > 0)
printer.puts(" ");
printer.hypo(&hypos[i]);
}
return printer.get_text();
}
PGF_API
PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u)
{

View File

@@ -331,6 +331,11 @@ PgfText *pgf_print_type(PgfType ty,
PgfPrintContext *ctxt, int prio,
PgfMarshaller *m);
PGF_API_DECL
PgfText *pgf_print_context(size_t n_hypos, PgfTypeHypo *hypos,
PgfPrintContext *ctxt, int prio,
PgfMarshaller *m);
PGF_API_DECL
PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u);

View File

@@ -380,6 +380,26 @@ PgfLiteral PgfPrinter::lstr(PgfText *v)
return 0;
}
void PgfPrinter::hypo(PgfTypeHypo *hypo)
{
if (textcmp(hypo->cid, &wildcard) == 0) {
prio = 1;
m->match_type(this, hypo->type);
} else {
push_variable(hypo->cid);
puts("(");
if (hypo->bind_type == PGF_BIND_TYPE_IMPLICIT)
puts("{");
puts(&ctxt->name);
if (hypo->bind_type == PGF_BIND_TYPE_IMPLICIT)
puts("}");
puts(" : ");
m->match_type(this, hypo->type);
puts(")");
}
}
PgfType PgfPrinter::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfText *cat,
size_t n_exprs, PgfExpr *exprs)
@@ -391,23 +411,7 @@ PgfType PgfPrinter::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
PgfPrintContext *save_ctxt = ctxt;
for (int i = 0; i < n_hypos; i++) {
if (textcmp(hypos[i].cid, &wildcard) == 0) {
prio = 1;
m->match_type(this, hypos[i].type);
} else {
push_variable(hypos[i].cid);
puts("(");
if (hypos[i].bind_type == PGF_BIND_TYPE_IMPLICIT)
puts("{");
puts(&ctxt->name);
if (hypos[i].bind_type == PGF_BIND_TYPE_IMPLICIT)
puts("}");
puts(" : ");
m->match_type(this, hypos[i].type);
puts(")");
}
hypo(&hypos[i]);
puts(" -> ");
}

View File

@@ -50,6 +50,8 @@ public:
PgfText *get_text();
void hypo(PgfTypeHypo *hypo);
virtual PgfExpr eabs(PgfBindType btype, PgfText *name, PgfExpr body);
virtual PgfExpr eapp(PgfExpr fun, PgfExpr arg);
virtual PgfExpr elit(PgfLiteral lit);

View File

@@ -912,7 +912,13 @@ showType scope ty =
peekText c_text
showContext :: [Var] -> [(BindType,Var,Type)] -> String
showContext = error "TODO: showContext"
showContext scope hypos =
unsafePerformIO $
withHypos hypos $ \n_hypos c_hypos ->
bracket (newPrintCtxt scope) freePrintCtxt $ \pctxt ->
withForeignPtr marshaller $ \m ->
bracket (pgf_print_context n_hypos c_hypos pctxt 0 m) free $ \c_text ->
peekText c_text
-- | parses a 'String' as a type
readType :: String -> Maybe Type

View File

@@ -78,6 +78,8 @@ foreign import ccall pgf_read_expr_ex :: Ptr PgfText -> Ptr CString -> Ptr PgfUn
foreign import ccall "pgf_print_type"
pgf_print_type :: StablePtr Type -> Ptr PgfPrintContext -> CInt -> Ptr PgfMarshaller -> IO (Ptr PgfText)
foreign import ccall pgf_print_context :: CSize -> Ptr PgfTypeHypo -> Ptr PgfPrintContext -> CInt -> Ptr PgfMarshaller -> IO (Ptr PgfText)
foreign import ccall "pgf_read_type"
pgf_read_type :: Ptr PgfText -> Ptr PgfUnmarshaller -> IO (StablePtr Type)