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 / empg.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  120 lines

  1. ############################################################################
  2. #
  3. #    File:     empg.icn
  4. #
  5. #    Subject:  Program to make expression-evaluation programs
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 16, 1998
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program produces a program for evaluating Icon expressions.  The
  18. #  input to this program has three forms, depending on the first character
  19. #  of each line:
  20. #
  21. #    :    the remainder of the line is an expression to be evaluated
  22. #        only once
  23. #
  24. #    %    the remainder of the line is part of a declaration
  25. #
  26. #    #    the remainder of the line is a comment and is ignored
  27. #
  28. #  Anything else is an expression to be evaluated in a loop.
  29. #
  30. #  For example, the input
  31. #
  32. #  # Time record access
  33. #  %record complex(r, i)
  34. #  :z := complex(1.0, 3.5)
  35. #  z.r
  36. #
  37. #  produces a program to time z.r in a loop.
  38.  
  39. #  The following options are supported:
  40. #
  41. #    -l i    use i for the number of loop iterations, default 100000
  42. #    -d i    use i for the "delta" to adjust timings; otherwise it
  43. #          is computed when the expression-evaluation program
  44. #          is run
  45. #
  46. ############################################################################
  47. #
  48. #  Links:  options
  49. #
  50. ############################################################################
  51.  
  52. link options
  53.  
  54. global decls
  55.  
  56. procedure main(args)
  57.    local line, opts, limit, delcomp
  58.  
  59.    opts := options(args, "d+l+")
  60.  
  61.    write("link empgsup")
  62.    write("link options")
  63.    write("procedure main(args)")
  64.    write("   local opts")
  65.    write("   opts := options(args, \"d+l+\")")
  66.    write("   _Limit := ", \opts["l"] | " \\opts[\"l\"] | 100000")
  67.    write("   _Delta := ", \opts["d"] | " \\opts[\"d\"] | _Initialize(_Limit)")
  68.   
  69.    decls := []
  70.  
  71.    while line := read() do
  72.       line ? {
  73.          if =":" then evaluate(tab(0))
  74.          else if ="%" then declare(tab(0))
  75.          else if ="#" then next
  76.          else timeloop(tab(0))
  77.          }
  78.  
  79.    write("end")
  80.  
  81.    every write(!decls)
  82.  
  83. end
  84.  
  85. #  Save a declaration line.
  86.  
  87. procedure declare(line)
  88.  
  89.    put(decls, line)
  90.  
  91.    return
  92.  
  93. end
  94.  
  95. #  Produce code to just evaluate an expression.
  96.  
  97. procedure evaluate(expr)
  98.  
  99.    write("   ", expr)
  100.  
  101.    return
  102.  
  103. end
  104.  
  105. #  Produce code to evaluate an expression in a loop and time it.
  106.  
  107. procedure timeloop(expr)
  108.  
  109.    write("   write(", image(expr), ")")
  110.    write("   _Itime := &time")
  111.    write("   every 1 to _Limit do {")
  112.    write("      &null & (", expr, ")")
  113.    write("      }")
  114.    write("   write(real(&time - _Itime -_Delta) / _Limit, \" ms.\")")
  115.    write("   write()")
  116.  
  117.    return
  118.  
  119. end
  120.