This scriptlet allows channel members to use a trigger to perform a calculation on a formula. For the most part, it checks for valid input and fixes the equation so that the $calc() will not flintch when trying to calculate the requested input.
The standard */+-^() math functions are the only functions that work with this calculator.
;========================================================================
; a channel calculator, by Tsueg
;========================================================================
; List of commands:
; /calc on --> turns on the calculator
; /calc off --> turns off the calculator
; /calc <equation> --> performs a private calculation when YOU enter
; an equation into an IRC window
; ~calc <equation> --> performs a calculation when SOMEONE ELSE enters
; the '~calc' trigger in the channel
;========================================================================
;========================================================================
; Input Catching
;------------------------------------
#Calculator on
on *:TEXT:~calc &:#: { msg $chan $nick $+ $chr(44) $$2 = $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
}
;========================================================================