mIRC Homepage

Looking for this regsub function

Posted By: QuickStep

Looking for this regsub function - 26/07/05 10:18 AM

Im looking for a regsub function that looks for a calculation identifier like $sin, $cos, $int etc. etc. and puts $calc inside the brackets, but only if there are no commas inside the brackets, few examples:
;Put $calc inside the functions:
$sin(2 + 3)  -->  $sin($calc(2 + 3))
$int(2) + 4  -->  $int($calc(2)) + 4

;If there are only brackets, it should stay this way:
(4 + 6) * 4  --> (4 + 6) * 6

;Functions with commas should stay the way they are:
$rand(2,6) * 6  -->  $rand(2,6) * 6

Any help, im not so good with regular expressions
Posted By: Sigh

Re: Looking for this regsub function - 26/07/05 11:12 AM

alias addcalc {
  var %x
  .echo -q $regsub($1,/(\$(?!calc\50)[a-z\d]+)\(((?:[^,()]*\([^,()]*\))*[^,()]*)\)/gi,\1(\$calc(\2)),%x)
  return %x

Supports parentheses within these functions as long as there are no mismatches, for example: $addcalc( $!sin(1+1) $!tan((2+3)*5) ). Won't do anything with $calc itself
Posted By: Kelder

Re: Looking for this regsub function - 26/07/05 11:39 AM

Here's a try:

alias addcalcs {
var %calc = $1-
var %re = /(\$(?:sin|tan|cos|int)\((?!\$calc))((?:[^,()]++|\([^)]++\))+)/g
var %sub = \1\$calc(\2)
while ($regsub(%calc,%re,%sub,%calc)) { }
if ($!isid) echo -a %calc
return %calc

I'll try to explain the regex itself (ignore whitespace below)

( \$ (?:sin|tan|cos|int) \( (?!\$calc) )
-> look for $ followed by sin or cos or tan or int followed by a ( followed by something else than $calc, and remember all that

( (?: [^,()]++ | \([^)]++\) )+ )
-> look for a sequence of characters that are not , or ( or )
-> or look for a sequence of ( followed by characters other than ) followed by a )
-> and do this multiple times, remember all that


\1 \$calc( \2 )
And replace it with the first remembered part followed by $calc( second remembed part )

The ) of the $sin() isn't matched, so it won't need to be replaced.

Posted By: harl

Re: Looking for this regsub function - 26/07/05 12:16 PM

because i'm not too familiar with regular expressions yet, i don't know of a perfect solution using regex's and $regsub.
the ideas in the post above look fine to me though.

so here's just a fairly stupid workaround to test i thought of.
especially stupid, because $eval hasn't any parameter like the regex ()+ so evaluating a 200 times is as good (or bad) as 5 or 5000 here, not knowing what's exactly in $1-, and i don't see another manual (non-regex) way to have the returned "$calc($1-)" evaluated.
k, anyways..

alias hCheckpoint { return $iif(($chr(44) isin $1-),$1-,$eval($+($chr(36),calc,$chr(40),$1-,$chr(41)),200)) }

alias testRS {
  echo -at -- 12testrs on --
  echo -at $hCheckpoint($1-)

  ;-- echo -at $eval($+($1,$chr(40),$hCheckpoint($2-),$chr(41)),200)
  ;-- line above adds for sth like "testRS $sin 2+3", but obviously doesn't check if there're functions like $sin (where $1) used
  ;--which wouldn't support the commas, in case any of them are in $2-

  echo -at -- 04testrs end --

use /testRS

i also don't know if something like that can be combined with a regex effectively without adding to it, then again getting too intricate.

Posted By: QuickStep

Re: Looking for this regsub function - 26/07/05 05:03 PM

Thanks for your replies its very much appreciated

First I went with Sigh's script, but unfortunately found a problem with nested functions, example:
$sin(2 + 3 * $cos(3 * 6))
;would return
$sin($calc(2 + 3 * $cos(3 * 6)))
;and not
$sin($calc(2 + 3 * $cos($calc(3 * 6))))

Then i went with Kelders snippet and everything seemed to work fine. So thanks again all of you and got it working now!
© 2022 mIRC Discussion Forums