1 /* Copyright (C) 1995,1996,1997,1998 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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
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) \
141 register unsigned long i; \
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_putc ('#', port); \
153 PUSH_REF(pstate, obj); \
156 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
158 SCM scm_print_state_vtable
;
160 static SCM print_state_pool
;
162 #ifdef GUILE_DEBUG /* Used for debugging purposes */
163 SCM_PROC(s_current_pstate
, "current-pstate", 0, 0, 0, scm_current_pstate
);
166 scm_current_pstate ()
168 return SCM_CADR (print_state_pool
);
172 #define PSTATE_SIZE 50L
174 static SCM make_print_state
SCM_P ((void));
179 SCM print_state
= scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
182 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
183 pstate
->ref_vect
= scm_make_vector (SCM_MAKINUM (PSTATE_SIZE
),
185 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
186 pstate
->ceiling
= SCM_LENGTH (pstate
->ref_vect
);
191 scm_make_print_state ()
195 /* First try to allocate a print state from the pool */
197 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
199 answer
= SCM_CADR (print_state_pool
);
200 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
204 return answer
? answer
: make_print_state ();
207 static char s_print_state_printer
[] = "print-state-printer";
209 print_state_printer (obj
, port
)
213 /* This function can be made visible by means of struct-ref, so
214 we need to make sure that it gets what it wants. */
215 SCM_ASSERT (SCM_NIMP (obj
) && SCM_PRINT_STATE_P (obj
),
218 s_print_state_printer
);
219 SCM_ASSERT (scm_valid_oport_value_p (port
),
222 s_print_state_printer
);
223 port
= SCM_COERCE_OUTPORT (port
);
224 scm_puts ("#<print-state ", port
);
225 scm_intprint (obj
, 16, port
);
226 scm_putc ('>', port
);
227 return SCM_UNSPECIFIED
;
231 scm_free_print_state (print_state
)
235 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
236 /* Cleanup before returning print state to pool.
237 * It is better to do it here. Doing it in scm_prin1
238 * would cost more since that function is called much more
242 pstate
->revealed
= 0;
243 SCM_NEWCELL (handle
);
245 SCM_SETCAR (handle
, print_state
);
246 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
247 SCM_SETCDR (print_state_pool
, handle
);
251 static void grow_ref_stack
SCM_P ((scm_print_state
*pstate
));
254 grow_ref_stack (pstate
)
255 scm_print_state
*pstate
;
257 int new_size
= 2 * pstate
->ceiling
;
258 scm_vector_set_length_x (pstate
->ref_vect
, SCM_MAKINUM (new_size
));
259 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
260 pstate
->ceiling
= new_size
;
264 static void print_circref
SCM_P ((SCM port
, scm_print_state
*pstate
, SCM ref
));
267 print_circref (port
, pstate
, ref
)
269 scm_print_state
*pstate
;
273 int self
= pstate
->top
- 1;
275 if (SCM_CONSP (pstate
->ref_stack
[i
]))
279 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
280 || SCM_CDR (pstate
->ref_stack
[i
- 1]) != pstate
->ref_stack
[i
])
286 for (i
= pstate
->top
- 1; 1; --i
)
287 if (pstate
->ref_stack
[i
] == ref
)
289 scm_putc ('#', port
);
290 scm_intprint (i
- self
, 10, port
);
291 scm_putc ('#', port
);
294 /* Print generally. Handles both write and display according to PSTATE.
299 scm_iprin1 (exp
, port
, pstate
)
302 scm_print_state
*pstate
;
305 switch (7 & (int) exp
)
309 scm_intprint (SCM_INUM (exp
), 10, port
);
317 if (SCM_WRITINGP (pstate
))
319 scm_puts ("#\\", port
);
320 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
321 scm_puts (scm_charnames
[i
], port
);
322 else if (i
< 0 || i
> '\177')
323 scm_intprint (i
, 8, port
);
330 else if (SCM_IFLAGP (exp
)
331 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
332 scm_puts (SCM_ISYMCHARS (exp
), port
);
333 else if (SCM_ILOCP (exp
))
335 scm_puts ("#@", port
);
336 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
337 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
338 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
345 scm_puts ("#@", port
);
346 exp
= SCM_CAR (exp
- 1);
350 scm_ipruk ("immediate", exp
, port
);
353 switch (SCM_TYP7 (exp
))
355 case scm_tcs_cons_gloc
:
357 if (SCM_CDR (SCM_CAR (exp
) - 1L) == 0)
359 ENTER_NESTED_DATA (pstate
, exp
, circref
);
360 scm_print_struct (exp
, port
, pstate
);
361 EXIT_NESTED_DATA (pstate
);
365 case scm_tcs_cons_imcar
:
366 case scm_tcs_cons_nimcar
:
367 ENTER_NESTED_DATA (pstate
, exp
, circref
);
368 scm_iprlist ("(", exp
, ')', port
, pstate
);
369 EXIT_NESTED_DATA (pstate
);
372 print_circref (port
, pstate
, exp
);
375 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
377 case scm_tcs_closures
:
378 /* The user supplied print closure procedure must handle
379 macro closures as well. */
380 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
381 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
385 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
387 /* Printing a macro. */
389 name
= scm_macro_name (exp
);
390 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
393 scm_puts ("#<primitive-", port
);
397 code
= SCM_CODE (SCM_CDR (exp
));
398 env
= SCM_ENV (SCM_CDR (exp
));
399 scm_puts ("#<", port
);
401 if (SCM_CAR (exp
) & (3L << 16))
402 scm_puts ("macro", port
);
404 scm_puts ("syntax", port
);
405 if (SCM_CAR (exp
) & (2L << 16))
406 scm_putc ('!', port
);
410 /* Printing a closure. */
411 name
= scm_procedure_name (exp
);
412 code
= SCM_CODE (exp
);
414 scm_puts ("#<procedure", port
);
416 if (SCM_NIMP (name
) && SCM_ROSTRINGP (name
))
418 scm_putc (' ', port
);
419 scm_puts (SCM_ROCHARS (name
), port
);
423 if (SCM_PRINT_SOURCE_P
)
425 code
= scm_unmemocopy (code
,
426 SCM_EXTEND_ENV (SCM_CAR (code
),
429 ENTER_NESTED_DATA (pstate
, exp
, circref
);
430 scm_iprlist (" ", code
, '>', port
, pstate
);
431 EXIT_NESTED_DATA (pstate
);
435 if (SCM_TYP16 (exp
) != scm_tc16_macro
)
437 scm_putc (' ', port
);
438 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
440 scm_putc ('>', port
);
444 scm_putc ('>', port
);
447 case scm_tc7_substring
:
449 if (SCM_WRITINGP (pstate
))
453 scm_putc ('"', port
);
454 for (i
= 0; i
< SCM_ROLENGTH (exp
); ++i
)
455 switch (SCM_ROCHARS (exp
)[i
])
459 scm_putc ('\\', port
);
461 scm_putc (SCM_ROCHARS (exp
)[i
], port
);
463 scm_putc ('"', port
);
467 scm_lfwrite (SCM_ROCHARS (exp
), (scm_sizet
) SCM_ROLENGTH (exp
),
470 case scm_tcs_symbols
:
480 len
= SCM_LENGTH (exp
);
481 str
= SCM_CHARS (exp
);
488 scm_lfwrite ("#{}#", 4, port
);
490 for (end
= pos
; end
< len
; ++end
)
493 #ifdef BRACKETS_AS_PARENS
501 case SCM_WHITE_SPACES
:
502 case SCM_LINE_INCREMENTORS
:
511 scm_lfwrite ("#{", 2, port
);
516 scm_lfwrite (str
+ pos
, end
- pos
, port
);
522 scm_lfwrite (buf
, 2, port
);
544 scm_lfwrite (str
+ pos
, end
- pos
, port
);
546 scm_lfwrite ("}#", 2, port
);
550 ENTER_NESTED_DATA (pstate
, exp
, circref
);
551 if (SCM_IS_WHVEC (exp
))
552 scm_puts ("#wh(", port
);
554 scm_puts ("#w(", port
);
555 goto common_vector_printer
;
558 ENTER_NESTED_DATA (pstate
, exp
, circref
);
559 scm_puts ("#(", port
);
560 common_vector_printer
:
563 int last
= SCM_LENGTH (exp
) - 1;
565 if (pstate
->fancyp
&& SCM_LENGTH (exp
) > pstate
->length
)
567 last
= pstate
->length
- 1;
570 for (i
= 0; i
< last
; ++i
)
573 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
574 scm_putc (' ', port
);
579 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
582 scm_puts (" ...", port
);
583 scm_putc (')', port
);
585 EXIT_NESTED_DATA (pstate
);
598 scm_raprin1 (exp
, port
, pstate
);
601 scm_puts ("#<primitive-procedure ", port
);
602 scm_puts (SCM_CHARS (SCM_SNAME (exp
)), port
);
603 scm_putc ('>', port
);
607 scm_puts ("#<compiled-closure ", port
);
608 scm_iprin1 (SCM_CCLO_SUBR (exp
), port
, pstate
);
609 scm_putc ('>', port
);
613 scm_puts ("#<continuation ", port
);
614 scm_intprint (SCM_LENGTH (exp
), 10, port
);
615 scm_puts (" @ ", port
);
616 scm_intprint ((long) SCM_CHARS (exp
), 16, port
);
617 scm_putc ('>', port
);
621 register long i
= SCM_PTOBNUM (exp
);
623 && scm_ptobs
[i
].print
624 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
631 ENTER_NESTED_DATA (pstate
, exp
, circref
);
632 i
= SCM_SMOBNUM (exp
);
633 if (i
< scm_numsmob
&& scm_smobs
[i
].print
634 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
636 EXIT_NESTED_DATA (pstate
);
639 EXIT_NESTED_DATA (pstate
);
640 /* Macros have their print field set to NULL. They are
641 handled at the same place as closures in order to achieve
642 non-redundancy. Placing the condition here won't slow
643 down printing of other smobs. */
644 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
649 scm_ipruk ("type", exp
, port
);
654 /* Print states are necessary for circular reference safe printing.
655 * They are also expensive to allocate. Therefore print states are
656 * kept in a pool so that they can be reused.
659 /* The PORT argument can also be a print-state/port pair, which will
660 * then be used instead of allocating a new print state. This is
661 * useful for continuing a chain of print calls from Scheme. */
664 scm_prin1 (exp
, port
, writingp
)
669 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
671 scm_print_state
*pstate
;
673 /* If PORT is a print-state/port pair, use that. Else create a new
676 if (SCM_NIMP (port
) && SCM_CONSP (port
))
678 pstate_scm
= SCM_CDR (port
);
679 port
= SCM_CAR (port
);
683 /* First try to allocate a print state from the pool */
685 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
687 handle
= SCM_CDR (print_state_pool
);
688 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
691 if (handle
== SCM_BOOL_F
)
692 handle
= scm_cons (make_print_state (), SCM_EOL
);
693 pstate_scm
= SCM_CAR (handle
);
696 pstate
= SCM_PRINT_STATE (pstate_scm
);
697 pstate
->writingp
= writingp
;
698 scm_iprin1 (exp
, port
, pstate
);
700 /* Return print state to pool if it has been created above and
701 hasn't escaped to Scheme. */
703 if (handle
!= SCM_BOOL_F
&& !pstate
->revealed
)
706 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
707 SCM_SETCDR (print_state_pool
, handle
);
717 scm_intprint (n
, radix
, port
)
722 char num_buf
[SCM_INTBUFLEN
];
723 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
726 /* Print an object of unrecognized type.
730 scm_ipruk (hdr
, ptr
, port
)
735 scm_puts ("#<unknown-", port
);
736 scm_puts (hdr
, port
);
739 scm_puts (" (0x", port
);
740 scm_intprint (SCM_CAR (ptr
), 16, port
);
741 scm_puts (" . 0x", port
);
742 scm_intprint (SCM_CDR (ptr
), 16, port
);
743 scm_puts (") @", port
);
745 scm_puts (" 0x", port
);
746 scm_intprint (ptr
, 16, port
);
747 scm_putc ('>', port
);
755 scm_iprlist (hdr
, exp
, tlr
, port
, pstate
)
760 scm_print_state
*pstate
;
762 register SCM hare
, tortoise
;
763 int floor
= pstate
->top
- 2;
764 scm_puts (hdr
, port
);
769 /* Run a hare and tortoise so that total time complexity will be
770 O(depth * N) instead of O(N^2). */
771 hare
= SCM_CDR (exp
);
773 while (SCM_NIMP (hare
) && SCM_ECONSP (hare
))
775 if (hare
== tortoise
)
777 hare
= SCM_CDR (hare
);
778 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
780 hare
= SCM_CDR (hare
);
781 tortoise
= SCM_CDR (tortoise
);
784 /* No cdr cycles intrinsic to this list */
785 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
787 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
791 if (SCM_NECONSP (exp
))
793 for (i
= floor
; i
>= 0; --i
)
794 if (pstate
->ref_stack
[i
] == exp
)
796 PUSH_REF (pstate
, exp
);
797 scm_putc (' ', port
);
799 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
801 if (SCM_NNULLP (exp
))
803 scm_puts (" . ", port
);
804 scm_iprin1 (exp
, port
, pstate
);
808 scm_putc (tlr
, port
);
809 pstate
->top
= floor
+ 2;
814 int n
= pstate
->length
;
816 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
817 exp
= SCM_CDR (exp
); --n
;
818 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
820 register unsigned long i
;
822 if (SCM_NECONSP (exp
))
824 for (i
= 0; i
< pstate
->top
; ++i
)
825 if (pstate
->ref_stack
[i
] == exp
)
831 scm_puts (" ...", port
);
837 PUSH_REF(pstate
, exp
);
838 ++pstate
->list_offset
;
839 scm_putc (' ', port
);
841 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
844 if (SCM_NNULLP (exp
))
846 scm_puts (" . ", port
);
847 scm_iprin1 (exp
, port
, pstate
);
850 pstate
->list_offset
-= pstate
->top
- floor
- 2;
854 pstate
->list_offset
-= pstate
->top
- floor
- 2;
857 scm_puts (" . ", port
);
858 print_circref (port
, pstate
, exp
);
865 scm_valid_oport_value_p (SCM val
)
867 return (SCM_NIMP (val
)
868 && (SCM_OPOUTPORTP (val
)
870 && SCM_NIMP (SCM_CAR (val
))
871 && SCM_OPOUTPORTP (SCM_CAR (val
))
872 && SCM_NIMP (SCM_CDR (val
))
873 && SCM_PRINT_STATE_P (SCM_CDR (val
)))));
876 SCM_PROC(s_write
, "write", 1, 1, 0, scm_write
);
879 scm_write (obj
, port
)
883 if (SCM_UNBNDP (port
))
886 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
888 scm_prin1 (obj
, port
, 1);
892 scm_close_port (port
);
895 return SCM_UNSPECIFIED
;
899 SCM_PROC(s_display
, "display", 1, 1, 0, scm_display
);
902 scm_display (obj
, port
)
906 if (SCM_UNBNDP (port
))
909 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
911 scm_prin1 (obj
, port
, 0);
915 scm_close_port (port
);
918 return SCM_UNSPECIFIED
;
921 SCM_PROC(s_newline
, "newline", 0, 1, 0, scm_newline
);
927 if (SCM_UNBNDP (port
))
930 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG1
, s_newline
);
932 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
936 scm_close_port (port
);
940 if (port
== scm_cur_outp
)
942 return SCM_UNSPECIFIED
;
945 SCM_PROC(s_write_char
, "write-char", 1, 1, 0, scm_write_char
);
948 scm_write_char (chr
, port
)
952 if (SCM_UNBNDP (port
))
955 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write_char
);
957 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG1
, s_write_char
);
958 scm_putc ((int) SCM_ICHR (chr
), SCM_COERCE_OUTPORT (port
));
962 scm_close_port (port
);
965 return SCM_UNSPECIFIED
;
970 /* Call back to Scheme code to do the printing of special objects
971 (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a pair
972 containing PORT and PSTATE. This pair can be used as the port for
973 display/write etc to continue the current print chain. The REVEALED
974 field of PSTATE is set to true to indicate that the print state has
975 escaped to Scheme and thus has to be freed by the GC. */
978 scm_printer_apply (proc
, exp
, port
, pstate
)
980 scm_print_state
*pstate
;
982 SCM pair
= scm_cons (port
, pstate
->handle
);
983 pstate
->revealed
= 1;
984 return scm_apply (proc
, exp
, scm_cons (pair
, scm_listofnull
));
992 SCM vtable
, layout
, printer
, type
;
994 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
995 vtable
= scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr
),
998 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
999 printer
= scm_make_subr_opt (s_print_state_printer
,
1001 (SCM (*) ()) print_state_printer
,
1002 0 /* Don't bind the name. */);
1003 type
= scm_make_struct (vtable
, SCM_INUM0
, SCM_LIST2 (layout
, printer
));
1004 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));
1006 scm_print_state_vtable
= type
;