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

  1. ############################################################################
  2. #
  3. #    File:     iundecl.icn
  4. #
  5. #    Subject:  Program to find undeclared Icon identifiers
  6. #
  7. #    Authors:  Robert J. Alexander and Ralph E. Griswold
  8. #
  9. #    Date:     August 14, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program invokes icont to find undeclared variables in an Icon
  18. #  source program.  The output is in the form of a "local" declaration,
  19. #  preceded by a comment line that identifies that procedure and file
  20. #  name from whence it arose.  Beware that undeclared variables aren't
  21. #  necessarily local, so any which are intended to be global must be
  22. #  removed from the generated list.
  23. #
  24. #  Multiple files can be specified as arguments, and will be processed
  25. #  in sequence.  A file name of "-" represents the standard input file.
  26. #  If there are no arguments, standard input is processed.
  27. #
  28. #  The program works only if procedures are formatted such that the
  29. #  keywords "procedure" and "end" are the first words on their
  30. #  respective lines.
  31. #
  32. #  Only for UNIX, since the "p" (pipe) option of open() is used.
  33. #
  34. ############################################################################
  35. #
  36. #  Requires: UNIX
  37. #
  38. ############################################################################
  39. #
  40. #  Links:  io
  41. #
  42. ############################################################################
  43.  
  44. link io
  45.  
  46. procedure main(arg)
  47.    local f, fn, line, names, p, sep, t, argstring, undeclared, pn
  48.    #
  49.    #  Process command line file names.
  50.    #
  51.    if *arg = 0 then arg := ["-"] # if no arguments, standard input
  52.    #
  53.    #  Build a set of all the undeclared identifiers.
  54.    #
  55.    argstring := ""
  56.    every argstring ||:= " " || !arg
  57.    p := open("icont -s -u -o /dev/null 2>&1" || argstring,"p") |
  58.        stop("popen failed")
  59.    undeclared := set()
  60.    while line := read(p) do line ? {
  61.          if find("version mismatch") then {
  62.                write(&errout, line)
  63.                exit()
  64.                }
  65.      if find("undeclared identifier") then
  66.            tab(find("\"") + 1) & insert(undeclared,tab(find("\"")))
  67.          }
  68.    close(p)
  69.    #
  70.    #  Loop through files to process individual procedures.
  71.    #
  72.    every fn := !arg do {
  73.       f := if fn == "-" then &input else {
  74.      fn := \suffix(fn)[1] || ".icn"
  75.      open(fn) | stop("Can't open input file \"",fn,"\"")
  76.      }
  77.       #
  78.       #  Loop to process lines of file (in string scanning mode).
  79.       #
  80.       while line := read(f) do line ? {
  81.      if tab(many(' \t')) | "" & ="procedure" & tab(many(' \t')) then {
  82.         t := open("undeclared_tmp.icn","w") | stop("Can't open work file")
  83.         write(t,line)
  84.         while line := read(f) do line ? {
  85.            write(t,line)
  86.            if tab(many(' \t')) | "" & ="end" & many(' \t') | pos(0) then
  87.              break
  88.            }
  89.         close(t)
  90.         #
  91.             #  Now we have an isolated Icon procedure -- invoke icont to
  92.         #  determine its undeclared variables.
  93.         #
  94.         p := open("icont -s -u -o /dev/null 2>&1 undeclared_tmp.icn","p") |
  95.           stop("popen failed")
  96.         names := []
  97.         while line := read(p) do line ?
  98.           if find("undeclared identifier") then
  99.             tab(find("\"") + 1) &
  100.             put(names,member(undeclared,tab(find("\""))))
  101.         close(p)
  102.         #
  103.         #  Output the declaration.
  104.         #
  105.         pn := "\"" || tab(upto(' \t(')) || "\"" ||
  106.           if *arg > 1 then " (" || fn || ")" else ""
  107.         if *names = 0 then write("# ",pn," is OK")
  108.         else {
  109.            write("# Local declarations for procedure ",pn)
  110.            sep := "   local "
  111.            every writes(sep,!sort(names)) do sep := ", "
  112.            write()
  113.            }
  114.         }
  115.      }
  116.       #
  117.       #  Close this input file.
  118.       #
  119.       close(f)
  120.       }
  121.    remove("undeclared_tmp.icn")
  122. end
  123.  
  124.  
  125.