-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcalc.tcl
More file actions
31 lines (25 loc) · 701 Bytes
/
calc.tcl
File metadata and controls
31 lines (25 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# created by fedex
bind pub - !calc safe_calc
bind pub - .calc safe_calc
setudef flag calc
proc is_op {str} {
return [expr [lsearch {{ } . + - * / ( ) %} $str] != -1]
}
proc safe_calc {nick uhost hand chan str} {
if {![channel get $chan calc]} { return }
foreach char [split $str {}] {
if {![is_op $char] && ![string is integer $char]} {
putserv "PRIVMSG $chan :$nick: Invalid expression for calc."
return
}
}
# make all values floating point
set str [regsub -all -- {((?:\d+)?\.?\d+)} $str {[expr {\1*1.0}]}]
set str [subst $str]
if {[catch {expr $str} out]} {
putserv "PRIVMSG $chan :$nick: Invalid equation."
return
} else {
putserv "PRIVMSG $chan :$str = $out"
}
}