home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / xlispplu / xlisp2tc / xlpp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-25  |  2.6 KB  |  128 lines

  1. /* xlpp.c - xlisp pretty printer */
  2. /*    Copyright (c) 1985, by David Betz
  3.     All Rights Reserved            */
  4.  
  5. #include "xlisp.h"
  6.  
  7. /* external variables */
  8. extern LVAL s_stdout;
  9. extern int xlfsize;
  10.  
  11. /* local variables */
  12. static int pplevel,ppmargin,ppmaxlen;
  13. static LVAL ppfile;
  14.  
  15. /* forward declarations */
  16. #ifdef PROTOTYPES
  17. LOCAL(void) ppputc(int) ;
  18. LOCAL(void) pp(LVAL) ;
  19. LOCAL(void) pplist(LVAL) ;
  20. LOCAL(void) ppexpr(LVAL) ;
  21. LOCAL(void) ppterpri(void) ;
  22. LOCAL(int) flatsize(LVAL) ;
  23. #else
  24. FORWARD void pp(),ppputc(),pplist(),ppexpr(),ppterpri();
  25. FORWARD int flatsize();
  26. #endif PROTOTYPES
  27.  
  28. /* xpp - pretty-print an expression */
  29. LVAL xpp()
  30. {
  31.     LVAL expr;
  32.  
  33.     /* get expression to print and file pointer */
  34.     expr = xlgetarg();
  35.     ppfile = (moreargs() ? xlgetfile() : getvalue(s_stdout));
  36.     xllastarg();
  37.  
  38.     /* pretty print the expression */
  39.     pplevel = ppmargin = 0; ppmaxlen = 40;
  40.     pp(expr);
  41.     /* ppterpri(ppfile); OOPS, doesn't take parm -rdb */
  42.     ppterpri();
  43.  
  44.     /* return nil */
  45.     return (NIL);
  46. }
  47.  
  48. /* pp - pretty print an expression */
  49. LOCAL(void) pp(expr)
  50.   LVAL expr;
  51. {
  52.     if (consp(expr))
  53.     pplist(expr);
  54.     else
  55.     ppexpr(expr);
  56. }
  57.  
  58. /* pplist - pretty print a list */
  59. LOCAL(void) pplist(expr)
  60.   LVAL expr;
  61. {
  62.     int n;
  63.  
  64.     /* if the expression will fit on one line, print it on one */
  65.     if ((n = flatsize(expr)) < ppmaxlen) {
  66.     xlprint(ppfile,expr,TRUE);
  67.     pplevel += n;
  68.     }
  69.  
  70.     /* otherwise print it on several lines */
  71.     else {
  72.     n = ppmargin;
  73.     ppputc('(');
  74.     if (atom(car(expr))) {
  75.         ppexpr(car(expr));
  76.         ppputc(' ');
  77.         ppmargin = pplevel;
  78.         expr = cdr(expr);
  79.     }
  80.     else
  81.         ppmargin = pplevel;
  82.     for (; consp(expr); expr = cdr(expr)) {
  83.         pp(car(expr));
  84.         if (consp(cdr(expr)))
  85.         ppterpri();
  86.     }
  87.     if (expr != NIL) {
  88.         ppputc(' '); ppputc('.'); ppputc(' ');
  89.         ppexpr(expr);
  90.     }
  91.     ppputc(')');
  92.     ppmargin = n;
  93.     }
  94. }
  95.  
  96. /* ppexpr - print an expression and update the indent level */
  97. LOCAL(void) ppexpr(expr)
  98.   LVAL expr;
  99. {
  100.     xlprint(ppfile,expr,TRUE);
  101.     pplevel += flatsize(expr);
  102. }
  103.  
  104. /* ppputc - output a character and update the indent level */
  105. LOCAL(void) ppputc(ch)
  106.   int ch;
  107. {
  108.     xlputc(ppfile,ch);
  109.     pplevel++;
  110. }
  111.  
  112. /* ppterpri - terminate the print line and indent */
  113. LOCAL(void) ppterpri()
  114. {
  115.     xlterpri(ppfile);
  116.     for (pplevel = 0; pplevel < ppmargin; pplevel++)
  117.     xlputc(ppfile,' ');
  118. }
  119.  
  120. /* flatsize - compute the flat size of an expression */
  121. LOCAL(int) flatsize(expr)
  122.   LVAL expr;
  123. {
  124.     xlfsize = 0;
  125.     xlprint(NIL,expr,TRUE);
  126.     return (xlfsize);
  127. }
  128.