home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / basics / printutil.sml < prev    next >
Encoding:
Text File  |  1993-01-27  |  2.2 KB  |  78 lines

  1. (* Copyright 1989 by AT&T Bell Laboratories *)
  2. structure PrintUtil : PRINTUTIL = struct
  3.  
  4.   val say = System.Print.say
  5.  
  6.   structure Symbol : SYMBOL = Symbol
  7.  
  8.   fun newline () = say "\n"
  9.   fun tab 0 = () | tab n = (say " "; tab(n-1))
  10.  
  11.   fun printSequence (separator: string) pr elems =
  12.       let fun prElems [el] = pr el
  13.         | prElems (el::rest) = (pr el; say separator; prElems rest)
  14.         | prElems [] = ()
  15.        in prElems elems
  16.       end
  17.  
  18.   fun printClosedSequence (front: string, sep, back:string) pr elems =
  19.       (say front; printSequence sep pr elems; say back)
  20.  
  21.   fun printSym(s: Symbol.symbol) = print(Symbol.name s)
  22.       (* fix -- maybe this belongs in Symbol *)
  23.  
  24.   fun formatQid p =
  25.     let fun f [s] = [Symbol.name s]
  26.           | f (a::r) = Symbol.name a :: "." :: f r
  27.       | f nil = ["<bogus qid>"]
  28.      in implode(f p)
  29.     end
  30.  
  31.   val stringDepth = System.Print.stringDepth
  32.  
  33.   fun decimal i = let val m = Integer.makestring
  34.           in  m(i div 100)^m((i div 10)mod 10)^m(i mod 10) end
  35.   val ctrl_a = 1
  36.   val ctrl_z = 26
  37.   val offset = ord "A" - ctrl_a
  38.   val smallestprintable = ord " "
  39.   val biggestprintable = ord "~"
  40.   fun ml_char "\n" = "\\n"
  41.     | ml_char "\t" = "\\t"
  42.     | ml_char "\\" = "\\\\"
  43.     | ml_char "\"" = "\\\""
  44.     | ml_char c =
  45.       let val char = ord c
  46.       in  if char >= ctrl_a andalso char <= ctrl_z
  47.           then "\\^" ^ chr(char+offset)
  48.           else if char >= smallestprintable andalso char <= biggestprintable
  49.            then c
  50.           else "\\" ^ decimal char
  51.       end
  52.  
  53.   fun mlstr s = "\"" ^ implode(map ml_char (explode s)) ^ "\""
  54.   fun pr_mlstr s =
  55.       let val depth = !stringDepth
  56.       fun pr i =
  57.           if i=depth then say "#"
  58.           else (let val ch = substring(s,i,1)
  59.             in  print(ml_char ch); pr (i+1)
  60.             end handle Substring => ())
  61.       in say "\""; pr 0; say "\""
  62.       end
  63.  
  64.   fun nlindent n = (newline(); tab n)
  65.  
  66.   fun printvseq ind (sep:string) pr elems =
  67.       let fun prElems [el] = pr el
  68.         | prElems (el::rest) = (pr el; nlindent ind; say sep; prElems rest)
  69.         | prElems [] = ()
  70.        in prElems elems
  71.       end
  72.  
  73.   (* debug print functions *)
  74.   val prIntPath = printClosedSequence ("[",",","]") (say o Integer.makestring)
  75.   val prSymPath = printSequence "." printSym
  76.  
  77. end (* structure PrintUtil *)
  78.