1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
47 #include "mbstrings.h"
60 /* {Names of immediate symbols}
62 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
65 char *scm_isymnames
[] =
67 /* This table must agree with the declarations */
83 "#@literal-variable-ref",
84 "#@literal-variable-set!",
87 "#@call-with-current-continuation",
89 /* user visible ISYMS */
101 scm_option scm_print_opts
[] = {
102 { SCM_OPTION_SCM
, "closure-hook", SCM_BOOL_F
,
103 "Hook for printing closures." },
104 { SCM_OPTION_BOOLEAN
, "source", 0,
105 "Print closures with source." }
108 SCM_PROC (s_print_options
, "print-options-interface", 0, 1, 0, scm_print_options
);
111 scm_print_options (setting
)
114 SCM ans
= scm_options (setting
,
122 /* {Printing of Scheme Objects}
125 /* Detection of circular references.
127 * Due to other constraints in the implementation, this code has bad
128 * time complexity (O (depth * N)), The printer code will be
129 * completely rewritten before next release of Guile. The new code
132 #define PUSH_REF(pstate, obj) \
134 pstate->ref_stack[pstate->top++] = (obj); \
135 if (pstate->top == pstate->ceiling) \
136 grow_ref_stack (pstate); \
139 #define ENTER_NESTED_DATA(pstate, obj, label) \
142 for (i = 0; i < pstate->top; ++i) \
143 if (pstate->ref_stack[i] == (obj)) \
145 if (pstate->fancyp) \
147 if (pstate->top - pstate->list_offset >= pstate->level) \
149 scm_gen_putc ('#', port); \
153 PUSH_REF(pstate, obj); \
156 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
158 static SCM print_state_pool
;
160 #if 1 /* Used for debugging purposes */
161 SCM_PROC(s_current_pstate
, "current-pstate", 0, 0, 0, scm_current_pstate
);
164 scm_current_pstate ()
166 return SCM_CADR (print_state_pool
);
170 #define PSTATE_SIZE 50L
172 static SCM make_print_state
SCM_P ((void));
177 SCM print_state
= scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
180 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
181 pstate
->ref_vect
= scm_make_vector (SCM_MAKINUM (PSTATE_SIZE
),
184 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
185 pstate
->ceiling
= SCM_LENGTH (pstate
->ref_vect
);
190 scm_make_print_state ()
194 /* First try to allocate a print state from the pool */
196 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
198 answer
= SCM_CADR (print_state_pool
);
199 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
203 return answer
? answer
: make_print_state ();
207 scm_free_print_state (print_state
)
211 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
212 /* Cleanup before returning print state to pool.
213 * It is better to do it here. Doing it in scm_prin1
214 * would cost more since that function is called much more
218 SCM_NEWCELL (handle
);
220 SCM_SETCAR (handle
, print_state
);
221 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
222 SCM_SETCDR (print_state_pool
, handle
);
226 static void grow_ref_stack
SCM_P ((scm_print_state
*pstate
));
229 grow_ref_stack (pstate
)
230 scm_print_state
*pstate
;
232 int new_size
= 2 * pstate
->ceiling
;
233 scm_vector_set_length_x (pstate
->ref_vect
, SCM_MAKINUM (new_size
));
234 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
235 pstate
->ceiling
= new_size
;
239 static void print_circref
SCM_P ((SCM port
, scm_print_state
*pstate
, SCM ref
));
242 print_circref (port
, pstate
, ref
)
244 scm_print_state
*pstate
;
248 int self
= pstate
->top
- 1;
250 if (SCM_CONSP (pstate
->ref_stack
[i
]))
254 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
255 || SCM_CDR (pstate
->ref_stack
[i
- 1]) != pstate
->ref_stack
[i
])
261 for (i
= pstate
->top
- 1; 1; --i
)
262 if (pstate
->ref_stack
[i
] == ref
)
264 scm_gen_putc ('#', port
);
265 scm_intprint (i
- self
, 10, port
);
266 scm_gen_putc ('#', port
);
269 /* Print generally. Handles both write and display according to PSTATE.
274 scm_iprin1 (exp
, port
, pstate
)
277 scm_print_state
*pstate
;
281 switch (7 & (int) exp
)
285 scm_intprint (SCM_INUM (exp
), 10, port
);
291 scm_put_wchar (i
, port
, SCM_WRITINGP (pstate
));
294 else if (SCM_IFLAGP (exp
)
295 && (SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
296 scm_gen_puts (scm_regular_string
, SCM_ISYMCHARS (exp
), port
);
297 else if (SCM_ILOCP (exp
))
299 scm_gen_puts (scm_regular_string
, "#@", port
);
300 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
301 scm_gen_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
302 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
309 scm_gen_puts (scm_regular_string
, "#@", port
);
310 exp
= SCM_CAR (exp
- 1);
314 scm_ipruk ("immediate", exp
, port
);
317 switch (SCM_TYP7 (exp
))
319 case scm_tcs_cons_gloc
:
321 if (SCM_CDR (SCM_CAR (exp
) - 1L) == 0)
323 scm_gen_write (scm_regular_string
, "#<struct ", sizeof ("#<struct ") - 1, port
);
324 scm_intprint(exp
, 16, port
);
325 scm_gen_putc ('>', port
);
329 case scm_tcs_cons_imcar
:
330 case scm_tcs_cons_nimcar
:
331 ENTER_NESTED_DATA (pstate
, exp
, circref
);
332 scm_iprlist ("(", exp
, ')', port
, pstate
);
333 EXIT_NESTED_DATA (pstate
);
336 print_circref (port
, pstate
, exp
);
338 case scm_tcs_closures
:
339 if (SCM_NFALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
)))
341 SCM ans
= scm_cons2 (exp
, port
,
342 scm_cons (SCM_WRITINGP (pstate
)
346 ans
= scm_apply (SCM_PRINT_CLOSURE
, ans
, SCM_EOL
);
351 name
= scm_procedure_property (exp
, scm_i_name
);
352 code
= SCM_CODE (exp
);
353 scm_gen_puts (scm_regular_string
, "#<procedure ", port
);
354 if (SCM_NIMP (name
) && SCM_ROSTRINGP (name
))
356 scm_gen_puts (scm_regular_string
, SCM_ROCHARS (name
), port
);
357 scm_gen_putc (' ', port
);
359 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
360 if (SCM_PRINT_SOURCE_P
)
362 code
= scm_unmemocopy (SCM_CDR (code
),
363 SCM_EXTEND_ENV (SCM_CAR (code
),
366 ENTER_NESTED_DATA (pstate
, exp
, circref
);
367 scm_iprlist (" ", code
, '>', port
, pstate
);
368 EXIT_NESTED_DATA (pstate
);
371 scm_gen_putc ('>', port
);
374 case scm_tc7_mb_string
:
375 case scm_tc7_mb_substring
:
376 scm_print_mb_string (exp
, port
, SCM_WRITINGP (pstate
));
378 case scm_tc7_substring
:
380 if (SCM_WRITINGP (pstate
))
382 scm_gen_putc ('"', port
);
383 for (i
= 0; i
< SCM_ROLENGTH (exp
); ++i
)
384 switch (SCM_ROCHARS (exp
)[i
])
388 scm_gen_putc ('\\', port
);
390 scm_gen_putc (SCM_ROCHARS (exp
)[i
], port
);
392 scm_gen_putc ('"', port
);
396 scm_gen_write (scm_regular_string
, SCM_ROCHARS (exp
),
397 (scm_sizet
) SCM_ROLENGTH (exp
),
400 case scm_tcs_symbols
:
401 if (SCM_MB_STRINGP (exp
))
403 scm_print_mb_symbol (exp
, port
);
416 len
= SCM_LENGTH (exp
);
417 str
= SCM_CHARS (exp
);
424 scm_gen_write (scm_regular_string
, "#{}#", 4, port
);
426 for (end
= pos
; end
< len
; ++end
)
429 #ifdef BRACKETS_AS_PARENS
437 case SCM_WHITE_SPACES
:
438 case SCM_LINE_INCREMENTORS
:
447 scm_gen_write (scm_regular_string
, "#{", 2, port
);
452 scm_gen_write (scm_regular_string
, str
+ pos
, end
- pos
, port
);
458 scm_gen_write (scm_regular_string
, buf
, 2, port
);
480 scm_gen_write (scm_regular_string
, str
+ pos
, end
- pos
, port
);
482 scm_gen_write (scm_regular_string
, "}#", 2, port
);
486 ENTER_NESTED_DATA (pstate
, exp
, circref
);
487 if (SCM_IS_WHVEC (exp
))
488 scm_gen_puts (scm_regular_string
, "#wh(", port
);
490 scm_gen_puts (scm_regular_string
, "#w(", port
);
491 goto common_vector_printer
;
494 ENTER_NESTED_DATA (pstate
, exp
, circref
);
495 scm_gen_puts (scm_regular_string
, "#(", port
);
496 common_vector_printer
:
497 for (i
= 0; i
+ 1 < SCM_LENGTH (exp
); ++i
)
500 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
501 scm_gen_putc (' ', port
);
503 if (i
< SCM_LENGTH (exp
))
506 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
508 scm_gen_putc (')', port
);
509 EXIT_NESTED_DATA (pstate
);
522 scm_raprin1 (exp
, port
, pstate
);
525 scm_gen_puts (scm_regular_string
, "#<primitive-procedure ", port
);
526 scm_gen_puts ((SCM_MB_STRINGP (SCM_SNAME(exp
))
528 : scm_regular_string
),
529 SCM_CHARS (SCM_SNAME (exp
)), port
);
530 scm_gen_putc ('>', port
);
534 scm_gen_puts (scm_regular_string
, "#<compiled-closure ", port
);
535 scm_iprin1 (SCM_CCLO_SUBR (exp
), port
, pstate
);
536 scm_gen_putc ('>', port
);
540 scm_gen_puts (scm_regular_string
, "#<continuation ", port
);
541 scm_intprint (SCM_LENGTH (exp
), 10, port
);
542 scm_gen_puts (scm_regular_string
, " @ ", port
);
543 scm_intprint ((long) SCM_CHARS (exp
), 16, port
);
544 scm_gen_putc ('>', port
);
547 i
= SCM_PTOBNUM (exp
);
549 && scm_ptobs
[i
].print
550 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
554 ENTER_NESTED_DATA (pstate
, exp
, circref
);
555 i
= SCM_SMOBNUM (exp
);
556 if (i
< scm_numsmob
&& scm_smobs
[i
].print
557 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
559 EXIT_NESTED_DATA (pstate
);
562 EXIT_NESTED_DATA (pstate
);
565 scm_ipruk ("type", exp
, port
);
570 /* Print states are necessary for circular reference safe printing.
571 * They are also expensive to allocate. Therefore print states are
572 * kept in a pool so that they can be reused.
576 scm_prin1 (exp
, port
, writingp
)
581 SCM handle
= 0; /* Will GC protect the handle whilst unlinked */
582 scm_print_state
*pstate
;
584 /* First try to allocate a print state from the pool */
586 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
588 handle
= SCM_CDR (print_state_pool
);
589 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
594 handle
= scm_cons (make_print_state (), SCM_EOL
);
596 pstate
= SCM_PRINT_STATE (SCM_CAR (handle
));
597 pstate
->writingp
= writingp
;
598 scm_iprin1 (exp
, port
, pstate
);
600 /* Return print state to pool */
602 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
603 SCM_SETCDR (print_state_pool
, handle
);
612 scm_intprint (n
, radix
, port
)
617 char num_buf
[SCM_INTBUFLEN
];
618 scm_gen_write (scm_regular_string
, num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
621 /* Print an object of unrecognized type.
625 scm_ipruk (hdr
, ptr
, port
)
630 scm_gen_puts (scm_regular_string
, "#<unknown-", port
);
631 scm_gen_puts (scm_regular_string
, hdr
, port
);
634 scm_gen_puts (scm_regular_string
, " (0x", port
);
635 scm_intprint (SCM_CAR (ptr
), 16, port
);
636 scm_gen_puts (scm_regular_string
, " . 0x", port
);
637 scm_intprint (SCM_CDR (ptr
), 16, port
);
638 scm_gen_puts (scm_regular_string
, ") @", port
);
640 scm_gen_puts (scm_regular_string
, " 0x", port
);
641 scm_intprint (ptr
, 16, port
);
642 scm_gen_putc ('>', port
);
650 scm_iprlist (hdr
, exp
, tlr
, port
, pstate
)
655 scm_print_state
*pstate
;
658 register SCM hare
, tortoise
;
659 int floor
= pstate
->top
- 2;
660 scm_gen_puts (scm_regular_string
, hdr
, port
);
665 /* Run a hare and tortoise so that total time complexity will be
666 O(depth * N) instead of O(N^2). */
667 hare
= SCM_CDR (exp
);
669 while (SCM_NIMP (hare
) && SCM_ECONSP (hare
))
671 if (hare
== tortoise
)
673 hare
= SCM_CDR (hare
);
674 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
676 hare
= SCM_CDR (hare
);
677 tortoise
= SCM_CDR (tortoise
);
680 /* No cdr cycles intrinsic to this list */
681 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
683 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
685 if (SCM_NECONSP (exp
))
687 for (i
= floor
; i
>= 0; --i
)
688 if (pstate
->ref_stack
[i
] == exp
)
690 PUSH_REF (pstate
, exp
);
691 scm_gen_putc (' ', port
);
693 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
695 if (SCM_NNULLP (exp
))
697 scm_gen_puts (scm_regular_string
, " . ", port
);
698 scm_iprin1 (exp
, port
, pstate
);
702 scm_gen_putc (tlr
, port
);
703 pstate
->top
= floor
+ 2;
708 int n
= pstate
->length
;
710 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
711 exp
= SCM_CDR (exp
); --n
;
712 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
714 if (SCM_NECONSP (exp
))
716 for (i
= 0; i
< pstate
->top
; ++i
)
717 if (pstate
->ref_stack
[i
] == exp
)
723 scm_gen_puts (scm_regular_string
, " ...", port
);
729 PUSH_REF(pstate
, exp
);
730 ++pstate
->list_offset
;
731 scm_gen_putc (' ', port
);
733 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
736 if (SCM_NNULLP (exp
))
738 scm_gen_puts (scm_regular_string
, " . ", port
);
739 scm_iprin1 (exp
, port
, pstate
);
742 pstate
->list_offset
-= pstate
->top
- floor
- 2;
746 pstate
->list_offset
-= pstate
->top
- floor
- 2;
749 scm_gen_puts (scm_regular_string
, " . ", port
);
750 print_circref (port
, pstate
, exp
);
756 SCM_PROC(s_write
, "write", 1, 1, 0, scm_write
);
759 scm_write (obj
, port
)
763 if (SCM_UNBNDP (port
))
766 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG2
, s_write
);
767 scm_prin1 (obj
, port
, 1);
771 scm_close_port (port
);
774 return SCM_UNSPECIFIED
;
778 SCM_PROC(s_display
, "display", 1, 1, 0, scm_display
);
781 scm_display (obj
, port
)
785 if (SCM_UNBNDP (port
))
788 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG2
, s_display
);
789 scm_prin1 (obj
, port
, 0);
793 scm_close_port (port
);
796 return SCM_UNSPECIFIED
;
799 SCM_PROC(s_newline
, "newline", 0, 1, 0, scm_newline
);
805 if (SCM_UNBNDP (port
))
808 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_newline
);
809 scm_gen_putc ('\n', port
);
813 scm_close_port (port
);
817 if (port
== scm_cur_outp
)
819 return SCM_UNSPECIFIED
;
822 SCM_PROC(s_write_char
, "write-char", 1, 1, 0, scm_write_char
);
825 scm_write_char (chr
, port
)
829 if (SCM_UNBNDP (port
))
832 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG2
, s_write_char
);
833 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG1
, s_write_char
);
834 scm_gen_putc ((int) SCM_ICHR (chr
), port
);
838 scm_close_port (port
);
841 return SCM_UNSPECIFIED
;
852 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
853 vtable
= scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr
), SCM_INUM0
, SCM_EOL
);
854 type
= scm_make_struct (vtable
,
856 scm_cons (scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
)),
858 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));