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 (SCM setting
)
114 scm_print_options (setting
)
118 SCM ans
= scm_options (setting
,
126 /* {Printing of Scheme Objects}
129 /* Detection of circular references.
131 * Due to other constraints in the implementation, this code has bad
132 * time complexity (O (depth * N)), The printer code will be
133 * completely rewritten before next release of Guile. The new code
136 #define PUSH_REF(pstate, obj) \
138 pstate->ref_stack[pstate->top++] = (obj); \
139 if (pstate->top == pstate->ceiling) \
140 grow_ref_stack (pstate); \
143 #define ENTER_NESTED_DATA(pstate, obj, label) \
146 for (i = 0; i < pstate->top; ++i) \
147 if (pstate->ref_stack[i] == (obj)) \
149 if (pstate->fancyp) \
151 if (pstate->top - pstate->list_offset >= pstate->level) \
153 scm_gen_putc ('#', port); \
157 PUSH_REF(pstate, obj); \
160 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
162 static SCM print_state_pool
;
164 #if 1 /* Used for debugging purposes */
165 SCM_PROC(s_current_pstate
, "current-pstate", 0, 0, 0, scm_current_pstate
);
168 scm_current_pstate (void)
171 scm_current_pstate ()
174 return SCM_CADR (print_state_pool
);
178 #define PSTATE_SIZE 50L
182 scm_make_print_state (void)
185 scm_make_print_state ()
188 return scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
189 SCM_MAKINUM (PSTATE_SIZE
),
195 grow_ref_stack (scm_print_state
*pstate
)
198 grow_ref_stack (pstate
)
199 scm_print_state
*pstate
;
202 int i
, size
= pstate
->ceiling
;
207 handle
= pstate
->handle
;
208 data
= (SCM
*) pstate
- scm_struct_n_extra_words
;
209 total_size
= ((SCM
*) pstate
)[scm_struct_i_n_words
];
210 data
= (SCM
*) scm_must_realloc ((char *) data
,
214 pstate
= (scm_print_state
*) (data
+ scm_struct_n_extra_words
);
215 ((SCM
*) pstate
)[scm_struct_i_n_words
] = total_size
+ size
;
216 pstate
->ceiling
+= size
;
217 for (i
= size
; i
< pstate
->ceiling
; ++i
)
218 pstate
->ref_stack
[i
] = SCM_BOOL_F
;
219 SCM_SETCDR (handle
, pstate
);
225 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
228 print_circref (port
, pstate
, ref
)
230 scm_print_state
*pstate
;
235 int self
= pstate
->top
- 1;
237 if (SCM_CONSP (pstate
->ref_stack
[i
]))
241 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
242 || SCM_CDR (pstate
->ref_stack
[i
- 1]) != pstate
->ref_stack
[i
])
248 for (i
= pstate
->top
- 1; 1; --i
)
249 if (pstate
->ref_stack
[i
] == ref
)
251 scm_gen_putc ('#', port
);
252 scm_intprint (i
- self
, 10, port
);
253 scm_gen_putc ('#', port
);
256 /* Print generally. Handles both write and display according to PSTATE.
261 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
264 scm_iprin1 (exp
, port
, pstate
)
267 scm_print_state
*pstate
;
272 switch (7 & (int) exp
)
276 scm_intprint (SCM_INUM (exp
), 10, port
);
282 scm_put_wchar (i
, port
, SCM_WRITINGP (pstate
));
285 else if (SCM_IFLAGP (exp
)
286 && (SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
287 scm_gen_puts (scm_regular_string
, SCM_ISYMCHARS (exp
), port
);
288 else if (SCM_ILOCP (exp
))
290 scm_gen_puts (scm_regular_string
, "#@", port
);
291 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
292 scm_gen_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
293 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
300 scm_gen_puts (scm_regular_string
, "#@", port
);
301 exp
= SCM_CAR (exp
- 1);
305 scm_ipruk ("immediate", exp
, port
);
308 switch (SCM_TYP7 (exp
))
310 case scm_tcs_cons_gloc
:
312 if (SCM_CDR (SCM_CAR (exp
) - 1L) == 0)
314 scm_gen_write (scm_regular_string
, "#<struct ", sizeof ("#<struct ") - 1, port
);
315 scm_intprint(exp
, 16, port
);
316 scm_gen_putc ('>', port
);
320 case scm_tcs_cons_imcar
:
321 case scm_tcs_cons_nimcar
:
322 ENTER_NESTED_DATA (pstate
, exp
, circref
);
323 scm_iprlist ("(", exp
, ')', port
, pstate
);
324 EXIT_NESTED_DATA (pstate
);
327 print_circref (port
, pstate
, exp
);
329 case scm_tcs_closures
:
330 if (SCM_NFALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
)))
332 SCM ans
= scm_cons2 (exp
, port
,
333 scm_cons (SCM_WRITINGP (pstate
)
337 ans
= scm_apply (SCM_PRINT_CLOSURE
, ans
, SCM_EOL
);
342 name
= scm_procedure_property (exp
, scm_i_name
);
343 code
= SCM_CODE (exp
);
344 scm_gen_puts (scm_regular_string
, "#<procedure ", port
);
345 if (SCM_NIMP (name
) && SCM_ROSTRINGP (name
))
347 scm_gen_puts (scm_regular_string
, SCM_ROCHARS (name
), port
);
348 scm_gen_putc (' ', port
);
350 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
351 if (SCM_PRINT_SOURCE_P
)
353 code
= scm_unmemocopy (SCM_CDR (code
),
354 SCM_EXTEND_ENV (SCM_CAR (code
),
357 ENTER_NESTED_DATA (pstate
, exp
, circref
);
358 scm_iprlist (" ", code
, '>', port
, pstate
);
359 EXIT_NESTED_DATA (pstate
);
362 scm_gen_putc ('>', port
);
365 case scm_tc7_mb_string
:
366 case scm_tc7_mb_substring
:
367 scm_print_mb_string (exp
, port
, SCM_WRITINGP (pstate
));
369 case scm_tc7_substring
:
371 if (SCM_WRITINGP (pstate
))
373 scm_gen_putc ('"', port
);
374 for (i
= 0; i
< SCM_ROLENGTH (exp
); ++i
)
375 switch (SCM_ROCHARS (exp
)[i
])
379 scm_gen_putc ('\\', port
);
381 scm_gen_putc (SCM_ROCHARS (exp
)[i
], port
);
383 scm_gen_putc ('"', port
);
387 scm_gen_write (scm_regular_string
, SCM_ROCHARS (exp
),
388 (scm_sizet
) SCM_ROLENGTH (exp
),
391 case scm_tcs_symbols
:
392 if (SCM_MB_STRINGP (exp
))
394 scm_print_mb_symbol (exp
, port
);
407 len
= SCM_LENGTH (exp
);
408 str
= SCM_CHARS (exp
);
415 scm_gen_write (scm_regular_string
, "#{}#", 4, port
);
417 for (end
= pos
; end
< len
; ++end
)
420 #ifdef BRACKETS_AS_PARENS
428 case SCM_WHITE_SPACES
:
429 case SCM_LINE_INCREMENTORS
:
438 scm_gen_write (scm_regular_string
, "#{", 2, port
);
443 scm_gen_write (scm_regular_string
, str
+ pos
, end
- pos
, port
);
449 scm_gen_write (scm_regular_string
, buf
, 2, port
);
471 scm_gen_write (scm_regular_string
, str
+ pos
, end
- pos
, port
);
473 scm_gen_write (scm_regular_string
, "}#", 2, port
);
477 ENTER_NESTED_DATA (pstate
, exp
, circref
);
478 if (SCM_IS_WHVEC (exp
))
479 scm_gen_puts (scm_regular_string
, "#wh(", port
);
481 scm_gen_puts (scm_regular_string
, "#w(", port
);
482 goto common_vector_printer
;
485 ENTER_NESTED_DATA (pstate
, exp
, circref
);
486 scm_gen_puts (scm_regular_string
, "#(", port
);
487 common_vector_printer
:
488 for (i
= 0; i
+ 1 < SCM_LENGTH (exp
); ++i
)
491 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
492 scm_gen_putc (' ', port
);
494 if (i
< SCM_LENGTH (exp
))
497 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
499 scm_gen_putc (')', port
);
500 EXIT_NESTED_DATA (pstate
);
513 scm_raprin1 (exp
, port
, pstate
);
516 scm_gen_puts (scm_regular_string
, "#<primitive-procedure ", port
);
517 scm_gen_puts ((SCM_MB_STRINGP (SCM_SNAME(exp
))
519 : scm_regular_string
),
520 SCM_CHARS (SCM_SNAME (exp
)), port
);
521 scm_gen_putc ('>', port
);
525 scm_gen_puts (scm_regular_string
, "#<compiled-closure ", port
);
526 scm_iprin1 (SCM_CCLO_SUBR (exp
), port
, pstate
);
527 scm_gen_putc ('>', port
);
531 scm_gen_puts (scm_regular_string
, "#<continuation ", port
);
532 scm_intprint (SCM_LENGTH (exp
), 10, port
);
533 scm_gen_puts (scm_regular_string
, " @ ", port
);
534 scm_intprint ((long) SCM_CHARS (exp
), 16, port
);
535 scm_gen_putc ('>', port
);
538 i
= SCM_PTOBNUM (exp
);
540 && scm_ptobs
[i
].print
541 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
545 ENTER_NESTED_DATA (pstate
, exp
, circref
);
546 i
= SCM_SMOBNUM (exp
);
547 if (i
< scm_numsmob
&& scm_smobs
[i
].print
548 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
550 EXIT_NESTED_DATA (pstate
);
553 EXIT_NESTED_DATA (pstate
);
556 scm_ipruk ("type", exp
, port
);
561 /* Print states are necessary for circular reference safe printing.
562 * They are also expensive to allocate. Therefore print states are
563 * kept in a pool so that they can be reused.
567 scm_prin1 (SCM exp
, SCM port
, int writingp
)
570 scm_prin1 (exp
, port
, writingp
)
576 SCM handle
= 0; /* Will GC protect the handle whilst unlinked */
577 scm_print_state
*pstate
;
579 /* First try to allocate a print state from the pool */
581 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
583 handle
= SCM_CDR (print_state_pool
);
584 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
589 handle
= scm_cons (scm_make_print_state (), SCM_EOL
);
591 pstate
= (scm_print_state
*) SCM_STRUCT_DATA (SCM_CAR (handle
));
592 pstate
->writingp
= writingp
;
593 scm_iprin1 (exp
, port
, pstate
);
595 /* Return print state to pool */
597 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
598 SCM_SETCDR (print_state_pool
, handle
);
607 scm_intprint (long n
, int radix
, SCM port
)
610 scm_intprint (n
, radix
, port
)
616 char num_buf
[SCM_INTBUFLEN
];
617 scm_gen_write (scm_regular_string
, num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
620 /* Print an object of unrecognized type.
624 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
627 scm_ipruk (hdr
, ptr
, port
)
633 scm_gen_puts (scm_regular_string
, "#<unknown-", port
);
634 scm_gen_puts (scm_regular_string
, hdr
, port
);
637 scm_gen_puts (scm_regular_string
, " (0x", port
);
638 scm_intprint (SCM_CAR (ptr
), 16, port
);
639 scm_gen_puts (scm_regular_string
, " . 0x", port
);
640 scm_intprint (SCM_CDR (ptr
), 16, port
);
641 scm_gen_puts (scm_regular_string
, ") @", port
);
643 scm_gen_puts (scm_regular_string
, " 0x", port
);
644 scm_intprint (ptr
, 16, port
);
645 scm_gen_putc ('>', port
);
653 scm_iprlist (char *hdr
, SCM exp
, char tlr
, SCM port
, scm_print_state
*pstate
)
656 scm_iprlist (hdr
, exp
, tlr
, port
, pstate
)
661 scm_print_state
*pstate
;
665 register SCM hare
, tortoise
;
666 int floor
= pstate
->top
- 2;
667 scm_gen_puts (scm_regular_string
, hdr
, port
);
672 /* Run a hare and tortoise so that total time complexity will be
673 O(depth * N) instead of O(N^2). */
674 hare
= SCM_CDR (exp
);
676 while (SCM_NIMP (hare
) && SCM_ECONSP (hare
))
678 if (hare
== tortoise
)
680 hare
= SCM_CDR (hare
);
681 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
683 hare
= SCM_CDR (hare
);
684 tortoise
= SCM_CDR (tortoise
);
687 /* No cdr cycles intrinsic to this list */
688 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
690 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
692 if (SCM_NECONSP (exp
))
694 for (i
= floor
; i
>= 0; --i
)
695 if (pstate
->ref_stack
[i
] == exp
)
697 PUSH_REF (pstate
, exp
);
698 scm_gen_putc (' ', port
);
700 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
702 if (SCM_NNULLP (exp
))
704 scm_gen_puts (scm_regular_string
, " . ", port
);
705 scm_iprin1 (exp
, port
, pstate
);
709 scm_gen_putc (tlr
, port
);
710 pstate
->top
= floor
+ 2;
715 int n
= pstate
->length
;
717 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
718 exp
= SCM_CDR (exp
); --n
;
719 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
721 if (SCM_NECONSP (exp
))
723 for (i
= 0; i
< pstate
->top
; ++i
)
724 if (pstate
->ref_stack
[i
] == exp
)
730 scm_gen_puts (scm_regular_string
, " ...", port
);
736 PUSH_REF(pstate
, exp
);
737 ++pstate
->list_offset
;
738 scm_gen_putc (' ', port
);
740 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
743 if (SCM_NNULLP (exp
))
745 scm_gen_puts (scm_regular_string
, " . ", port
);
746 scm_iprin1 (exp
, port
, pstate
);
749 pstate
->list_offset
-= pstate
->top
- floor
- 2;
753 pstate
->list_offset
-= pstate
->top
- floor
- 2;
756 scm_gen_puts (scm_regular_string
, " . ", port
);
757 print_circref (port
, pstate
, exp
);
763 SCM_PROC(s_write
, "write", 1, 1, 0, scm_write
);
766 scm_write (SCM obj
, SCM port
)
769 scm_write (obj
, port
)
774 if (SCM_UNBNDP (port
))
777 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG2
, s_write
);
778 scm_prin1 (obj
, port
, 1);
782 scm_close_port (port
);
785 return SCM_UNSPECIFIED
;
789 SCM_PROC(s_display
, "display", 1, 1, 0, scm_display
);
792 scm_display (SCM obj
, SCM port
)
795 scm_display (obj
, port
)
800 if (SCM_UNBNDP (port
))
803 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG2
, s_display
);
804 scm_prin1 (obj
, port
, 0);
808 scm_close_port (port
);
811 return SCM_UNSPECIFIED
;
814 SCM_PROC(s_newline
, "newline", 0, 1, 0, scm_newline
);
817 scm_newline(SCM port
)
824 if (SCM_UNBNDP (port
))
827 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_newline
);
828 scm_gen_putc ('\n', port
);
832 scm_close_port (port
);
836 if (port
== scm_cur_outp
)
838 return SCM_UNSPECIFIED
;
841 SCM_PROC(s_write_char
, "write-char", 1, 1, 0, scm_write_char
);
844 scm_write_char (SCM chr
, SCM port
)
847 scm_write_char (chr
, port
)
852 if (SCM_UNBNDP (port
))
855 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG2
, s_write_char
);
856 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG1
, s_write_char
);
857 scm_gen_putc ((int) SCM_ICHR (chr
), port
);
861 scm_close_port (port
);
864 return SCM_UNSPECIFIED
;
872 scm_init_print (void)
879 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
880 vtable
= scm_make_vtable_vtable (scm_make_struct_layout (scm_makfrom0str ("")), SCM_INUM0
, SCM_EOL
);
881 type
= scm_make_struct (vtable
,
883 scm_cons (scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
)),
885 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));