indentation for pretty printer

This commit is contained in:
Peter McGoron 2024-06-23 01:06:47 -04:00
parent 38b044921f
commit c8b6f12851
2 changed files with 24 additions and 11 deletions

20
main.c
View File

@ -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:

View File

@ -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:
; <undefined>: 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 <undefined> (car (cdr l)))
(__unilambda <undefined> (car (cdr (cdr l))))
)
`(__continue-if ,(car l)
(__unilambda <undefined> ,(car (cdr l)))
(__unilambda <undefined> ,(car (cdr (cdr l))))
)
)
)