diff options
| author | krangelov <kr.angelov@gmail.com> | 2019-06-24 12:13:14 +0200 |
|---|---|---|
| committer | krangelov <kr.angelov@gmail.com> | 2019-06-24 12:13:14 +0200 |
| commit | 2d9478b97332db11980ed12b1ab0b892ab95cfff (patch) | |
| tree | bb6bd3979a5896f87487929382ed62cfaac71242 /src/runtime/c/pgf/printer.c | |
| parent | 8c18d7162fc197b45a0292e93f29116c918b9d2f (diff) | |
share some code for printing
Diffstat (limited to 'src/runtime/c/pgf/printer.c')
| -rw-r--r-- | src/runtime/c/pgf/printer.c | 111 |
1 files changed, 69 insertions, 42 deletions
diff --git a/src/runtime/c/pgf/printer.c b/src/runtime/c/pgf/printer.c index 153130b54..dd21072ce 100644 --- a/src/runtime/c/pgf/printer.c +++ b/src/runtime/c/pgf/printer.c @@ -94,6 +94,74 @@ pgf_print_fid(int fid, GuOut* out, GuExn* err) gu_printf(out, err, "C%d", fid); } +PGF_INTERNAL void +pgf_print_production_args(PgfPArgs* args, + GuOut* out, GuExn* err) +{ + size_t n_args = gu_seq_length(args); + for (size_t j = 0; j < n_args; j++) { + if (j > 0) + gu_putc(',',out,err); + + PgfPArg arg = gu_seq_get(args, PgfPArg, j); + + if (arg.hypos != NULL && + gu_seq_length(arg.hypos) > 0) { + size_t n_hypos = gu_seq_length(arg.hypos); + for (size_t k = 0; k < n_hypos; k++) { + PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k); + pgf_print_fid(hypo->fid, out, err); + gu_putc(' ',out,err); + } + gu_puts("-> ",out,err); + } + + pgf_print_fid(arg.ccat->fid, out, err); + } +} + +PGF_INTERNAL void +pgf_print_production(int fid, PgfProduction prod, + GuOut *out, GuExn* err) +{ + pgf_print_fid(fid, out, err); + gu_puts(" -> ", out, err); + + GuVariantInfo i = gu_variant_open(prod); + switch (i.tag) { + case PGF_PRODUCTION_APPLY: { + PgfProductionApply* papp = i.data; + gu_printf(out,err,"F%d(",papp->fun->funid); + if (papp->fun->ep != NULL) { + pgf_print_expr(papp->fun->ep->expr, NULL, 0, out, err); + } else { + PgfPArg* parg = gu_seq_index(papp->args, PgfPArg, 0); + gu_printf(out,err,"linref %s", parg->ccat->cnccat->abscat->name); + } + gu_printf(out,err,")["); + pgf_print_production_args(papp->args,out,err); + gu_printf(out,err,"]\n"); + break; + } + case PGF_PRODUCTION_COERCE: { + PgfProductionCoerce* pcoerce = i.data; + gu_puts("_[",out,err); + pgf_print_fid(pcoerce->coerce->fid, out, err); + gu_puts("]\n",out,err); + break; + } + case PGF_PRODUCTION_EXTERN: { + PgfProductionExtern* pext = i.data; + gu_printf(out,err,"<extern>("); + pgf_print_expr(pext->ep->expr, NULL, 0, out, err); + gu_printf(out,err,")[]\n"); + break; + } + default: + gu_impossible(); + } +} + static void pgf_print_productions(GuMapItor* fn, const void* key, void* value, GuExn* err) @@ -107,48 +175,7 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value, size_t n_prods = gu_seq_length(ccat->prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i); - - gu_puts(" ", out, err); - pgf_print_fid(fid, out, err); - gu_puts(" -> ", out, err); - - GuVariantInfo i = gu_variant_open(prod); - switch (i.tag) { - case PGF_PRODUCTION_APPLY: { - PgfProductionApply* papp = i.data; - gu_printf(out,err,"F%d[",papp->fun->funid); - size_t n_args = gu_seq_length(papp->args); - for (size_t j = 0; j < n_args; j++) { - if (j > 0) - gu_putc(',',out,err); - - PgfPArg arg = gu_seq_get(papp->args, PgfPArg, j); - - if (arg.hypos != NULL) { - size_t n_hypos = gu_seq_length(arg.hypos); - for (size_t k = 0; k < n_hypos; k++) { - if (k > 0) - gu_putc(' ',out,err); - PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k); - pgf_print_fid(hypo->fid, out, err); - } - } - - pgf_print_fid(arg.ccat->fid, out, err); - } - gu_printf(out,err,"]\n"); - break; - } - case PGF_PRODUCTION_COERCE: { - PgfProductionCoerce* pcoerce = i.data; - gu_puts("_[", out, err); - pgf_print_fid(pcoerce->coerce->fid, out, err); - gu_puts("]\n", out, err); - break; - } - default: - gu_impossible(); - } + pgf_print_production(fid, prod, out, err); } } } |
