2002-07-20 Han-Wen <hanwen@cs.uu.nl>
[bpt/guile.git] / libguile / print.c
index 37eddea..63389f1 100644 (file)
@@ -39,8 +39,6 @@
  * whether to permit this exception to apply to your modifications.
  * If you do not wish that, delete this exception notice.  */
 
-/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
-   gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
 
 \f
 
@@ -64,6 +62,7 @@
 #include "libguile/strings.h"
 #include "libguile/strports.h"
 #include "libguile/vectors.h"
+#include "libguile/lang.h"
 
 #include "libguile/validate.h"
 #include "libguile/print.h"
@@ -76,7 +75,7 @@
 
 char *scm_isymnames[] =
 {
-  /* This table must agree with the declarations */
+  /* This table must agree with the list of SCM_IM_ constants in tags.h */
   "#@and",
   "#@begin",
   "#@case",
@@ -115,16 +114,17 @@ char *scm_isymnames[] =
   /* Multi-language support */
   
   "#@nil-cond",
-  "#@nil-ify",
-  "#@t-ify",
-  "#@0-cond",
-  "#@0-ify",
-  "#@1-ify",
   "#@bind",
   
   "#@delay",
-  
-  "#<unbound>"
+  "#@call-with-values",
+
+  "#<unbound>",
+
+  /* Elisp nil value.  This is its Scheme name; whenever it's printed
+     in Elisp, it should appear as the symbol `nil'. */
+
+  "#nil"
 };
 
 scm_t_option scm_print_opts[] = {
@@ -186,21 +186,20 @@ do { \
 
 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
 
-SCM scm_print_state_vtable;
-
-static SCM print_state_pool;
+SCM scm_print_state_vtable = SCM_BOOL_F;
+static SCM print_state_pool = SCM_EOL;
 
 #ifdef GUILE_DEBUG /* Used for debugging purposes */
 
 SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0, 
            (),
-           "Return the current-pstate -- the cadr of the\n"
+           "Return the current-pstate -- the car of the\n"
            "@code{print_state_pool}.  @code{current-pstate} is only\n"
            "included in @code{--enable-guile-debug} builds.")
 #define FUNC_NAME s_scm_current_pstate
 {
-  if (SCM_NNULLP (SCM_CDR (print_state_pool)))
-    return SCM_CADR (print_state_pool);
+  if (!SCM_NULLP (print_state_pool))
+    return SCM_CAR (print_state_pool);
   else
     return SCM_BOOL_F;
 }
@@ -213,12 +212,11 @@ SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
 static SCM
 make_print_state (void)
 {
-  SCM print_state = scm_make_struct (SCM_CAR (print_state_pool), /* pstate type */
-                                    SCM_INUM0,
-                                    SCM_EOL);
+  SCM print_state
+    = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL);
   scm_print_state *pstate = SCM_PRINT_STATE (print_state);
   pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
-  pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
+  pstate->ref_stack = SCM_WRITABLE_VELTS (pstate->ref_vect);
   pstate->ceiling = SCM_VECTOR_LENGTH (pstate->ref_vect);
   return print_state;
 }
@@ -230,10 +228,10 @@ scm_make_print_state ()
 
   /* First try to allocate a print state from the pool */
   SCM_DEFER_INTS;
-  if (SCM_NNULLP (SCM_CDR (print_state_pool)))
+  if (!SCM_NULLP (print_state_pool))
     {
-      answer = SCM_CADR (print_state_pool);
-      SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
+      answer = SCM_CAR (print_state_pool);
+      print_state_pool = SCM_CDR (print_state_pool);
     }
   SCM_ALLOW_INTS;
   
@@ -252,11 +250,9 @@ scm_free_print_state (SCM print_state)
    */
   pstate->fancyp = 0;
   pstate->revealed = 0;
-  SCM_NEWCELL (handle);
   SCM_DEFER_INTS;
-  SCM_SET_CELL_WORD_0 (handle, print_state);
-  SCM_SET_CELL_WORD_1 (handle, SCM_CDR (print_state_pool));
-  SCM_SETCDR (print_state_pool, handle);
+  handle = scm_cons (print_state, print_state_pool);
+  print_state_pool = handle;
   SCM_ALLOW_INTS;
 }
 
