home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROGS.LZH / INTERPP.ICN < prev    next >
Text File  |  1991-09-05  |  7KB  |  371 lines

  1. ############################################################################
  2. #
  3. #    Name:    interpp.icn
  4. #
  5. #    Title:    Interpret Icon programs
  6. #
  7. #    Author:    Jerry Nowlin
  8. #
  9. #    Date:    January 3, 1991
  10. #
  11. ############################################################################
  12. #  
  13. #    This program is kind of like an interactive version of BASIC in that Icon
  14. #  expressions are entered with line numbers and you can resequence them list
  15. #  them etc. and execute all the lines entered.  There is no editor built
  16. #  in.  You have to retype a line to change it.
  17. #
  18. #    Documentation is lacking but there is a "?" help command that lists all
  19. #  the other commands.
  20. #
  21. ############################################################################
  22. #
  23. #  See also:  interpe.icn
  24. #
  25. ############################################################################
  26.  
  27. global    WHITE,    # the white space cset
  28.     MFLAG,    # the modified flag
  29.     PRTBL    # the program table
  30.  
  31. procedure main(arg)
  32.  
  33. #    define the needed cset
  34.     WHITE := ' \t\n\f'
  35.  
  36. #    initialize the program table
  37.     PRTBL := table()
  38.  
  39. #    initialize the modified flag
  40.     MFLAG := 0
  41.  
  42. #    get all the input
  43.     writes("Icon> ")
  44.     while line := read() do {
  45.  
  46. #        scan the input line
  47.         line ? {
  48.  
  49. #            skip any initial white space
  50.             tab(many(WHITE))
  51.  
  52. #            check for program lines (they have line numbers)
  53.             if lno := tab(many(&digits)) & tab(many(WHITE)) then {
  54.  
  55. #                get the program line
  56.                 pline := tab(0)
  57.  
  58. #                store the line in the program table
  59.                 PRTBL[numeric(lno)] := pline
  60.  
  61. #                set the modified flag
  62.                 MFLAG +:= 1
  63.             }
  64.  
  65. #            read command
  66.             else if (tab(upto(WHITE)) | tab(0)) ==
  67.                 ("read" | "r") then {
  68.                 readprog()
  69.  
  70. #                clear the modified flag
  71.                 MFLAG := 0
  72.             }
  73.  
  74. #            write command
  75.             else if (tab(upto(WHITE)) | tab(0)) ==
  76.                 ("write" | "w") then {
  77.                 writeprog()
  78.  
  79. #                clear the modified flag
  80.                 MFLAG := 0
  81.             }
  82.  
  83. #            delete command
  84.             else if (tab(upto(WHITE)) | tab(0)) ==
  85.                 ("delete" | "d") then {
  86.                 delprog()
  87.  
  88. #                set the modified flag
  89.                 MFLAG +:= 1
  90.             }
  91.  
  92. #            sequence command
  93.             else if (tab(upto(WHITE)) | tab(0)) ==
  94.                 ("sequence" | "s") then {
  95.                 seqprog()
  96.             }
  97.  
  98. #            list command
  99.             else if (tab(upto(WHITE)) | tab(0)) ==
  100.                 ("list" | "l") then {
  101.                 listprog()
  102.             }
  103.  
  104. #            execute command
  105.             else if (tab(upto(WHITE)) | tab(0)) ==
  106.                 ("execute" | "e") then {
  107.                 execprog()
  108.             }
  109.  
  110. #            help command
  111.             else if (tab(upto(WHITE)) | tab(0)) ==
  112.                 ("help" | "h" | "?") then {
  113.                 helpprog()
  114.             }
  115.  
  116. #            quit command
  117.             else if (tab(upto(WHITE)) | tab(0)) ==
  118.                 ("quit" | "q") then {
  119.                 quitprog()
  120.             }
  121.  
  122. #            invalid syntax input
  123.             else {
  124.                 write("Syntax Error: ",line)
  125.                 helpprog()
  126.             }
  127.         }
  128.         writes("Icon> ")
  129.     }
  130.  
  131. end
  132.  
  133. procedure execprog()
  134.  
  135.     static    tmpfile
  136.  
  137.     initial tmpfile := "TMPFILE.icn"
  138.  
  139. #    get any runtime arguments
  140.     runargs := tab(0)
  141.  
  142. #    create the temporary Icon file
  143.     (out := open(tmpfile,"w")) |
  144.  
  145. #    or mention the problem and fail
  146.     (write("I can't open '",tmpfile,"' for writing") & fail)
  147.  
  148. #    sort the program table
  149.     prog := sort(PRTBL)
  150.  
  151. #    put the program in the file
  152.     every line := !prog do {
  153.         write(out,line[2])
  154.     }
  155.     close(out)
  156.  
  157. #    format the command to execute the program
  158.     command := "icont -s " || tmpfile || " -x " || runargs
  159.  
  160. #    add the command to remove the temporary file
  161.     command ||:= " ; rm -f " || tmpfile
  162.  
  163. #    execute the command
  164.     system(command)
  165.  
  166. end
  167.  
  168. procedure seqprog()
  169.  
  170. #    initialize the sequencing numbers
  171.     begno := incno := 10
  172.  
  173. #    skip any white space
  174.     tab(many(WHITE))
  175.  
  176. #    get an initial line number
  177.     begno := numeric(tab(many(&digits)))
  178.  
  179. #    skip any white space
  180.     tab(many(WHITE))
  181.  
  182. #    get a increment number
  183.     incno := numeric(tab(many(&digits)))
  184.  
  185. #    sort the program table
  186.     prog := sort(PRTBL)
  187.  
  188. #    reinitialize it
  189.     PRTBL := table()
  190.  
  191. #    sequence the program lines starting with begno by incno
  192.     lno := begno
  193.     every l := !prog do {
  194.         PRTBL[lno] := l[2]
  195.         lno +:= incno
  196.     }
  197.  
  198. end
  199.  
  200. procedure readprog()
  201.  
  202. #    get a possible command line file name
  203.     tab(many(WHITE))
  204.     readfile := tab(upto(WHITE) | 0)
  205.  
  206. #    if there was no file with the command get one
  207.     if /readfile | *readfile = 0 then {
  208.         writes("Read file name: ")
  209.         readfile := read()
  210.     }
  211.  
  212. #    make sure a modified file has been written
  213.     if MFLAG > 0 then {
  214.         writes("Write before reading over current program? ")
  215.         response := read()
  216.         if any('yY',response) then
  217.             writeprog()
  218.     }
  219.  
  220. #    initialize the program table
  221.     PRTBL := table()
  222.  
  223. #    read the program from the read file
  224.     in := open(readfile,"r")
  225.     lno := 10
  226.     every line := !in do {
  227.         PRTBL[lno] := line
  228.         lno +:= 10
  229.     }
  230.     close(in)
  231.  
  232. #    tell them what you did
  233.     write("Read '",readfile,"'...",*PRTBL," lines")
  234.  
  235. end
  236.  
  237. procedure writeprog()
  238.  
  239. #    get a possible command line file name
  240.     tab(many(WHITE))
  241.     writefile := tab(upto(WHITE) | 0)
  242.  
  243. #    if there was no file with the command get one
  244.     if /writefile | *writefile = 0 then {
  245.         writes("Write file name: ")
  246.         writefile := read()
  247.     }
  248.  
  249. #    sort the program table
  250.     prog := sort(PRTBL)
  251.  
  252. #    write the program to the write file
  253.     out := open(writefile,"w")
  254.     every l := !prog do {
  255.         write(out,l[2])
  256.     }
  257.     close(out)
  258.  
  259. #    tell them what you did
  260.     write("Write '",writefile,"'...",*PRTBL," lines")
  261.  
  262. end
  263.  
  264. procedure delprog()
  265.  
  266. #    initialize the line numbers
  267.     begno := 0
  268.     endno := 99999
  269.  
  270. #    skip any white space
  271.     tab(many(WHITE))
  272.  
  273. #    get an initial line number
  274.     begno := endno := numeric(tab(many(&digits)))
  275.  
  276. #    skip any white space
  277.     tab(many(WHITE))
  278.  
  279. #    get a final line number
  280.     endno := numeric(tab(many(&digits)))
  281.  
  282. #    sort the program table
  283.     prog := sort(PRTBL)
  284.  
  285. #    reinitialize it
  286.     PRTBL := table()
  287.  
  288. #    delete the program lines between the optional numbers
  289.     every l := !prog do {
  290.         lno := numeric(l[1])
  291.         if (lno < begno) | (lno > endno) then PRTBL[lno] := l[2]
  292.     }
  293.  
  294. end
  295.  
  296. procedure listprog()
  297.  
  298. #    initialize the line numbers
  299.     begno := 0
  300.     endno := 99999
  301.  
  302. #    skip any white space
  303.     tab(many(WHITE))
  304.  
  305. #    get an initial line number
  306.     begno := endno := numeric(tab(many(&digits)))
  307.  
  308. #    skip any white space
  309.     tab(many(WHITE))
  310.  
  311. #    get a final line number
  312.     endno := numeric(tab(many(&digits)))
  313.  
  314. #    sort the program table
  315.     prog := sort(PRTBL)
  316.  
  317. #    list the program lines between the optional numbers
  318.     every l := !prog do {
  319.         lno := numeric(l[1])
  320.         if (lno >= begno) & (lno <= endno) then
  321.             write(right(lno,5),": ",l[2])
  322.         if lno > endno then break
  323.     }
  324.  
  325. end
  326.  
  327. procedure helpprog()
  328.  
  329.     static helpmsg
  330.  
  331. #    define the help message
  332.     initial {
  333.         helpmsg := [
  334.         "<<< Icon Expression Syntax >>>",
  335.         "",
  336.         "lineno expression",
  337.         "",
  338.         "<<< Command Summary >>>",
  339.         " (1st character works)",
  340.         "",
  341.         "read [ file ]",
  342.         "write [ file ]",
  343.         "list [ begno [ endno ] ]",
  344.         "delete [ begno [ endno ] ]",
  345.         "sequence [ begno [ increment ] ]",
  346.         "execute [ args ]",
  347.         "quit",
  348.         "help"
  349.         ]
  350.     }
  351.  
  352. #    print it
  353.     every write(!helpmsg)
  354.  
  355. end
  356.  
  357. procedure quitprog()
  358.  
  359. #    make sure a modified file has been written
  360.     if MFLAG > 0 then {
  361.         writes("Write before quitting? ")
  362.         response := read()
  363.         if any('yY',response) then
  364.             writeprog()
  365.     }
  366.  
  367.     stop("Goodbye.")
  368.  
  369. end
  370.  
  371.