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

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