@@ -264,23 +260,22 @@ static void
 grow_ref_stack (scm_print_state *pstate)
 {
   unsigned long int old_size = SCM_VECTOR_LENGTH (pstate->ref_vect);
-  SCM *old_elts = SCM_VELTS (pstate->ref_vect);
+  SCM const *old_elts = SCM_VELTS (pstate->ref_vect);
   unsigned long int new_size = 2 * pstate->ceiling;
   SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
-  SCM *new_elts = SCM_VELTS (new_vect);
   unsigned long int i;
 
   for (i = 0; i != old_size; ++i)
-    new_elts [i] = old_elts [i];
+    SCM_VECTOR_SET (new_vect, i, old_elts [i]);
 
   pstate->ref_vect = new_vect;
-  pstate->ref_stack = new_elts;
+  pstate->ref_stack = SCM_WRITABLE_VELTS(new_vect);
   pstate->ceiling = new_size;
 }
 
 
 static void
-print_circref (SCM port,scm_print_state *pstate,SCM ref)
+print_circref (SCM port, scm_print_state *pstate, SCM ref)
 {
   register long i;
   long self = pstate->top - 1;
@@ -289,7 +284,7 @@ print_circref (SCM port,scm_print_state *pstate,SCM ref)
     {
       while (i > 0)
        {
-         if (SCM_NCONSP (pstate->ref_stack[i - 1])
+         if (!SCM_CONSP (pstate->ref_stack[i - 1])
              || !SCM_EQ_P (SCM_CDR (pstate->ref_stack[i - 1]), 
                            pstate->ref_stack[i]))
            break;
@@ -398,7 +393,6 @@ SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
 void 
 scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
 {
-taloop:
   switch (SCM_ITAG3 (exp))
     {
     case scm_tc3_closure:
@@ -452,39 +446,31 @@ taloop:
          scm_ipruk ("immediate", exp, port);
        }
       break;
-    case scm_tc3_cons_gloc:
-      /* gloc */
-      scm_puts ("#@", port);
-      exp = scm_module_reverse_lookup (scm_current_module (),
-                                      SCM_GLOC_VAR (exp));
-      goto taloop;
     case scm_tc3_cons:
       switch (SCM_TYP7 (exp))
        {
-       case scm_tcs_cons_gloc:
-
-         if (SCM_STRUCT_VTABLE_DATA (exp) [scm_vtable_index_vcell] == 0)
-           {
-             ENTER_NESTED_DATA (pstate, exp, circref);
-             if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
-               {
-                 SCM pwps, print = pstate->writingp ? g_write : g_display;
-                 if (!print)
-                   goto print_struct;
-                 SCM_NEWSMOB (pwps,
-                              scm_tc16_port_with_ps,
-                              SCM_UNPACK (scm_cons (port, pstate->handle)));
-                 scm_call_generic_2 (print, exp, pwps);
-               }
-             else
-               {
-               print_struct:
-                 scm_print_struct (exp, port, pstate);
-               }
-             EXIT_NESTED_DATA (pstate);
-             break;
-           }
-
+       case scm_tcs_struct:
+         {
+           ENTER_NESTED_DATA (pstate, exp, circref);
+           if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
+             {
+               SCM pwps, print = pstate->writingp ? g_write : g_display;
+               if (!print)
+                 goto print_struct;
+               SCM_NEWSMOB (pwps,
+                            scm_tc16_port_with_ps,
+                            SCM_UNPACK (scm_cons (port, pstate->handle)));
+               pstate->revealed = 1;
+               scm_call_generic_2 (print, exp, pwps);
+             }
+           else
+             {
+             print_struct:
+               scm_print_struct (exp, port, pstate);
+             }
+           EXIT_NESTED_DATA (pstate);
+         }
+         break;
        case scm_tcs_cons_imcar:
        case scm_tcs_cons_nimcar:
          ENTER_NESTED_DATA (pstate, exp, circref);
@@ -518,7 +504,6 @@ taloop:
              scm_putc ('>', port);
            }
          break;
-       case scm_tc7_substring:
        case scm_tc7_string:
          if (SCM_WRITINGP (pstate))
            {
@@ -541,10 +526,26 @@ taloop:
            scm_lfwrite (SCM_STRING_CHARS (exp), SCM_STRING_LENGTH (exp), port);
          break;
        case scm_tc7_symbol:
-         scm_print_symbol_name (SCM_SYMBOL_CHARS (exp),
-                                SCM_SYMBOL_LENGTH (exp),
-                                port);
-         scm_remember_upto_here_1 (exp);
+         if (SCM_SYMBOL_INTERNED_P (exp))
+           {
+             scm_print_symbol_name (SCM_SYMBOL_CHARS (exp),
+                                    SCM_SYMBOL_LENGTH (exp),
+                                    port);
+             scm_remember_upto_here_1 (exp);
+           }
+         else
+           {
+             scm_puts ("#<uninterned-symbol ", port);
+             scm_print_symbol_name (SCM_SYMBOL_CHARS (exp),
+                                    SCM_SYMBOL_LENGTH (exp),
+                                    port);
+             scm_putc (' ', port);
+             scm_intprint ((long)exp, 16, port);
+             scm_putc ('>', port);
+           }
+         break;
+       case scm_tc7_variable:
+         scm_i_variable_print (exp, port, pstate);
          break;
        case scm_tc7_wvect:
          ENTER_NESTED_DATA (pstate, exp, circref);
@@ -616,7 +617,7 @@ taloop:
                /* Print gsubrs as primitives */
                SCM name = scm_procedure_name (exp);
                scm_puts ("#<primitive-procedure", port);
-               if (SCM_NFALSEP (name))
+               if (!SCM_FALSEP (name))
                  {
                    scm_putc (' ', port);
                    scm_puts (SCM_SYMBOL_CHARS (name), port);
@@ -635,7 +636,7 @@ taloop:
          scm_puts ("#<procedure-with-setter", port);
          {
            SCM name = scm_procedure_name (exp);
-           if (SCM_NFALSEP (name))
+           if (!SCM_FALSEP (name))
              {
                scm_putc (' ', port);
                scm_display (name, port);
@@ -692,14 +693,14 @@ scm_prin1 (SCM exp, SCM port, int writingp)
     {
       /* First try to allocate a print state from the pool */
       SCM_DEFER_INTS;
-      if (SCM_NNULLP (SCM_CDR (print_state_pool)))
+      if (!SCM_NULLP (print_state_pool))
        {
-         handle = SCM_CDR (print_state_pool);
-         SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
+         handle = print_state_pool;
+         print_state_pool = SCM_CDR (print_state_pool);
        }
       SCM_ALLOW_INTS;
       if (SCM_FALSEP (handle))
-       handle = scm_cons (make_print_state (), SCM_EOL);
+       handle = scm_list_1 (make_print_state ());
       pstate_scm = SCM_CAR (handle);
     }
 
@@ -713,8 +714,8 @@ scm_prin1 (SCM exp, SCM port, int writingp)
   if (!SCM_FALSEP (handle) && !pstate->revealed)
     {
       SCM_DEFER_INTS;
-      SCM_SETCDR (handle, SCM_CDR (print_state_pool));
-      SCM_SETCDR (print_state_pool, handle);
+      SCM_SETCDR (handle, print_state_pool);
+      print_state_pool = handle;
       SCM_ALLOW_INTS;
     }
 }
@@ -738,7 +739,7 @@ scm_ipruk (char *hdr, SCM ptr, SCM port)
 {
   scm_puts ("#<unknown-", port);
   scm_puts (hdr, port);
-  if (SCM_CELLP (ptr))
+  if (scm_cellp (ptr))
     {
       scm_puts (" (0x", port);
       scm_intprint (SCM_CELL_WORD_0 (ptr), 16, port);
@@ -752,12 +753,10 @@ scm_ipruk (char *hdr, SCM ptr, SCM port)
 }
 
 
-/* Print a list.  The list may be either a list of ordinary data, or it may be
-   a list that represents code.  Lists that represent code may contain gloc
-   cells.
+/* Print a list.
  */
 void 
-scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate)
+scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
 {
   register SCM hare, tortoise;
   long floor = pstate->top - 2;
@@ -770,12 +769,12 @@ scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate)
      O(depth * N) instead of O(N^2). */
   hare = SCM_CDR (exp);
   tortoise = exp;
-  while (SCM_ECONSP (hare))
+  while (SCM_CONSP (hare))
     {
       if (SCM_EQ_P (hare, tortoise))
        goto fancy_printing;
       hare = SCM_CDR (hare);
-      if (SCM_IMP (hare) || SCM_NECONSP (hare))
+      if (!SCM_CONSP (hare))
        break;
       hare = SCM_CDR (hare);
       tortoise = SCM_CDR (tortoise);
@@ -783,7 +782,7 @@ scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate)
   
   /* No cdr cycles intrinsic to this list */
   scm_iprin1 (SCM_CAR (exp), port, pstate);
-  for (exp = SCM_CDR (exp); SCM_ECONSP (exp); exp = SCM_CDR (exp))
+  for (exp = SCM_CDR (exp); SCM_CONSP (exp); exp = SCM_CDR (exp))
     {
       register long i;
 
@@ -795,7 +794,7 @@ scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate)
       /* CHECK_INTS; */
       scm_iprin1 (SCM_CAR (exp), port, pstate);
     }
-  if (!SCM_NULLP (exp))
+  if (!SCM_NULL_OR_NIL_P (exp))
     {
       scm_puts (" . ", port);
       scm_iprin1 (exp, port, pstate);
@@ -812,7 +811,7 @@ fancy_printing:
     
     scm_iprin1 (SCM_CAR (exp), port, pstate);
     exp = SCM_CDR (exp); --n;
-    for (; SCM_ECONSP (exp); exp = SCM_CDR (exp))
+    for (; SCM_CONSP (exp); exp = SCM_CDR (exp))
       {
        register unsigned long i;
 
@@ -836,7 +835,7 @@ fancy_printing:
        scm_iprin1 (SCM_CAR (exp), port, pstate);
       }
   }
-  if (SCM_NNULLP (exp))
+  if (!SCM_NULL_OR_NIL_P (exp))
     {
       scm_puts (" . ", port);
       scm_iprin1 (exp, port, pstate);
@@ -969,6 +968,7 @@ SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
            start = p + 1;
            continue;
          case '%':
+           scm_lfwrite (start, p - start - 1, destination);
            scm_newline (destination);
            start = p + 1;
            continue;
@@ -990,7 +990,7 @@ SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
       }
 
   scm_lfwrite (start, p - start, destination);
-  if (args != SCM_EOL)
+  if (!SCM_EQ_P (args, SCM_EOL))
     SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
                    scm_list_1 (scm_length (args)));
 
@@ -1004,13 +1004,14 @@ SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
 
 SCM_DEFINE (scm_newline, "newline", 0, 1, 0, 
             (SCM port),
-           "Send a newline to @var{port}.")
+           "Send a newline to @var{port}.\n"
+           "If @var{port} is omitted, send to the current output port.")
 #define FUNC_NAME s_scm_newline
 {
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
 
-  SCM_VALIDATE_OPORT_VALUE (1,port);
+  SCM_VALIDATE_OPORT_VALUE (1, port);
 
   scm_putc ('\n', SCM_COERCE_OUTPORT (port));
   return SCM_UNSPECIFIED;
@@ -1025,8 +1026,8 @@ SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
 
-  SCM_VALIDATE_CHAR (1,chr);
-  SCM_VALIDATE_OPORT_VALUE (2,port);
+  SCM_VALIDATE_CHAR (1, chr);
+  SCM_VALIDATE_OPORT_VALUE (2, port);
 
   scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port));
 #ifdef HAVE_PIPE
@@ -1077,8 +1078,8 @@ SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0,
 #define FUNC_NAME s_scm_port_with_print_state
 {
   SCM pwps;
-  SCM_VALIDATE_OPORT_VALUE (1,port);
-  SCM_VALIDATE_PRINTSTATE (2,pstate);
+  SCM_VALIDATE_OPORT_VALUE (1, port);
+  SCM_VALIDATE_PRINTSTATE (2, pstate);
   port = SCM_COERCE_OUTPORT (port);
   SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (scm_cons (port, pstate)));
   return pwps;
@@ -1105,14 +1106,15 @@ void
 scm_init_print ()
 {
   SCM vtable, layout, type;
-  
+
   scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS);
+
+  scm_gc_register_root (&print_state_pool);
+  scm_gc_register_root (&scm_print_state_vtable);
   vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
   layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT));
   type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout));
   scm_set_struct_vtable_name_x (type, scm_str2symbol ("print-state"));
-  print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL));
-
   scm_print_state_vtable = type;
 
   /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
@@ -1120,9 +1122,7 @@ scm_init_print ()
   scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
   scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
   
-#ifndef SCM_MAGIC_SNARFER
 #include "libguile/print.x"
-#endif
 }
 
 /*