;======================================================================== ; a channel calculator, by Tsueg ;======================================================================== ; List of commands: ; /calc on --> turns on the calculator ; /calc off --> turns off the calculator ; /calc --> performs a private calculation when YOU enter ; an equation into an IRC window ; ~calc --> performs a calculation when SOMEONE ELSE enters ; the '~calc' trigger in the channel ;======================================================================== ;======================================================================== ; Input Catching ;------------------------------------ #Calculator on on *:TEXT:?calc &:#: { msg $chan 7 $+ Calculator $+ $chr(44) $$2 = 12 $+ $fnCalc($$2) } #calculator end ;------------------------------------ alias calc { if ($1) { if ($1 == on) { .enable #calculator | echo -a Calculator has been ENABLED. } elseif ($1 == off) { .disable #calculator | echo -a Calculator has been DISABLED. } else { echo -a $me $+ $chr(44) $1 = $fnCalc($1) } } else { echo -a $me $+ $chr(44) error $+ $chr(44) no equation entered. } } ;======================================================================== ; Calculator code alias -l fnCalc { ;---------- Variable Deffinitions ---------- var %txtFormula = $strip($1) var %txtOutput = $null var %txtRegex.TestInput = /([^0-9\.\+\-\*\/\^\(\)])/i var %txtRegex.TestOne = /(\.\.)|(\*\*)|(\/\/)|(\.\*)|(\.\/)|(\*\/)|(\/\*)/i var %txtRegex.TestTwo = /(\.\()|(\.\))|(\(\))|(\(\*)|(\*\))|(\(\/)|(\/\))/i var %txtRegex.TestThree = /(\+\*)|(-\*)|(\+\/)|(-\/)|(\+\+\+)|(\+\+-)|(\+-\+)|(-\+\+)|(\+\--)|(-\+-)|(--\+)|(---)/i var %txtRegex.TestFour = /([0-9][\(]).*[\)]/i var %txtRegex.TestFive = /[\(].*([\)][0-9])/i var %txtRegex.TestSix = /[\(].*([\)][\(]).*[\)]/i var %txtRegex.TestSeven = /(--)/i var %txtRegex.TestEight = /(\+\+)/i var %txtRegex.TestNine = /((\d)+\.(\d)+\.(\d)+)/i var %txtRegex.TestTen = /(\^\^)|(-\^)|(\+\^)|(\/\^)|(\*/^)/i ;---------- test for valid input: 0123456789.+-*/^() ---------- if ($regex(%txtFormula, %txtRegex.TestInput)) { %txtOutput = Error $+ $chr(44) bad input: $regml(1) } ;---------- test for bad bracket counts ---------- elseif ($count(%txtFormula, $chr(40)) != $count(%txtFormula, $chr(41))) { %txtOutput = Error $+ $chr(44) bracket mismatch. } ;---------- test for bad operator use: .*/ ---------- elseif ($regex(%txtFormula, %txtRegex.TestOne)) { %txtOutput = Error $+ $chr(44) bad equation: $regml(1) } ;---------- test for more bad operator use: ^ ---------- elseif ($regex(%txtFormula, %txtRegex.TestTen)) { %txtOutput = Error $+ $chr(44) bad equation: $regml(1) } ;---------- test for more bad operator use: () ---------- elseif ($regex(%txtFormula, %txtRegex.TestTwo)) { %txtOutput = Error $+ $chr(44) bad equation: $regml(1) } ;---------- test for more bad operator use: +- ---------- elseif ($regex(%txtFormula, %txtRegex.TestThree)) { %txtOutput = Error $+ $chr(44) bad equation: $regml(1) } ;---------- test for bad decimal use: #.#.# ---------- elseif ($regex(%txtFormula, %txtRegex.TestNine)) { %txtOutput = Error $+ $chr(44) bad equation: $regml(1) } ;---------- test for more bracket mismatches: )( ---------- else { var %nbrOpened = 0 var %nbrClosed = 0 var %nbrLength = $len(%txtFormula) var %LoopCount = 1 while (%LoopCount <= %nbrLength && %txtOutput == $null) { if ($mid(%txtFormula, %loopCount, 1) == $chr(40)) { .inc %nbrOpened } elseif ($mid(%txtFormula, %loopCount, 1) == $chr(41)) { .inc %nbrClosed } if (%nbrClosed > %nbrOpened) { %txtOutput = Error $+ $chr(44) bracket mismatch. } .inc %LoopCount } } ;---------- calculate and output the result ---------- if (%txtOutput == $null) { ;---------- check for #( anomalies ---------- while ($regex(%txtFormula, %txtRegex.TestFour)) { %txtFormula = $replace(%txtFormula, $regml(1), $left($regml(1), 1) $+ $chr(42) $+ $chr(40)) } ;---------- check for )# anomalies ---------- while ($regex(%txtFormula, %txtRegex.TestFive)) { %txtFormula = $replace(%txtFormula, $regml(1), $chr(41) $+ $chr(42) $+ $right($regml(1), 1)) } ;---------- check for ). anomalies ---------- while ($chr(41) $+ $chr(46) isin %txtFormula) { %txtFormula = $replace(%txtFormula, $chr(41) $+ $chr(46), $chr(41) $+ $chr(42) $+ $chr(46)) } ;---------- check for )( anomalies ---------- while ($regex(%txtFormula, %txtRegex.TestSix)) { %txtFormula = $replace(%txtFormula, $regml(1), $chr(41) $+ $chr(42) $+ $chr(40)) } ;---------- check for -- anomalies ---------- while ($regex(%txtFormula, %txtRegex.TestSeven)) { %txtFormula = $replace(%txtFormula, $regml(1), $chr(43)) } ;---------- check for ++ anomalies ---------- while ($regex(%txtFormula, %txtRegex.TestEight)) { %txtFormula = $replace(%txtFormula, $regml(1), $chr(43)) } ;---------- echo output for debug ---------- ; echo -a --> in the calc function, using the equation: %txtFormula ;---------- calculate the equation ---------- %txtOutput = $calc(%txtFormula) } ;---------- return the result (or error message) ---------- return %txtOutput } ;========================================================================