Amethyst/Brain/Infobot/Module/Math.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 149 CC 14 SCAR 48.6
CC
14
Cov
0%
process
210.0
new
2.0
BEGIN
2.0
CRAP 128.6
1 package Amethyst::Brain::Infobot::Module::Math;
2  
3 use strict;
4 use vars qw(@ISA %BASES %DIGITS @DIGITS);
5 use Math::Trig;
6 use Math::BaseCalc;
7 use Amethyst::Message;
8 use Amethyst::Store;
9 use Amethyst::Brain::Infobot;
10 use Amethyst::Brain::Infobot::Module;
11  
12 @ISA = qw(Amethyst::Brain::Infobot::Module);
13 %BASES = (
14 binary => 2,
15 ternary => 3,
16 octal => 8,
17 decimal => 10,
18 hex => 16,
19 hexadecimal => 16,
20 );
21 %DIGITS = (
22 first => 1,
23 second => 2,
24 third => 3,
25 fourth => 4,
26 fifth => 5,
27 sixth => 6,
28 seventh => 7,
29 eighth => 8,
30 ninth => 9,
31 tenth => 10,
32 one => 1,
33 two => 2,
34 three => 3,
35 four => 4,
36 five => 5,
37 six => 6,
38 seven => 7,
39 eight => 8,
40 nine => 9,
41 ten => 10,
42 );
43 @DIGITS = ( 0 .. 9, 'A' .. 'Z' );
44  
45 sub new {
46 my $class = shift;
47 my $self = $class->SUPER::new(
48 Name => 'Math',
49 Usage => 'Evaluates mathematical expressions',
50 Description => "Math handler",
51 @_
52 );
53  
54 return bless $self, $class;
55 }
56  
57 sub process {
58 my ($self, $message) = @_;
59  
60 my $content = $message->content;
61 my $base = 10;
62  
63 $content =~ s/^what\s+is\s+//i;
64 $content =~ s/[.?!]$//g;
65  
66 my $basename = undef;
67 if ($content =~ s/\s+in\s+(.*)$//) {
68 $basename = $1;
69  
70 # print STDERR "Parsing base $basename\n";
71  
72 if ($BASES{$basename}) {
73 $base = $BASES{$basename};
74 }
75 elsif ($basename =~ /^base\s+(\d+)$/i) {
76 $base = $1;
77 }
78 else {
79 $base = 0;
80 }
81  
82 # print STDERR "Identified base $base\n";
83 }
84  
85 foreach (keys %DIGITS) {
86 $content =~ s/\b$_\b/ $DIGITS{$_} /g;
87 }
88  
89 # Conver to decimal
90 $content =~ s/\b0x([a-fA-F0-9]*)/ hex($1) /ge;
91 # $content =~ s/\bh([a-fA-F0-9]*)/ hex($1) /ge;
92 # $content =~ s/\bb([a-fA-F0-9]*)/ unpack("L", pack("B*", $1)) /ge;
93 $content =~ s/\bpi\b/ pi /ge;
94  
95 $content =~ s/^\s*//g;
96 $content =~ s/\s*$//g;
97 $content =~ s/\s+/ /g;
98  
99 # print STDERR "Math: Premunge: '$content'\n";
100  
101 # Perform usual infobot substitutions
102 $content =~ s/ to the / ** /g;
103 $content =~ s/\btimes\b/\*/g;
104 $content =~ s/\bdiv(ided by)? /\/ /g;
105 $content =~ s/\bover /\/ /g;
106 $content =~ s/\bsquared/\*\*2 /g;
107 $content =~ s/\bcubed/\*\*3 /g;
108 # $content =~ s/\bto\s+(the\s+)?(\d+)(r?st|nd|rd|th)?( power)?/\*\*$1 /ig;
109 $content =~ s/\bpercent of/*0.01*/ig;
110 $content =~ s/\bpercent/*0.01/ig;
111 $content =~ s/\% of\b/*0.01*/g;
112 $content =~ s/\%/*0.01/g;
113 $content =~ s/\bsquare root of (\d+)/sqrt($1)/ige;
114 $content =~ s/\bsqrt\s*(\d+)/sqrt($1)/ige;
115 # $content =~ s/\bcubed? root of (\d+)/$1 **(1.0\/3.0) /ig;
116 $content =~ s/ of / * /;
117 $content =~ s/(bit(-| )?)?xor(\'?e?d(\s+with))?/\^/g;
118 $content =~ s/(bit(-| )?)?or(\'?e?d(\s+with))?/\|/g;
119 $content =~ s/bit(-| )?and(\'?e?d(\s+with))?/\& /g;
120 $content =~ s/(plus|and)/+/ig;
121  
122 # print STDERR "Math: Postmunge: '$content'\n";
123  
124 $content =~ s/^\s*//g;
125 $content =~ s/\s*$//g;
126 $content =~ s/\s+/ /g;
127  
128 # print STDERR "Math: Final stage: '$content'\n";
129  
130 # Now for the throw outs:
131 return undef if $content !~ /\S/; # Empty
132 return undef if # Not an exp
133 $content !~ /^[-+\/\*\d*\.\s()^\|\&]+$/;
134 return undef if $content !~ /\d/; # Boring
135 return undef if # Trivial
136 (($content =~ /^\(?\d+\.?\d*\)?$/) && ($base == 10));
137 return undef if $content =~ /&\s*[^\d]/; # Subroutine call
138 return undef if $content =~ /&\s*\d+\s*\(/; # Subroutine call
139  
140 if ($base == 0) {
141 my $reply = $self->reply_to($message, "I don't think " .
142 "$basename is a valid base for " .
143 "computation.");
144 $reply->send;
145 return 1;
146 }
147  
148 if (($base > 36) || ($base < 2)) {
149 my $reply = $self->reply_to($message, "Are you trying to " .
150 "mess with my head by using base $base?");
151 $reply->send;
152 return 1;
153 }
154  
155 my $result;
156 eval qq{ \$result = $content; };
157 if ($@) {
158 my $err = $@;
159 chomp($err);
160 my $reply = $self->reply_to($message, "Math error: $err" .
161 " (probably your fault)");
162 $reply->send;
163 return 1;
164 }
165  
166 if ($base != 10) {
167 my $calc = new Math::BaseCalc(
168 digits => [ @DIGITS[0..($base - 1)] ],
169 );
170 $result = $calc->to_base($result);
171 }
172  
173 my $reply = $self->reply_to($message, "Math: $result");
174 $reply->send;
175  
176 return 1;
177 }
178  
179 1;