~jack/misc

a3ca0850d797fbe3f3f192e80f655f6d1638efc5 — Jack Kelly 6 months ago 9f433d7
lambda-c: make the pretty printer not suck
2 files changed, 26 insertions(+), 17 deletions(-)

M lambda-c/src/term.c
M lambda-c/src/term.h
M lambda-c/src/term.c => lambda-c/src/term.c +25 -16
@@ 82,25 82,34 @@ term_unref(Term *t) {
  g_free(t);
}

void
term_fput(Term *t, FILE *fp) {
  switch (t->tag) {
  case TERM_VAR:
static void
term_fput_var(const Term *t, FILE *fp) {
  if (t->tag == TERM_VAR) {
    fprintf(fp, "%s", t->var.v);
    break;
  case TERM_APP:
  } else {
    fprintf(fp, "(");
    term_fput(t->app.t1, fp);
    fprintf(fp, ")(");
    term_fput(t->app.t2, fp);
    term_fput(t, fp);
    fprintf(fp, ")");
    break;
  case TERM_LAM:
    fprintf(fp, "\\%s.", t->lam.v);
  }
}

static void
term_fput_app(const Term *t, FILE *fp) {
  if (t->tag == TERM_APP) {
    term_fput_app(t->app.t1, fp);
    fprintf(fp, " ");
    term_fput_var(t->app.t2, fp);
  } else {
    term_fput_var(t, fp);
  }
}

void
term_fput(const Term *t, FILE *fp) {
  if (t->tag == TERM_LAM) {
    fprintf(fp, "\\%s . ", t->lam.v);
    term_fput(t->lam.t, fp);
    break;
  default:
    fprintf(stderr, "term_fput: unexpected tag %d\n", t->tag);
    abort();
  } else {
    term_fput_app(t, fp);
  }
}

M lambda-c/src/term.h => lambda-c/src/term.h +1 -1
@@ 48,6 48,6 @@ void term_unref(Term *t);

G_DEFINE_AUTOPTR_CLEANUP_FUNC(Term, term_unref);

void term_fput(struct term *t, FILE *fp);
void term_fput(const Term *t, FILE *fp);

#endif