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

  1. ############################################################################
  2. #
  3. #    File:     calc.icn
  4. #
  5. #    Subject:  Program to simulate desk calculator
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     January 3, 1993
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This is a simple Polish "desk calculator".  It accepts as values Icon
  18. #  integers, reals, csets, and strings (as they would appear in an Icon
  19. #  program) as well as an empty line for the null value.
  20. #
  21. #  Other lines of input are interpreted as operations. These may be Icon
  22. #  operators, functions, or the commands listed below.
  23. #
  24. #  In the case of operator symbols, such as +, that correspond to both unary
  25. #  and binary operations, the binary one is used.  Thus, the unary operation
  26. #  is not available.
  27. #
  28. #  In case of Icon functions like write() that take an arbitrary number of
  29. #  arguments, one argument is used.
  30. #
  31. #  The commands are:
  32. #
  33. #    clear    remove all values from the calculator's stack
  34. #    dump    write out the contents of the stack
  35. #    quit    exit from the calculator
  36. #
  37. #  Example: the input lines
  38. #
  39. #    "abc"
  40. #    3
  41. #    repl
  42. #    write
  43. #
  44. #  writes abcabcabc and leaves this as the top value on the stack.
  45. #
  46. #  Failure and most errors are detected, but in these cases, arguments are
  47. #  consumed and not restored to the stack.
  48. #
  49. ############################################################################
  50. #
  51. #  Links: ivalue, usage
  52. #
  53. ############################################################################
  54.  
  55. invocable all
  56.  
  57. link ivalue, usage
  58.  
  59. global stack
  60.  
  61. procedure main()
  62.    local line
  63.  
  64.    stack := []
  65.  
  66.    while line := read() do
  67.       (operation | value | command)(line) |
  68.          Error("erroneous input ",  image(line))
  69.  
  70. end
  71.  
  72. procedure command(line)
  73.  
  74.    case line of {
  75.       "clear":    stack := []
  76.       "dump":    every write(image(!stack))
  77.       "quit":     exit()
  78.       default:    fail
  79.       }      
  80.  
  81.    return
  82.  
  83. end
  84.  
  85. procedure operation(line)
  86.    local p, n, arglist
  87.  
  88.    if p := proc(line, 2 | 1 | 3) then {    # function or operation?
  89.       n := abs(args(p))
  90.       arglist := stack[-n : *stack + 1] | {
  91.          Error("too few arguments")
  92.          fail
  93.          }
  94.       stack := stack[1 : -n]
  95.       &error := 1            # anticipate possible error
  96.       put(stack, p ! arglist) | {    # invoke
  97.          if &error = 0 then
  98.             Error("error ", &errornumber, " evaluating ", image(line))
  99.          else
  100.             Error("failure evaluating ", image(line))
  101.          stack |||:= arglist        # restore unused arguments
  102.          }
  103.       &error := 0
  104.       return
  105.       }
  106.  
  107.    else fail
  108.  
  109. end
  110.  
  111. procedure value(line)
  112.  
  113.    put(stack,ivalue(line)) | fail
  114.  
  115.    return
  116.  
  117. end
  118.