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 / toktab.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  127 lines

  1. ############################################################################
  2. #
  3. #    File:     toktab.icn
  4. #
  5. #    Subject:  Program to summarize Icon token counts
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     June 21, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program reads the token files given on the command line and
  18. #  summarizes them in a single file.
  19. #
  20. #  The supported options are:
  21. #
  22. #    -n    sort tokens by category in decreasing numerical order;
  23. #           default alphabetical sorting
  24. #    -l i    limit output in any category to i items; default no limit
  25. #
  26. ############################################################################
  27. #
  28. #  Links:  options, showtbl
  29. #
  30. ############################################################################
  31.  
  32. link options
  33. link showtbl
  34.  
  35. global binops, unops, vars, controls, procs, others, keys
  36. global clits, ilits, rlits, slits
  37. global summary, globals, locals, statics, declarations, fields, files, parms
  38. global fldref
  39.  
  40. procedure main(args)
  41.    local names, tables, i, file, input, count, line, tbl, opts, k, limit
  42.    local total, result
  43.  
  44.    opts := options(args, "nl+")
  45.    k := if \opts["n"] then "val" else "ref"
  46.    limit := \opts["l"] | 2 ^ 31
  47.  
  48.    total := 0
  49.  
  50.    # WARNING:  The following data must match the data in tokgen.icn.
  51.    #           Ideally, they both should work from an include file.
  52.    #           Later ...
  53.  
  54.    #  Build a list of tables for the different types of tokens.  The order
  55.    #  of the tables determines the order of output.
  56.  
  57.    tables := []
  58.    every put(tables, (unops | binops | others | controls | keys | clits | 
  59.       ilits | rlits | slits | vars | fldref | declarations | globals |
  60.       locals | statics | parms | fields | files) := table(0))
  61.  
  62.    #  Create a list of names for the different types of tokens.  The order
  63.    #  of the names must correspond to the order of the tables above.
  64.  
  65.    names := ["Unary operators", "Binary operators", "Other operations", 
  66.       "Control structures", "Keywords", "Cset literals", "Integer literals",
  67.       "Real literals", "String literals",  "Variable references",
  68.       "Field references", "Declarations", "Globals", "Locals", "Statics",
  69.       "Procedure parameters", "Record fields", "Included files"]
  70.  
  71.    #  Read the token files
  72.  
  73.    every file := !args do {
  74.       input := open(file) | stop("*** cannot open ", file)
  75.       read(input)                # get rid of first line
  76.       while line := trim(read(input)) do {
  77.          line ? {
  78.             if ="Total tokens:" then break
  79.             if any(&ucase) & name := tab(upto(':')) & pos(-1) then {
  80.                (tbl := tables[index(names, name)]) |
  81.                   stop("*** invalid token category: ", name)
  82.                read(input)            # get rid of blank line
  83.                next
  84.                }
  85.             if *line = 0 then {
  86.                read(input)            # get rid of "total"
  87.                read(input)            # and blank line
  88.                next
  89.                }
  90.             if tab(upto(&digits)) then {
  91.                count := tab(many(&digits)) | next
  92.                tab(many(' '))
  93.                name := tab(0)
  94.                tbl[name] +:= count
  95.                }
  96.             }
  97.          }
  98.          close(input)
  99.       }
  100.  
  101.    #  Now output the results
  102.  
  103.    every i := 1 to *names do {
  104.       result := showtbl(names[i], tables[i], k, limit)
  105.       count := result[1]
  106.       total +:= count
  107.       if result[2] > limit then write("        ...") else write()
  108.       write(right(count, 8), "   total")
  109.       }
  110.    write("\nTotal tokens: ", total)
  111.  
  112.  
  113. end
  114.  
  115. #  This procedure returns the first index in L whose corresponding element
  116. #  is x
  117.  
  118. procedure index(L, x)
  119.    local i
  120.  
  121.    every i := 1 to *L do
  122.       if L[i] === x then return i
  123.  
  124.    fail
  125.  
  126. end
  127.