home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmsmp.zip / RPN.E < prev    next >
Text File  |  1992-08-26  |  5KB  |  218 lines

  1. ; Written by Larry Salomon, Jr.
  2. ;
  3. ; Have at it ppl...Here is a reverse polish notation evaluator.
  4. ;
  5. ; It will accept the following operators:
  6. ;   +  plus  2 3 +  equals 5
  7. ;   -  minus  3 2 -  equals 1
  8. ;   *  times  3 2 *  equals 6
  9. ;   /  divide  5 2 /  equals 2.5
  10. ;   <  boolean less-than  5 2 <  equals 0
  11. ;   >  boolean greater-than  5 2 > equals 1
  12. ;   <= =<  boolean less-than-or-equal-to  3 4 <=  equals 1
  13. ;   >= =>  boolean greater-than-or-equal-to  4 4 >=  equals 1
  14. ;   <>  boolean not-equal-to  5 5 <>  equals 0
  15. ;   =  boolean equal-to  5 5 =  equals 1
  16. ;   << shl  shift left  1 3 <<  equals 8
  17. ;   >> shr  shift right  15 >> 3  equals 1
  18. ;   ^  power  2 3 ^  equals 8
  19. ;
  20. ;-------------------------------------------------------
  21. defc calc=
  22.    parse arg args
  23.  
  24.    if args='?' then
  25.       call do_help()
  26.       stop
  27.    endif
  28.  
  29.    s_new(stack)
  30.  
  31.    while args/=='' do
  32.       parse value args with val args
  33.  
  34.       if isnum(val) then
  35.          s_push(stack,val)
  36.       else
  37.          call do_op(stack,val)
  38.       endif
  39.    endwhile
  40.  
  41.    res=s_pop(stack)
  42.    sayerror 'The result is' res
  43.  
  44. defproc do_help()
  45.    sayerror 'Syntax:  CALC reverse_polish_expression'
  46.    sayerror 'Operators may be: + - * / < > <= >= = <> << >> int sqr'
  47.  
  48. defproc do_op(var stack,op)
  49.    if op='+' then
  50.       val1=s_pop(stack)
  51.       val2=s_pop(stack)
  52.  
  53.       call validate(2,val1,val2)
  54.       call s_push(stack,val2+val1)
  55.    elseif op='-' then
  56.       val1=s_pop(stack)
  57.       val2=s_pop(stack)
  58.  
  59.       call validate(2,val1,val2)
  60.       call s_push(stack,val2-val1)
  61.    elseif op='*' then
  62.       val1=s_pop(stack)
  63.       val2=s_pop(stack)
  64.  
  65.       call validate(2,val1,val2)
  66.       call s_push(stack,val2*val1)
  67.    elseif op='/' then
  68.       val1=s_pop(stack)
  69.       val2=s_pop(stack)
  70.  
  71.       call validate(2,val1,val2)
  72.       call s_push(stack,val2/val1)
  73.    elseif op='<' then
  74.       val1=s_pop(stack)
  75.       val2=s_pop(stack)
  76.  
  77.       call validate(2,val1,val2)
  78.       if val2<val1 then
  79.          call s_push(stack,1)
  80.       else
  81.          call s_push(stack,0)
  82.       endif
  83.    elseif op='>' then
  84.       val1=s_pop(stack)
  85.       val2=s_pop(stack)
  86.  
  87.       if val2>val1 then
  88.          call s_push(stack,1)
  89.       else
  90.          call s_push(stack,0)
  91.       endif
  92.    elseif op='=' then
  93.       val1=s_pop(stack)
  94.       val2=s_pop(stack)
  95.  
  96.       call validate(2,val1,val2)
  97.       if val2=val1 then
  98.          call s_push(stack,1)
  99.       else
  100.          call s_push(stack,0)
  101.       endif
  102.    elseif (op='<=') | (op='=<') then
  103.       val1=s_pop(stack)
  104.       val2=s_pop(stack)
  105.  
  106.       call validate(2,val1,val2)
  107.       if val2<=val1 then
  108.          call s_push(stack,1)
  109.       else
  110.          call s_push(stack,0)
  111.       endif
  112.    elseif (op='>=') | (op='=>') then
  113.       val1=s_pop(stack)
  114.       val2=s_pop(stack)
  115.  
  116.       call validate(2,val1,val2)
  117.       if val2>=val1 then
  118.          call s_push(stack,1)
  119.       else
  120.          call s_push(stack,0)
  121.       endif
  122.    elseif op='<>' then
  123.       val1=s_pop(stack)
  124.       val2=s_pop(stack)
  125.  
  126.       call validate(2,val1,val2)
  127.       if val2<>val1 then
  128.          call s_push(stack,1)
  129.       else
  130.          call s_push(stack,0)
  131.       endif
  132.    elseif op='^' then
  133.       val1=s_pop(stack)
  134.       val2=s_pop(stack)
  135.  
  136.       call validate(2,val1,val2)
  137.       call s_push(stack,power(val2,val1))
  138.    elseif (op='<<') | (op='shl') then
  139.       val1=s_pop(stack)
  140.       val2=s_pop(stack)
  141.  
  142.       call validate(2,val1,val2)
  143.       call s_push(stack,val2*power(2,val1))
  144.    elseif (op='>>') | (op='shr') then
  145.       val1=s_pop(stack)
  146.       val2=s_pop(stack)
  147.  
  148.       call validate(2,val1,val2)
  149.       call s_push(stack,trunc(val2/power(2,val1)))
  150.    elseif op='int' then
  151.       val1=s_pop(stack)
  152.  
  153.       call validate(1,val1)
  154.       call s_push(stack,trunc(val1))
  155.    elseif op='sqr' then
  156.       val1=s_pop(stack)
  157.  
  158.       call validate(1,val1)
  159.       call s_push(stack,val1*val1)
  160.    else
  161.       sayerror 'Invalid operator specified ("'op'").'
  162.       stop
  163.    endif
  164.    return
  165.  
  166. defproc validate(num)
  167.    for i = 1 to num
  168.       if arg(1+i)='' then
  169.          sayerror 'Missing operand.'
  170.          stop
  171.       endif
  172.    endfor
  173.  
  174. defproc power(num,exp)
  175.    if exp=0 then
  176.       return 1
  177.    else
  178.       res=num
  179.  
  180.       while exp>1 do
  181.          res=res*num
  182.          exp=exp-1
  183.       endwhile
  184.  
  185.       return res
  186.    endif
  187.  
  188. defproc trunc(val)
  189.    p=pos('.',val)
  190.  
  191.    if p=0 then
  192.       return val
  193.    elseif p=1 then
  194.       return 0
  195.    else
  196.       return substr(val,1,p-1)
  197.    endif
  198.  
  199. defproc s_new(var stack)
  200.    stack=''
  201.    return
  202.  
  203. defproc s_empty(stack)
  204.    if stack='' then
  205.       return 1
  206.    else
  207.       return 0
  208.    endif
  209.  
  210. defproc s_push(var stack,val)
  211.    stack=val stack
  212.    return
  213.  
  214. defproc s_pop(var stack)
  215.    parse value stack with val stack
  216.    return val
  217.  
  218.