2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,27 +1,29 @@
/*REXX program evaluates a Reverse Polish notation (RPN) expression.*/
parse arg x; if x='' then x = '3 4 2 * 1 5 - 2 3 ^ ^ / +'; ox=x
showSteps=1 /*set to 0 (zero) if working steps not wanted.*/
x=space(x); tokens=words(x)
do i=1 for tokens; @.i=word(x,i); end /*i*/ /*assign input tokens*/
L=max(20,length(x)) /*use 20 for the min show width. */
numeric digits L /*ensure enough digits for answer*/
say center('operand',L,'') center('stack',L*2,''); e='***error!***'
op='- + / * ^'; add2s='add tostack'; z=; stack=
do #=1 for tokens; ?=@.#; ??=? /*process each token from @. list*/
w=words(stack) /*stack count (# entries).*/
if datatype(?,'N') then do; stack=stack ?; call show add2s; iterate; end
if ?=='^' then ??="**" /*REXXify ^ ──► ** (make legal)*/
interpret 'y=' word(stack,w-1) ?? word(stack,w) /*compute.*/
if datatype(y,'W') then y=y/1 /*normalize the number with ÷ */
_=subword(stack,1,w-2); stack=_ y /*rebuild the stack with answer. */
call show ?
end /*#*/
z=space(z stack) /*append any residual entries. */
say; say ' RPN input:' ox; say ' answer' z /*show input & ans.*/
parse source upper . y . /*invoked via C.L. or REXX pgm?*/
if y=='COMMAND' | \datatype(z,'W') then exit /*stick a fork in it, done.*/
else return z /*RESULT ──► invoker.*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: if showSteps then say center(arg(1),L) left(space(stack),L); return
/*REXX program evaluates a ═════ Reverse Polish notation (RPN) ═════ expression. */
parse arg x /*obtain optional arguments from the CL*/
if x='' then x= "3 4 2 * 1 5 - 2 3 ^ ^ / +" /*Not specified? Then use the default.*/
tokens=words(x) /*save the number of tokens " ". */
showSteps=1 /*set to 0 if working steps not wanted.*/
ox=x /*save the original value of X. */
do i=1 for tokens; @.i=word(x,i) /*assign the input tokens to an array. */
end /*i*/
x=space(x) /*remove any superfluous blanks in X. */
L=max(20, length(x)) /*use 20 for the minimum display width.*/
numeric digits L /*ensure enough decimal digits for ans.*/
say center('operand', L, "") center('stack', L+L, "") /*display title*/
$= /*nullify the stack (completely empty).*/
do k=1 for tokens; ?=@.k; ??=? /*process each token from the @. list.*/
#=words($) /*stack the count (the number entries).*/
if datatype(?,'N') then do; $=$ ?; call show "add to───►stack"; iterate; end
if ?=='^' then ??= "**" /*REXXify ^ ───► ** (make legal).*/
interpret 'y='word($,#-1) ?? word($,#) /*compute via the famous REXX INTERPRET*/
if datatype(y,'N') then y=y/1 /*normalize the number with ÷ by unity.*/
$=subword($, 1, #-2) y /*rebuild the stack with the answer. */
call show ? /*display steps (tracing into), maybe.*/
end /*k*/
say /*display a blank line, better perusing*/
say ' RPN input:' ox; say " answer──►"$ /*display original input; display ans.*/
parse source upper . y . /*invoked via C.L. or via a REXX pgm?*/
if y=='COMMAND' | \datatype($,"W") then exit /*stick a fork in it, we're all done. */
else exit $ /*return the answer ───► the invoker.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: if showSteps then say center(arg(1), L) left(space($), L); return

View file

@ -1,42 +1,43 @@
/*REXX program evaluates a Reverse Polish notation (RPN) expression.*/
parse arg x; if x='' then x = '3 4 2 * 1 5 - 2 3 ^ ^ / +'; ox=x
showSteps=1 /*set to 0 (zero) if working steps not wanted.*/
x=space(x); tokens=words(x) /*elide extra blanks;count tokens*/
do i=1 for tokens; @.i=word(x,i); end /*i*/ /*assign input tokens*/
L=max(20,length(x)) /*use 20 for the min show width. */
numeric digits L /*ensure enough digits for answer*/
say center('operand',L,'') center('stack',L*2,''); e='***error!***'
add2s='add tostack'; z=; stack=
dop='/ // % ÷'; bop='& | &&' /*division ops; binary operands*/
aop='- + * ^ **' dop bop; lop=aop '||' /*arithmetic ops; legal operands*/
do #=1 for tokens; ?=@.#; ??=? /*process each token from @. list*/
w=words(stack); b=word(stack,max(1,w)) /*stack count; last entry.*/
a=word(stack,max(1,w-1)) /*stack's "first" operand.*/
division =wordpos(?,dop)\==0 /*flag: doing a division.*/
arith =wordpos(?,aop)\==0 /*flag: doing arithmetic.*/
bitOp =wordpos(?,bop)\==0 /*flag: doing binary math*/
if datatype(?,'N') then do; stack=stack ?; call show add2s; iterate; end
if wordpos(?,lop)==0 then do; z=e 'illegal operator:' ?; leave; end
if w<2 then do; z=e 'illegal RPN expression.'; leave; end
if ?=='^' then ??="**" /*REXXify ^ ──► ** (make legal)*/
if ?=='÷' then ??="/" /*REXXify ÷ ──► / (make legal)*/
if division & b=0 then do; z=e 'division by zero: ' b; leave; end
if bitOp & \isBit(a) then do; z=e "token isn't logical: " a; leave; end
if bitOp & \isBit(b) then do; z=e "token isn't logical: " b; leave; end
interpret 'y=' a ?? b /*compute with two stack operands*/
if datatype(y,'W') then y=y/1 /*normalize number with ÷ by 1.*/
_=subword(stack,1,w-2); stack=_ y /*rebuild the stack with answer. */
call show ?
end /*#*/
if word(z,1)==e then stack= /*handle special case of errors. */
z=space(z stack) /*append any residual entries. */
say; say ' RPN input:' ox; say ' answer' z /*show input & ans.*/
parse source upper . how . /*invoked via C.L. or REXX pgm?*/
if how=='COMMAND' | ,
\datatype(z,'W') then exit /*stick a fork in it, we're done.*/
return z /*return Z ──► invoker (RESULT).*/
/*──────────────────────────────────subroutines─────────────────────────*/
isBit: return arg(1)==0 | arg(1)==1 /*returns 1 if arg1 is bin bit.*/
show: if showSteps then say center(arg(1),L) left(space(stack),L); return
/*REXX program evaluates a ═════ Reverse Polish notation (RPN) ═════ expression. */
parse arg x /*obtain optional arguments from the CL*/
if x='' then x= "3 4 2 * 1 5 - 2 3 ^ ^ / +" /*Not specified? Then use the default.*/
tokens=words(x) /*save the number of tokens " ". */
showSteps=1 /*set to 0 if working steps not wanted.*/
ox=x /*save the original value of X. */
do i=1 for tokens; @.i=word(x,i) /*assign the input tokens to an array. */
end /*i*/
x=space(x) /*remove any superfluous blanks in X. */
L=max(20, length(x)) /*use 20 for the minimum display width.*/
numeric digits L /*ensure enough decimal digits for ans.*/
say center('operand', L, "") center('stack', L+L, "") /*display title*/
Dop= '/ // % ÷'; Bop='& | &&' /*division operators; binary operands.*/
Aop= '- + * ^ **' Dop Bop; Lop=Aop "||" /*arithmetic operators; legal operands.*/
$= /*nullify the stack (completely empty).*/
do k=1 for tokens; ?=@.k; ??=? /*process each token from the @. list.*/
#=words($); b=word($, max(1, #) ) /*the stack count; the last entry. */
a=word($, max(1, #-1) ) /*stack's "first" operand. */
division =wordpos(?, Dop)\==0 /*flag: doing a some kind of division.*/
arith =wordpos(?, Aop)\==0 /*flag: doing arithmetic. */
bitOp =wordpos(?, Bop)\==0 /*flag: doing some kind of binary oper*/
if datatype(?, 'N') then do; $=$ ?; call show "add to───►stack"; iterate; end
if wordpos(?, Lop)==0 then do; $=e 'illegal operator:' ?; leave; end
if w<2 then do; $=e 'illegal RPN expression.'; leave; end
if ?=='^' then ??= "**" /*REXXify ^ ──► ** (make it legal). */
if ?=='÷' then ??= "/" /*REXXify ÷ ──► / (make it legal). */
if division & b=0 then do; $=e 'division by zero.' ; leave; end
if bitOp & \isBit(a) then do; $=e "token isn't logical: " a; leave; end
if bitOp & \isBit(b) then do; $=e "token isn't logical: " b; leave; end
interpret 'y=' a ?? b /*compute with two stack operands*/
if datatype(y, 'W') then y=y/1 /*normalize the number with ÷ by unity.*/
_=subword($, 1, #-2); $=_ y /*rebuild the stack with the answer. */
call show ? /*display (possibly) a working step. */
end /*k*/
say /*display a blank line, better perusing*/
if word($,1)==e then $= /*handle the special case of errors. */
say ' RPN input:' ox; say " answer───►"$ /*display original input; display ans.*/
parse source upper . y . /*invoked via C.L. or via a REXX pgm?*/
if y=='COMMAND' | \datatype($,"W") then exit /*stick a fork in it, we're all done. */
else exit $ /*return the answer ───► the invoker.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isBit: return arg(1)==0 | arg(1)==1 /*returns 1 if arg1 is a binary bit*/
show: if showSteps then say center(arg(1), L) left(space($), L); return