forked from lwhjp/building-lisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
44 lines (42 loc) · 816 Bytes
/
print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "lisp.h"
#include <stdio.h>
void print_expr(Atom atom)
{
switch (atom.type) {
case AtomType_Nil:
printf("NIL");
break;
case AtomType_Pair:
putchar('(');
print_expr(car(atom));
atom = cdr(atom);
while (!nilp(atom)) {
if (atom.type == AtomType_Pair) {
putchar(' ');
print_expr(car(atom));
atom = cdr(atom);
} else {
printf(" . ");
print_expr(atom);
break;
}
}
putchar(')');
break;
case AtomType_Symbol:
printf("%s", atom.value.symbol);
break;
case AtomType_Integer:
printf("%ld", atom.value.integer);
break;
case AtomType_Builtin:
printf("#<BUILTIN:%p>", atom.value.builtin);
break;
case AtomType_Closure:
printf("#<CLOSURE:%p>", atom.value.pair);
break;
case AtomType_Macro:
printf("#<MACRO:%p>", atom.value.pair);
break;
}
}