-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
233 lines (212 loc) · 5.75 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "print.h"
#include <wctype.h>
#include "obj_binding.h"
#include "types.h"
#include "unicode.h"
/* XXX Reimplement this in Scheme. */
static inline bool is_ident_initial(wchar_t wc)
{
/* Ruthless elision of braces is fun! */
if (wc < 128)
return iswalpha(wc) || wcschr(L"!$%&*/:<=>?^_~.+-", wc);
else
switch ((int)unicode_general_category(wc))
case UGC_OTHER_PRIVATE_USE:
case UGC_LETTER_LOWERCASE:
case UGC_LETTER_MODIFIER:
case UGC_LETTER_OTHER:
case UGC_LETTER_TITLECASE:
case UGC_LETTER_UPPERCASE:
case UGC_MARK_NONSPACING:
case UGC_NUMBER_LETTER:
case UGC_NUMBER_OTHER:
case UGC_PUNCTUATION_CONNECTOR:
case UGC_PUNCTUATION_DASH:
case UGC_PUNCTUATION_OTHER:
case UGC_SYMBOL_CURRENCY:
case UGC_SYMBOL_MODIFIER:
case UGC_SYMBOL_MATH:
case UGC_SYMBOL_OTHER:
return true;
return false;
}
/* XXX this function duplicates one in scan.c. */
static inline bool is_ident_subsequent(wchar_t wc)
{
if (is_ident_initial(wc) || wcschr(L"+-.@", wc))
return true;
switch ((int)unicode_general_category(wc)) {
case UGC_NUMBER_DECIMAL_DIGIT:
case UGC_MARK_SPACING_COMBINING:
case UGC_MARK_ENCLOSING:
return true;
}
return false;
}
static void print_form(obj_t *, outstream_t *);
static void print_list_interior(obj_t *obj, wchar_t *sep, outstream_t *out)
{
while (!is_null(obj)) {
outstream_printf(out, sep);
print_form(pair_car(obj), out);
obj = pair_cdr(obj);
if (!is_null(obj) && !is_pair(obj)) {
outstream_printf(out, L" . ");
print_form(obj, out);
break;
}
sep = L" ";
}
}
static void print_pair(obj_t *obj, outstream_t *out)
{
outstream_putwc(L'(', out);
print_list_interior(obj, L"", out);
outstream_putwc(L')', out);
}
static inline bool is_printing(wchar_t wc)
{
if (wc < 128)
return wc > L' ' && wc < L'\177';
else
switch ((int)unicode_general_category(wc))
case UGC_OTHER_PRIVATE_USE:
case UGC_LETTER_LOWERCASE:
case UGC_LETTER_MODIFIER:
case UGC_LETTER_OTHER:
case UGC_LETTER_TITLECASE:
case UGC_LETTER_UPPERCASE:
case UGC_MARK_SPACING_COMBINING:
case UGC_MARK_ENCLOSING:
case UGC_MARK_NONSPACING:
case UGC_NUMBER_DECIMAL_DIGIT:
case UGC_NUMBER_LETTER:
case UGC_NUMBER_OTHER:
case UGC_PUNCTUATION_CONNECTOR:
case UGC_PUNCTUATION_DASH:
case UGC_PUNCTUATION_OTHER:
case UGC_SYMBOL_CURRENCY:
case UGC_SYMBOL_MODIFIER:
case UGC_SYMBOL_MATH:
case UGC_SYMBOL_OTHER:
return true;
return false;
}
static void print_char(obj_t *chobj, outstream_t *out)
{
wchar_t wc = character_value(chobj);
if (wc == L' ')
outstream_printf(out, L"#\\space");
else if (is_printing(wc))
outstream_printf(out, L"#\\%lc", wc);
else
outstream_printf(out, L"#\\x%04x", wc);
}
static void print_string(obj_t *string, outstream_t *out)
{
size_t i, len = string_len(string);
const wchar_t *value = string_value(string);
outstream_putwc(L'"', out);
for (i = 0; i < len; i++)
outstream_putwc(value[i], out);
outstream_putwc(L'"', out);
}
static void print_symbol(obj_t *obj, outstream_t *out)
{
obj_t *string = symbol_name(obj);
const wchar_t *value = string_value(string);
size_t i, len = string_len(string);
for (i = 0; i < len; i++) {
wchar_t wc = value[i];
if ((i ? is_ident_subsequent : is_ident_initial)(wc))
outstream_putwc(value[i], out);
else
outstream_printf(out, L"\\x%x;", wc);
}
}
static void print_procedure(obj_t *obj, outstream_t *out)
{
// if (procedure_is_special_form(obj) || procedure_is_C(obj)) {
if (procedure_is_C(obj)) {
outstream_printf(out, L"#<proc-%s%s>",
procedure_is_special_form(obj) ? "S" : "",
procedure_is_C(obj) ? "C" : "");
} else {
outstream_printf(out, L"(lambda ");
print_form(procedure_args(obj), out);
print_list_interior(procedure_body(obj), L" ", out);
outstream_printf(out, L")");
}
}
static void print_binding(obj_t *obj, outstream_t *out)
{
print_form(binding_name(obj), out);
outstream_putwc(binding_is_mutable(obj) ? L':' : L'!', out);
print_form(binding_value(obj), out);
}
static void print_vector(obj_t *obj, outstream_t *out)
{
size_t i, size = vector_len(obj);
const wchar_t *sep = L"";
outstream_printf(out, L"#(");
for (i = 0; i < size; i++) {
outstream_printf(out, sep);
print_form(vector_ref(obj, i), out);
sep = L" ";
}
outstream_printf(out, L")");
}
static void print_bytevector(obj_t *obj, outstream_t *out)
{
size_t i, size = bytevector_len(obj);
const wchar_t *sep = L"";
outstream_printf(out, L"#vu8(");
for (i = 0; i < size; i++) {
outstream_printf(out, sep);
outstream_printf(out, L"%d", bytevector_get(obj, i));
sep = L" ";
}
outstream_printf(out, L")");
}
static void print_form(obj_t *obj, outstream_t *out)
{
if (is_null(obj) || is_pair(obj)) {
print_pair(obj, out);
} else if (is_boolean(obj)) {
outstream_printf(out, boolean_value(obj) ? L"#t" : L"#f");
} else if (is_fixnum(obj)) {
outstream_printf(out, L"%d", fixnum_value(obj));
} else if (is_character(obj)) {
print_char(obj, out);
} else if (is_string(obj)) {
print_string(obj, out);
} else if (is_symbol(obj)) {
print_symbol(obj, out);
} else if (is_procedure(obj)) {
print_procedure(obj, out);
} else if (is_binding(obj)) {
print_binding(obj, out);
} else if (is_vector(obj)) {
print_vector(obj, out);
} else if (is_bytevector(obj)) {
print_bytevector(obj, out);
} else {
outstream_printf(out, L"#<%ls-%p>", object_type_name(obj), obj);
}
}
void princ(obj_t *obj, outstream_t *out)
{
print_form(obj, out);
}
static bool is_unspecified(obj_t *obj)
{
return is_symbol(obj) &&
!wcscmp(string_value(symbol_name(obj)), UNSPECIFIED_NAME);
}
void print(obj_t *obj, outstream_t *out)
{
if (!is_unspecified(obj)) {
print_form(obj, out);
outstream_putwc(L'\n', out);
}
}