From c8b6f12851802175f13d62abcbb2a95de756cf00 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Sun, 23 Jun 2024 01:06:47 -0400 Subject: [PATCH] indentation for pretty printer --- main.c | 20 ++++++++++++++------ prelude.scm | 15 ++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 4d1f24a..cdd3b2a 100644 --- a/main.c +++ b/main.c @@ -745,7 +745,7 @@ static void init_gc(void) alloc_of_type(&empty_list, EMPTY_LIST); } -static void display(struct uns_ctr *ctr) +static void display(struct uns_ctr *ctr, long indent) { struct uns_ctr tmp = {0}; long l; @@ -762,13 +762,21 @@ static void display(struct uns_ctr *ctr) printf("("); tmp.p = gc.record_get_ptr(ctr->p, 1); - display(&tmp); + display(&tmp, indent); ctr->p = gc.record_get_ptr(ctr->p, 2); while (get_type(ctr->p) == CELL) { - printf(" "); tmp.p = gc.record_get_ptr(ctr->p, 1); - display(&tmp); + if (get_type(tmp.p) == CELL) { + printf("\n"); + for (l = 0; l < indent; l++) + printf(" "); + display(&tmp, indent + 1); + } else { + printf(" "); + display(&tmp, indent); + } + ctr->p = gc.record_get_ptr(ctr->p, 2); } @@ -778,7 +786,7 @@ static void display(struct uns_ctr *ctr) break; default: printf(" . "); - display(ctr); + display(ctr, indent); printf(")"); break; } @@ -861,7 +869,7 @@ int main(void) switch (expr_parse(stdin, &expr)) { case EXPR_PARSE_OK: expr.p = gc.record_get_ptr(expr.p, 0); - display(&expr); + display(&expr, 1); printf("\n"); break; case EXPR_PARSE_INCOMPLETE: diff --git a/prelude.scm b/prelude.scm index 61aefce..2b413a0 100644 --- a/prelude.scm +++ b/prelude.scm @@ -31,7 +31,6 @@ ; __define-macro: Like __define but for macros ; set! ; __exec: apply an argument to a lambda and return it to the continuation. -; __continue-if: continuation version of if. ; ; Others: ; : A value that can only be passed to __unilambda. It cannot @@ -41,12 +40,18 @@ ; ; Macros are functions that return an sexpr. +; (define (__exec function argument continuation) +; (continuation (function argument)) +; ) + +; __continuation-if runs the first continuation if the argument is true, +; and the second if the argument is false. (__define-macro if (__unilambda l - (__continue-if (car l) - (__unilambda (car (cdr l))) - (__unilambda (car (cdr (cdr l)))) - ) + `(__continue-if ,(car l) + (__unilambda ,(car (cdr l))) + (__unilambda ,(car (cdr (cdr l)))) + ) ) )