Re: Overloading in ooRexx
- From: Michael Lueck <NmlueckO@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 02 Oct 2005 20:03:12 -0400
Hi Rony-
Thanks for the pointer, I will check those out as well. For now I started with Rick's suggested class in this group from some time back where I taked about creating a string-like class which would be mutable. From there I added the > and >= evaluation for now. For a bit I got bit that even though an "=" method is defined in the class, that does not allow for the line of code "a=1" to call the "=" method of the class "a" is an instance of and pass it "1" as arg.... rather it simply dereferences the instance of my custom class "a" was refering to and "a" becomes a simply object with "1" as its value. Anyway... I guess there are something I WILL have to do via the OO calling syntax, that's OK.
So, I have overloaded those couple of events like I said... working on getting my mind around the simplicity of Rick's suggested class, and trying to figure out "how it understands" to do so much with so little code. As well, I run AGAIN into the req that ooRexx support a "global trace" which would show how the entire flow through a class happens, even when it tries to execute methods which have not been coded thus ooRexx refers back to the parent class... like the string method for example. I'll let Rick continue with his major work he is doing to ooRexx, and then seeing that version of the code see if there is some way to stick in a "global trace" which maybe uses a new letter for the trace instruction or something to let ooRexx know this new kind of trace is being requested.
Anyway, here is what I have for now, and since I know it CAN work, I will continue as I have brain CPU cycles to devote to making this thing really slick. For now it is a test class to represent file version strings. Watch for line wraps, but I do have Mozilla set pretty high so it might send OK but maybe just receive bad if the news reader chops long lines in the data stream.
/* Testing overloading comparison operators */
a=.customtype~new b=.customtype~new
a[]='22.34.56' b[]='12.34.56'
say 'a=' || a say 'b=' || b
if a=b then say 'yes!' else say 'no!'
if a>b then say 'yes!' else say 'no!'
exit
::class customtype public
::method "[]" class return self~new(arg(1))
::method init expose value use arg value
::method "[]=" expose value use arg value
::method "[]" expose value return value
::method unknown expose value forward to (value)
::method string expose value forward to (value)
::method "=" expose value say "here =" forward to (value)
::method "==" expose value forward to (value)
::method "\=" expose value forward to (value)
::method "\==" expose value forward to (value)
::method ">" expose value --say "here >" --forward to (value) use arg othervalue if self~CompareVersionStrings(value, othervalue)=1 then return 1 return 0
::method ">=" expose value say "here >=" forward to (value)
/************************************************************************************/ /* METHOD : CompareVersionStrings */ /* */ /* PARAMETERS : VersionString1 VersionString2 */ /* */ /* DESCRIPTION : Compares Win32 version strings to see which is the higher rev */ /* */ /* RETURNS : -1 on error, 0 on same, 1 on 1=newer, 2 on 2=newer */ /************************************************************************************/ ::method CompareVersionStrings parse upper arg VersionString1, VersionString2
/* Quick check for same strings */
if VersionString1=VersionString2 then do
return 0
end /* if *//* So much for an easy out - processing time! */
/* Swap out , for . in the version, if found */
if pos(',', VersionString1)\=0 then VersionString1=translate(VersionString1, '.', ',')
if pos(',', VersionString2)\=0 then VersionString2=translate(VersionString2, '.', ',') /* Compare each section */
V1remaining=VersionString1
V2remaining=VersionString2
/* Var to track our loop count */
LoopCount=0
/* Track when we have gone into the last portion of the string */
EOS1flag=0
EOS2flag=0
do forever
/* Update the loop count */
LoopCount=LoopCount+1
/* Clean up leading dots */
do while left(V1remaining, 1)='.'
V1remaining=substr(V1remaining, 2)
end /* do */
do while left(V2remaining, 1)='.'
V2remaining=substr(V2remaining, 2)
end /* do */ /* Take the next piece */
if pos('.', V1remaining)\=0 then do
parse var V1remaining V1piece'.'V1remaining
end
else do
EOS1flag=1
V1piece=V1remaining
end if pos('.', V2remaining)\=0 then do
parse var V2remaining V2piece'.'V2remaining
end
else do
EOS2flag=1
V2piece=V2remaining
end /* Quick see if they are the same pieces */
if V1piece=V2piece then do
/* If the pieces are the same AND we are at the end of the string, then there must have been a 0=00 somewhere, outtahere! */
if EOS1flag & EOS2flag then return 0
else iterate
end /* Make sure both are whole numbers, then compare, else have to step through looking for letters */
if datatype(V1piece, 'W')=1 & datatype(V2piece, 'W')=1 then do
/* Only pad the numbers on the right side of the first decimal */
if LoopCount>1 then do
/* Pad the pieces if they are not the same length */
if length(V1piece)>length(V2piece) then V2piece=left(V2piece, length(V1piece), '0')
if length(V1piece)<length(V2piece) then V1piece=left(V1piece, length(V2piece), '0')
end /* if */
/* Compare away! */
if V1piece > V2piece then return 1
if V1piece < V2piece then return 2
end /* if */ /* See if these strings are of different length, and if there is a common portion */
if length(V1piece)>length(V2piece) then do
if left(V1piece, length(V2piece))=V2piece then return 1
else V1piece=left(V1piece, length(V2piece))
end if length(V1piece)<length(V2piece) then do
if left(V2piece, length(V1piece))=V1piece then return 2
else V2piece=left(V2piece, length(V1piece))
end /* Rats, have to go one char at a time but at least they are now the same length */
do c=1 to length(V1piece)+1 /* Must have this +1 to force the loop to run once for one char compares */
V1char=substr(V1piece, c, 1)
V2char=substr(V2piece, c, 1) /* Quick see if these chars are the same */
if V1char=V2char then iterate c /* Make sure both are whole numbers, then compare */
if datatype(V1char, 'W')=1 & datatype(V2char, 'W')=1 then do
if V1char > V2char then return 1
if V1char < V2char then return 2
end /* if */ /* See if one is number / one is alpha / number wins over letter */
if datatype(V1char, 'W')=1 & datatype(V2char, 'U')=1 then return 1
if datatype(V1char, 'U')=1 & datatype(V2char, 'W')=1 then return 2 /* Both might be alpha, check that */
if datatype(V1char, 'U')=1 & datatype(V2char, 'U')=1 then do
if c2d(V1char) > c2d(V2char) then return 1
if c2d(V1char) < c2d(V2char) then return 2
end /* do */ /* There must be a bad char in the string - puke and return! */
return -1
end /* if */
endreturn -1
-- Michael Lueck Lueck Data Systems http://www.lueckdatasystems.com/
Remove the upper case letters NOSPAM to contact me directly. .
- References:
- Re: Overloading in ooRexx
- From: rony
- Re: Overloading in ooRexx
- Prev by Date: Re: Overloading in ooRexx
- Next by Date: Re: A review of a text/script editor that discusses REXX
- Previous by thread: Re: Overloading in ooRexx
- Next by thread: Re: A review of a text/script editor that discusses REXX
- Index(es):
Relevant Pages
|