-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
110 lines (105 loc) · 2.38 KB
/
display.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "lilscheme.h"
void DisplayCons(Handle, FILE*);
void DisplayVector(Handle, FILE*);
void DisplayObject(Handle hnd, FILE *output) {
switch(TYPEOF(hnd)) {
case TYPE_NIL:
fputs("nil", output);
break;
case TYPE_INT:
fprintf(output, "%d", UnboxInteger(hnd));
break;
case TYPE_FLOAT:
fprintf(output, "%f", UnboxFloat(hnd));
break;
case TYPE_SYMBOL:
fprintf(output, "%s", NameOfSymbol(hnd));
break;
case TYPE_CONS:
DisplayCons(hnd, output);
break;
case TYPE_VECTOR:
DisplayVector(hnd, output);
break;
case TYPE_BYTEVECTOR:
fprintf(output, "<bytevector #%hd>", hnd);
break;
case TYPE_FUNCTION:
fprintf(output, "<function #%hd>", hnd);
break;
case TYPE_CONTEXT:
fprintf(output, "<continuation #%hd>", hnd);
break;
case TYPE_PRIMITIVE:
fprintf(output, "<primitive #%hd>", hnd);
break;
default:
panic("can't DisplayObject that type");
}
}
void DisplayCons(Handle hnd, FILE *output) {
Handle car, cdr;
car = Car(hnd);
cdr = Cdr(hnd);
fputc('(', output);
DisplayObject(car, output);
while (DEREF(cdr)->type == TYPE_CONS) {
car = Car(cdr);
cdr = Cdr(cdr);
fputc(' ', output);
DisplayObject(car, output);
}
if (cdr != nil) {
fputs(" . ", output);
DisplayObject(cdr, output);
}
fputc(')', output);
}
void DisplayVector(Handle hnd, FILE *output) {
fputs("#(", output);
for (int i = 0; i < VectorLength(hnd); i++) {
if (i > 0) fputc(' ', output);
Handle elt = VectorRef(hnd, i);
DisplayObject(elt, output);
}
fputc(')', output);
}
void DumpObject(Handle hnd, FILE *output) {
switch(TYPEOF(hnd)) {
case TYPE_NIL:
fputs("nil", output);
break;
case TYPE_INT:
fprintf(output, "%d", UnboxInteger(hnd));
break;
case TYPE_FLOAT:
fprintf(output, "%f", UnboxFloat(hnd));
break;
case TYPE_SYMBOL:
fprintf(output, "'%s", NameOfSymbol(hnd));
break;
case TYPE_CONS:
fprintf(output, "(#%hd . #%hd)", Car(hnd), Cdr(hnd));
break;
case TYPE_VECTOR: {
fputs("#(", output);
FOR_IN_VECTOR(i, hnd) {
if (i > 0) fputc(' ', output);
fprintf(output, "#%hd", VectorRef(hnd, i));
}
fputc(')', output);
break;
}
case TYPE_BYTEVECTOR:
fprintf(output, "{BVEC#%hd}", hnd);
break;
case TYPE_FUNCTION:
fprintf(output, "{FUNC#%hd}", hnd);
break;
default:
panic("can't DumpObject that type");
}
}