home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / ipsort.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  93 lines

  1. ############################################################################
  2. #
  3. #    File:     ipsort.icn
  4. #
  5. #    Subject:  Program to sort Icon procedures
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     June 27, 1997
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #  
  17. #     This program reads an Icon program and writes an equivalent
  18. #  program with the procedures sorted alphabetically. Global, link,
  19. #  and record declarations come first in the order they appear in
  20. #  the original program.  The main procedure comes next followed by
  21. #  the remaining procedures in alphabetical order.
  22. #  
  23. #     Comments and white space between declarations are attached to
  24. #  the next following declaration.
  25. #  
  26. #  Limitations: This program only recognizes declarations that start
  27. #  at the beginning of a line.
  28. #  
  29. #     Comments and interline white space between declarations may
  30. #  not come out as intended.
  31. #
  32. #     One option is accepted:
  33. #
  34. #    -v    preserve VIB section at end
  35. #  
  36. ############################################################################
  37. #
  38. #  Links:  options
  39. #
  40. ############################################################################
  41.  
  42. link options
  43.  
  44. procedure main(args)
  45.    local line, x, i, proctable, proclist, comments, procname, opts, vib
  46.  
  47.    opts := options(args, "v")
  48.  
  49.    vib := opts["v"]
  50.    comments := []            # list of comment lines
  51.    proctable := table()            # table of procedure declarations
  52.  
  53.    while line := read() do {
  54.      line ? {
  55.         if \vib & ="#===<<vib:begin>>===" then break
  56.         if ="procedure" &        #  procedure declaration
  57.            tab(many('\t ')) &
  58.            procname := tab(upto('(')) | stop("*** bad syntax: ",line)
  59.         then {                # if main, force sorting order
  60.            if procname == "main" then procname := "\0main"
  61.            proctable[procname] := x := []
  62.            while put(x,get(comments))    #  save it
  63.            put(x,line)
  64.            while line := read() do {
  65.               put(x,line)
  66.               if line == "end" then break
  67.               }
  68.            }
  69.                     #  other declarations
  70.          else if =("global" | "record" | "link" | "invocable")
  71.          then {
  72.             while write(get(comments))
  73.             write(line)
  74.             }
  75.          else put(comments,line)
  76.          }
  77.       }
  78.  
  79.    while write(get(comments))
  80.  
  81.    proclist := sort(proctable,3)        #  sort procedures
  82.  
  83.    while get(proclist) do
  84.       every write(!get(proclist))
  85.  
  86.    if \vib then {
  87.       write()
  88.       write(line)
  89.       while write(read())
  90.       }
  91.  
  92. end
  93.