Skip to content

Commit

Permalink
pretty printing output
Browse files Browse the repository at this point in the history
  • Loading branch information
billhails committed Nov 28, 2024
1 parent 165fd0c commit 2d8d458
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions fn/pettersson92.fn
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,67 @@ let
apply(code, list(code))
}

print code(c) {
let
fn pargs {
([]) { "" }
([a]) { h(0, false, a) }
(a @ b) { h(0, false, a); puts(", "); pargs(b) }
}
fn h {
(n, dp, symbol(name)) { pad(n, dp); puts(name) }
(n, dp, case(c, cases)) {
pad(n, dp);
puts("consider(");
h(n, false, c);
puts(") {\n");
cases |> fn (c) { h(n + 1, true, c); puts("\n"); };
pad(n, true); puts("}");
}
(n, dp, when(p, c)) {
pad(n, dp);
puts("when ");
print(p);
h(n + 1, true, c);
}
(n, dp, letrec(defs, body)) {
pad(n, dp);
puts("let\n");
defs |> fn (#(sym, val)) {
h(n + 1, true, sym);
puts(" = ");
h(n + 1, false, val);
puts("\n");
};
pad(n, true);
puts("in\n");
h(n + 1, true, body);
}
(n, dp, lambda(args, body)) {
pad(n, dp);
puts("fn (");
pargs(args);
puts(") {\n");
h(n + 1, true, body);
puts("\n");
pad(n, true);
puts("}");
}
(n, dp, apply(exp, args)) {
h(n, dp, exp);
puts("(");
pargs(args);
puts(")");
}
}
fn pad(n, dp) {
puts(lst.repeat(if(dp){n * 2} else {0}, ' '))
}
in
h(0, true, c);
c;
}

// makeTag: string -> number -> string
fn makeTag (base, n) { base @@ "$" @@ $n }

Expand Down

0 comments on commit 2d8d458

Please sign in to comment.