summaryrefslogtreecommitdiff
path: root/ipl/progs/ipsort.icn
blob: 2ac9083f2f6737ae3689ceea69e766dbf499852d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
############################################################################
#
#	File:     ipsort.icn
#
#	Subject:  Program to sort Icon procedures
#
#	Author:   Ralph E. Griswold
#
#	Date:     June 27, 1997
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#  
#     This program reads an Icon program and writes an equivalent
#  program with the procedures sorted alphabetically. Global, link,
#  and record declarations come first in the order they appear in
#  the original program.  The main procedure comes next followed by
#  the remaining procedures in alphabetical order.
#  
#     Comments and white space between declarations are attached to
#  the next following declaration.
#  
#  Limitations: This program only recognizes declarations that start
#  at the beginning of a line.
#  
#     Comments and interline white space between declarations may
#  not come out as intended.
#
#     One option is accepted:
#
#	-v	preserve VIB section at end
#  
############################################################################
#
#  Links:  options
#
############################################################################

link options

procedure main(args)
   local line, x, i, proctable, proclist, comments, procname, opts, vib

   opts := options(args, "v")

   vib := opts["v"]
   comments := []			# list of comment lines
   proctable := table()			# table of procedure declarations

   while line := read() do {
     line ? {
        if \vib & ="#===<<vib:begin>>===" then break
        if ="procedure" &		#  procedure declaration
           tab(many('\t ')) &
           procname := tab(upto('(')) | stop("*** bad syntax: ",line)
        then {				# if main, force sorting order
           if procname == "main" then procname := "\0main"
           proctable[procname] := x := []
           while put(x,get(comments))	#  save it
           put(x,line)
           while line := read() do {
              put(x,line)
              if line == "end" then break
              }
           }
					#  other declarations
         else if =("global" | "record" | "link" | "invocable")
         then {
            while write(get(comments))
            write(line)
            }
         else put(comments,line)
         }
      }

   while write(get(comments))

   proclist := sort(proctable,3)		#  sort procedures

   while get(proclist) do
      every write(!get(proclist))

   if \vib then {
      write()
      write(line)
      while write(read())
      }

end