Debian Codegraph @

src/c/printer/write_ugly.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * write_ugly.d - ugly printer
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #ifndef _MSC_VER
19 # include <unistd.h>
20 #endif
21 #include <ecl/ecl.h>
22 #include <ecl/internal.h>
23
24 static void
25 write_readable_pathname(cl_object path, cl_object stream)
26 {
27 cl_object l =
28 cl_list(15, @'make-pathname',
29 @':host', path->pathname.host,
30 @':device', path->pathname.device,
31 @':directory',
32 _ecl_funcall2(@'ext::maybe-quote', path->pathname.directory),
33 @':name', path->pathname.name,
34 @':type', path->pathname.type,
35 @':version', path->pathname.version,
36 @':defaults', ECL_NIL);
37 writestr_stream("#.", stream);
38 si_write_object(l, stream);
39 }
40
41 static void
42 write_pathname(cl_object path, cl_object stream)
43 {
44 cl_object namestring = ecl_namestring(path, 0);
45 bool readably = ecl_print_readably();
46 if (namestring == ECL_NIL) {
47 if (readably) {
48 write_readable_pathname(path, stream);
49 return;
50 }
51 namestring = ecl_namestring(path, 1);
52 if (namestring == ECL_NIL) {
53 writestr_stream("#<Unprintable pathname>", stream);
54 return;
55 }
56 }
57 if (readably || ecl_print_escape())
58 writestr_stream("#P", stream);
59 si_write_ugly_object(namestring, stream);
60 }
61
62 static void
63 write_integer(cl_object number, cl_object stream)
64 {
65 cl_object s = si_get_buffer_string();
66 int print_base = ecl_print_base();
67 si_integer_to_string(s, number,
68 ecl_make_fixnum(print_base),
69 ecl_symbol_value(@'*print-radix*'),
70 ECL_T /* decimal syntax */);
71 si_do_write_sequence(s, stream, ecl_make_fixnum(0), ECL_NIL);
72 si_put_buffer_string(s);
73 }
74
75 void
76 _ecl_write_fixnum(cl_fixnum i, cl_object stream)
77 {
78 cl_object s = si_get_buffer_string();
79 si_integer_to_string(s, ecl_make_fixnum(i), ecl_make_fixnum(10), ECL_NIL, ECL_NIL);
80 si_do_write_sequence(s, stream, ecl_make_fixnum(0), ECL_NIL);
81 si_put_buffer_string(s);
82 }
83
84 static void
85 write_ratio(cl_object r, cl_object stream)
86 {
87 cl_object s = si_get_buffer_string();
88 int print_base = ecl_print_base();
89 si_integer_to_string(s, r->ratio.num, ecl_make_fixnum(print_base),
90 ecl_symbol_value(@'*print-radix*'),
91 ECL_NIL /* decimal syntax */);
92 ecl_string_push_extend(s, '/');
93 si_integer_to_string(s, r->ratio.den,
94 ecl_make_fixnum(print_base),
95 ECL_NIL, ECL_NIL);
96 si_do_write_sequence(s, stream, ecl_make_fixnum(0), ECL_NIL);
97 si_put_buffer_string(s);
98 }
99
100 static void
101 write_complex(cl_object x, cl_object stream)
102 {
103 writestr_stream("#C(", stream);
104 si_write_ugly_object(x->gencomplex.real, stream);
105 ecl_write_char(' ', stream);
106 si_write_ugly_object(x->gencomplex.imag, stream);
107 ecl_write_char(')', stream);
108 }
109
110 static void
111 write_float(cl_object f, cl_object stream)
112 {
113 cl_object s = si_get_buffer_string();
114 s = si_float_to_string_free(s, f, ecl_make_fixnum(-3), ecl_make_fixnum(8));
115 si_do_write_sequence(s, stream, ecl_make_fixnum(0), ECL_NIL);
116 si_put_buffer_string(s);
117 }
118
119 #ifdef ECL_COMPLEX_FLOAT
120 static void /* XXX: do not cons new floats here! */
121 write_complex_float(cl_object f, cl_object stream)
122 {
123 cl_object real = OBJNULL, imag = OBJNULL;
124 switch (ecl_t_of(f)) {
125 case t_csfloat:
126 real = ecl_make_single_float(crealf(ecl_csfloat(f)));
127 imag = ecl_make_single_float(cimagf(ecl_csfloat(f)));
128 break;
129 case t_cdfloat:
130 real = ecl_make_double_float(creal(ecl_cdfloat(f)));
131 imag = ecl_make_double_float(cimag(ecl_cdfloat(f)));
132 break;
133 case t_clfloat:
134 real = ecl_make_long_float(creall(ecl_clfloat(f)));
135 imag = ecl_make_long_float(cimagl(ecl_clfloat(f)));
136 break;
137 default:
138 break;
139 }
140 writestr_stream("#C(", stream);
141 si_write_ugly_object(real, stream);
142 ecl_write_char(' ', stream);
143 si_write_ugly_object(imag, stream);
144 writestr_stream(")", stream);
145 }
146 #endif
147
148 static void
149 write_character(cl_object x, cl_object stream)
150 {
151 int i = ECL_CHAR_CODE(x);
152 if (!ecl_print_escape() && !ecl_print_readably()) {
153 ecl_write_char(i, stream);
154 } else {
155 writestr_stream("#\\", stream);
156 if (i < 32 || i >= 127) {
157 cl_object name = cl_char_name(ECL_CODE_CHAR(i));
158 writestr_stream((char*)name->base_string.self, stream);
159 } else {
160 ecl_write_char(i, stream);
161 }
162 }
163 }
164
165 static void
166 write_package(cl_object x, cl_object stream)
167 {
168 if (ecl_print_readably()) FEprint_not_readable(x);
169 writestr_stream("#<", stream);
170 si_write_ugly_object(x->pack.name, stream);
171 writestr_stream(" package>", stream);
172 }
173
174 static void
175 write_hashtable(cl_object x, cl_object stream)
176 {
177 if (ecl_print_readably() && !Null(ecl_symbol_value(@'*read-eval*'))) {
178 cl_object make =
179 cl_list(15, @'make-hash-table',
180 @':size', cl_hash_table_size(x),
181 @':synchronized', si_hash_table_synchronized_p(x),
182 @':weakness', si_hash_table_weakness(x),
183 @':hash-function', x->hash.generic_hash,
184 @':rehash-size', cl_hash_table_rehash_size(x),
185 @':rehash-threshold', cl_hash_table_rehash_threshold(x),
186 @':test', cl_list(2, @'quote', cl_hash_table_test(x)));
187 cl_object init =
188 cl_list(3, @'ext::hash-table-fill', make,
189 cl_list(2, @'quote', si_hash_table_content(x)));
190 writestr_stream("#.", stream);
191 si_write_ugly_object(init, stream);
192 } else {
193 _ecl_write_unreadable(x, "hash-table", ECL_NIL, stream);
194 }
195 }
196
197 static void
198 write_random(cl_object x, cl_object stream)
199 {
200 if (ecl_print_readably()) {
201 writestr_stream("#$", stream);
202 _ecl_write_vector(x->random.value, stream);
203 } else {
204 _ecl_write_unreadable(x->random.value, "random-state", ECL_NIL, stream);
205 }
206 }
207
208 static void
209 write_stream(cl_object x, cl_object stream)
210 {
211 const char *prefix;
212 cl_object tag;
213 cl_object buffer = OBJNULL;
214 switch ((enum ecl_smmode)x->stream.mode) {
215 case ecl_smm_input_file:
216 prefix = "closed input file";
217 tag = IO_STREAM_FILENAME(x);
218 break;
219 case ecl_smm_input:
220 prefix = "closed input stream";
221 tag = IO_STREAM_FILENAME(x);
222 break;
223 case ecl_smm_output_file:
224 prefix = "closed output file";
225 tag = IO_STREAM_FILENAME(x);
226 break;
227 case ecl_smm_output:
228 prefix = "closed output stream";
229 tag = IO_STREAM_FILENAME(x);
230 break;
231 #ifdef ECL_MS_WINDOWS_HOST
232 case ecl_smm_input_wsock:
233 prefix = "closed input win32 socket stream";
234 tag = IO_STREAM_FILENAME(x);
235 break;
236 case ecl_smm_output_wsock:
237 prefix = "closed output win32 socket stream";
238 tag = IO_STREAM_FILENAME(x);
239 break;
240 case ecl_smm_io_wsock:
241 prefix = "closed i/o win32 socket stream";
242 tag = IO_STREAM_FILENAME(x);
243 break;
244 case ecl_smm_io_wcon:
245 prefix = "closed i/o win32 console stream";
246 tag = IO_STREAM_FILENAME(x);
247 break;
248 #endif
249 case ecl_smm_io_file:
250 prefix = "closed io file";
251 tag = IO_STREAM_FILENAME(x);
252 break;
253 case ecl_smm_io:
254 prefix = "closed io stream";
255 tag = IO_STREAM_FILENAME(x);
256 break;
257 case ecl_smm_probe:
258 prefix = "closed probe stream";
259 tag = IO_STREAM_FILENAME(x);
260 break;
261 case ecl_smm_synonym:
262 prefix = "closed synonym stream to";
263 tag = SYNONYM_STREAM_SYMBOL(x);
264 break;
265 case ecl_smm_broadcast:
266 prefix = "closed broadcast stream";
267 tag = ECL_NIL;
268 break;
269 case ecl_smm_concatenated:
270 prefix = "closed concatenated stream";
271 tag = ECL_NIL;
272 break;
273 case ecl_smm_two_way:
274 prefix = "closed two-way stream";
275 tag = ECL_NIL;
276 break;
277 case ecl_smm_echo:
278 prefix = "closed echo stream";
279 tag = ECL_NIL;
280 break;
281 case ecl_smm_string_input: {
282 buffer = si_get_buffer_string();
283 cl_object text = x->stream.object0;
284 cl_index ndx, l = ecl_length(text);
285 for (ndx = 0; (ndx < 8) && (ndx < l); ndx++) {
286 ecl_char_set(buffer, ndx, ecl_char(text, ndx));
287 }
288 if (l > ndx) {
289 ecl_char_set(buffer, ndx-1, '.');
290 ecl_char_set(buffer, ndx-2, '.');
291 ecl_char_set(buffer, ndx-3, '.');
292 }
293 si_fill_pointer_set(buffer, ecl_make_fixnum(ndx));
294 prefix = "closed string-input stream from";
295 tag = buffer;
296 break;
297 }
298 case ecl_smm_string_output:
299 prefix = "closed string-output stream";
300 tag = ECL_NIL;
301 break;
302 case ecl_smm_sequence_input:
303 prefix = "closed sequence-input stream";
304 tag = ECL_NIL;
305 break;
306 case ecl_smm_sequence_output:
307 prefix = "closed sequence-output stream";
308 tag = ECL_NIL;
309 break;
310 default:
311 ecl_internal_error("illegal stream mode");
312 }
313 if (!x->stream.closed)
314 prefix = prefix + 7;
315 _ecl_write_unreadable(x, prefix, tag, stream);
316 if (buffer != OBJNULL)
317 si_put_buffer_string(buffer);
318 }
319
320 static void
321 write_instance(cl_object x, cl_object stream)
322 {
323 _ecl_funcall3(@'print-object', x, stream);
324 }
325
326 static void
327 write_readtable(cl_object x, cl_object stream)
328 {
329 _ecl_write_unreadable(x, "readtable", ECL_NIL, stream);
330 }
331
332 static void
333 write_cfun(cl_object x, cl_object stream)
334 {
335 _ecl_write_unreadable(x, "compiled-function", x->cfun.name, stream);
336 }
337
338 static void
339 write_codeblock(cl_object x, cl_object stream)
340 {
341 _ecl_write_unreadable(x, "codeblock", x->cblock.name, stream);
342 }
343
344 static void
345 write_cclosure(cl_object x, cl_object stream)
346 {
347 _ecl_write_unreadable(x, "compiled-closure", ECL_NIL, stream);
348 }
349
350 static void
351 write_foreign(cl_object x, cl_object stream)
352 {
353 if (ecl_print_readably()) {
354 FEprint_not_readable(x);
355 }
356 writestr_stream("#<foreign ", stream);
357 si_write_ugly_object(x->foreign.tag, stream);
358 ecl_write_char(' ', stream);
359 if (x->foreign.data == NULL) {
360 writestr_stream("NULL", stream);
361 } else {
362 _ecl_write_addr((void *)x->foreign.data, stream);
363 }
364 ecl_write_char('>', stream);
365 }
366
367 static void
368 write_frame(cl_object x, cl_object stream)
369 {
370 _ecl_write_unreadable(x, "frame", ecl_make_fixnum(x->frame.size), stream);
371 }
372
373 static void
374 write_weak_pointer(cl_object x, cl_object stream)
375 {
376 _ecl_write_unreadable(x, "weak-pointer", ECL_NIL, stream);
377 }
378
379 #ifdef ECL_THREADS
380 static void
381 write_process(cl_object x, cl_object stream)
382 {
383 _ecl_write_unreadable(x, "process", x->process.name, stream);
384 }
385
386 static void
387 write_lock(cl_object x, cl_object stream)
388 {
389 const char *prefix = x->lock.recursive?
390 "lock" : "lock (nonrecursive)";
391 _ecl_write_unreadable(x, prefix, x->lock.name, stream);
392 }
393
394 static void
395 write_rwlock(cl_object x, cl_object stream)
396 {
397 _ecl_write_unreadable(x, "rwlock", x->rwlock.name, stream);
398 }
399
400 static void
401 write_condition_variable(cl_object x, cl_object stream)
402 {
403 _ecl_write_unreadable(x, "condition-variable", ECL_NIL, stream);
404 }
405
406 static void
407 write_semaphore(cl_object x, cl_object stream)
408 {
409 _ecl_write_unreadable(x, "semaphore", ECL_NIL, stream);
410 }
411
412 static void
413 write_barrier(cl_object x, cl_object stream)
414 {
415 _ecl_write_unreadable(x, "barrier", ECL_NIL, stream);
416 }
417
418 static void
419 write_mailbox(cl_object x, cl_object stream)
420 {
421 _ecl_write_unreadable(x, "mailbox", ECL_NIL, stream);
422 }
423
424 #endif /* ECL_THREADS */
425
426 static void
427 write_illegal(cl_object x, cl_object stream)
428 {
429 _ecl_write_unreadable(x, "illegal pointer", ECL_NIL, stream);
430 }
431
432 typedef void (*printer)(cl_object x, cl_object stream);
433
434 static printer dispatch[FREE+1] = {
435 0 /* t_start = 0 */,
436 _ecl_write_list, /* t_list = 1 */
437 write_character, /* t_character = 2 */
438 write_integer, /* t_fixnum = 3 */
439 write_integer, /* t_bignum = 4 */
440 write_ratio, /* t_ratio */
441 /* write_float, */ /* t_shortfloat */
442 write_float, /* t_singlefloat */
443 write_float, /* t_doublefloat */
444 write_float, /* t_longfloat */
445 write_complex, /* t_complex */
446 #ifdef ECL_COMPLEX_FLOAT
447 write_complex_float, /* t_csfloat */
448 write_complex_float, /* t_cdfloat */
449 write_complex_float, /* t_clfloat */
450 #endif
451 _ecl_write_symbol, /* t_symbol */
452 write_package, /* t_package */
453 write_hashtable, /* t_hashtable */
454 _ecl_write_array, /* t_array */
455 _ecl_write_vector, /* t_vector */
456 #ifdef ECL_UNICODE
457 _ecl_write_string, /* t_string */
458 #endif
459 _ecl_write_base_string, /* t_base_string */
460 _ecl_write_bitvector, /* t_bitvector */
461 write_stream, /* t_stream */
462 write_random, /* t_random */
463 write_readtable, /* t_readtable */
464 write_pathname, /* t_pathname */
465 _ecl_write_bytecodes, /* t_bytecodes */
466 _ecl_write_bclosure, /* t_bclosure */
467 write_cfun, /* t_cfun */
468 write_cfun, /* t_cfunfixed */
469 write_cclosure, /* t_cclosure */
470 write_instance, /* t_instance */
471 #ifdef ECL_THREADS
472 write_process, /* t_process */
473 write_lock, /* t_lock */
474 write_rwlock, /* t_rwlock */
475 write_condition_variable, /* t_condition_variable */
476 write_semaphore, /* t_semaphore */
477 write_barrier, /* t_barrier */
478 write_mailbox, /* t_mailbox */
479 #endif
480 write_codeblock, /* t_codeblock */
481 write_foreign, /* t_foreign */
482 write_frame, /* t_frame */
483 write_weak_pointer, /* t_weak_pointer */
484 #ifdef ECL_SSE2
485 _ecl_write_sse, /* t_sse_pack */
486 #endif
487 /* t_end */
488 };
489
490 cl_object
491 si_write_ugly_object(cl_object x, cl_object stream)
492 {
493 if (x == OBJNULL) {
494 if (ecl_print_readably())
495 FEprint_not_readable(x);
496 writestr_stream("#<OBJNULL>", stream);
497 } else {
498 int t = ecl_t_of(x);
499 printer f = (t >= t_end)? write_illegal : dispatch[t];
500 f(x, stream);
501 }
502 @(return x);
503 }