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 / mprocs / loadfile.icn < prev    next >
Text File  |  2001-05-02  |  2KB  |  65 lines

  1. ############################################################################
  2. #
  3. #    File:     loadfile.icn
  4. #
  5. #    Subject:  Procedure to produce and load program on the fly
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     November 21, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  loadfile(exp, link, ...) produces and loads a program that generates
  18. #  the results of exp.  The trailing arguments name link
  19. #  files needed for the expression.  loadfile() returns a procedure
  20. #  that generates the results.
  21. #
  22. ############################################################################
  23. #
  24. #  Requires:  MT-Icon, system(), pipes, /tmp
  25. #
  26. ############################################################################
  27. #
  28. #  Links:  io
  29. #
  30. ############################################################################
  31.  
  32. link io
  33.  
  34. procedure loadfile(exp, links[])    #: produce and load program
  35.    local output, prog
  36.    static name
  37.  
  38.    output := tempfile("load", ".icn", "/tmp")
  39.  
  40.    image(output) ? {
  41.       ="file("
  42.       name := tab(find(".icn"))
  43.       }
  44.  
  45.    write(output, "invocable all")
  46.    every write(output, "link ", image(!links)) 
  47.    write(output, "procedure main(args)")
  48.    write(output, "   suspend ", exp)
  49.    write(output, "end")
  50.  
  51.    close(output)
  52.  
  53.    if system("mticont -o " || name || " -s " || name ||
  54.       " >/dev/null 2>/dev/null") ~= 0 then fail
  55.  
  56.    remove(name || ".icn")        # remove source code file
  57.  
  58.    #  Load the program
  59.  
  60.    prog := load(name) | stop("*** load failure in loadfile")
  61.  
  62.    return variable("main", prog)
  63.    
  64. end
  65.