| 1 | package AI::Prolog::Parser::PreProcessor::Math; | ||||||||||||||||||||||||||||
| 2 | 14 | $REVISION = '$Id: Math.pm,v 1.3 2005/08/06 23:28:40 ovid Exp $'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 14 | $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 5 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Regexp::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 14 | my $var = qr/[[:upper:]][[:alnum:]_]*/; | |||||||||||||||||||||||||||
| 11 | 14 | my $num = $RE{num}{real}; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # ** must be before * | ||||||||||||||||||||||||||||
| 14 | 14 | my $op = qr{(?:\*\*|[-+*/%])}; | |||||||||||||||||||||||||||
| 15 | 14 | my $compare = qr/(?:(?:\\|=)?=|is|[<>]=?)/; | |||||||||||||||||||||||||||
| 16 | 14 | my $lparen = qr/\(/; | |||||||||||||||||||||||||||
| 17 | 14 | my $rparen = qr/\)/; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Having a word boundary prior to $num breaks the regex | ||||||||||||||||||||||||||||
| 20 | # when trying to match negative numbers | ||||||||||||||||||||||||||||
| 21 | 14 | my $simple_math_term = qr/(?!\.(?![0-9]))(?:$num\b|\b$var\b)/; | |||||||||||||||||||||||||||
| 22 | 14 | my $simple_rhs = qr/ | |||||||||||||||||||||||||||
| 23 | $simple_math_term | ||||||||||||||||||||||||||||
| 24 | (?: | ||||||||||||||||||||||||||||
| 25 | \s* | ||||||||||||||||||||||||||||
| 26 | $op | ||||||||||||||||||||||||||||
| 27 | \s* | ||||||||||||||||||||||||||||
| 28 | $simple_math_term | ||||||||||||||||||||||||||||
| 29 | )* | ||||||||||||||||||||||||||||
| 30 | /x; | ||||||||||||||||||||||||||||
| 31 | 14 | my $simple_group_term = qr/$lparen\s*$simple_rhs\s*$rparen/; | |||||||||||||||||||||||||||
| 32 | 14 | my $math_term = qr/(?:$simple_math_term|$simple_group_term)/; | |||||||||||||||||||||||||||
| 33 | 14 | my $complex_rhs = qr/ | |||||||||||||||||||||||||||
| 34 | $math_term | ||||||||||||||||||||||||||||
| 35 | (?: | ||||||||||||||||||||||||||||
| 36 | \s* | ||||||||||||||||||||||||||||
| 37 | $op | ||||||||||||||||||||||||||||
| 38 | \s* | ||||||||||||||||||||||||||||
| 39 | $math_term | ||||||||||||||||||||||||||||
| 40 | )* | ||||||||||||||||||||||||||||
| 41 | /x; | ||||||||||||||||||||||||||||
| 42 | 14 | my $complex_group_term = qr/$lparen\s*$complex_rhs\s*$rparen/; | |||||||||||||||||||||||||||
| 43 | 14 | my $final_math_term = qr/(?:$math_term|$complex_group_term)/; | |||||||||||||||||||||||||||
| 44 | 14 | my $rhs = qr/ | |||||||||||||||||||||||||||
| 45 | $final_math_term | ||||||||||||||||||||||||||||
| 46 | (?: | ||||||||||||||||||||||||||||
| 47 | \s* | ||||||||||||||||||||||||||||
| 48 | $op | ||||||||||||||||||||||||||||
| 49 | \s* | ||||||||||||||||||||||||||||
| 50 | $final_math_term | ||||||||||||||||||||||||||||
| 51 | )* | ||||||||||||||||||||||||||||
| 52 | /x; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 14 | my $expression = qr/ | |||||||||||||||||||||||||||
| 55 | ( | ||||||||||||||||||||||||||||
| 56 | ($simple_math_term) | ||||||||||||||||||||||||||||
| 57 | \s+ | ||||||||||||||||||||||||||||
| 58 | ($compare) | ||||||||||||||||||||||||||||
| 59 | \s+ | ||||||||||||||||||||||||||||
| 60 | ($rhs) | ||||||||||||||||||||||||||||
| 61 | ) | ||||||||||||||||||||||||||||
| 62 | (?=[,.]) | ||||||||||||||||||||||||||||
| 63 | /x; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 14 | my %convert = ( | |||||||||||||||||||||||||||
| 66 | qw{ | ||||||||||||||||||||||||||||
| 67 | is is | ||||||||||||||||||||||||||||
| 68 | = eq | ||||||||||||||||||||||||||||
| 69 | + plus | ||||||||||||||||||||||||||||
| 70 | / div | ||||||||||||||||||||||||||||
| 71 | - minus | ||||||||||||||||||||||||||||
| 72 | % mod | ||||||||||||||||||||||||||||
| 73 | * mult | ||||||||||||||||||||||||||||
| 74 | ** pow | ||||||||||||||||||||||||||||
| 75 | < lt | ||||||||||||||||||||||||||||
| 76 | <= le | ||||||||||||||||||||||||||||
| 77 | > gt | ||||||||||||||||||||||||||||
| 78 | >= ge | ||||||||||||||||||||||||||||
| 79 | == eq | ||||||||||||||||||||||||||||
| 80 | \= ne | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 94 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 94 | my ( $class, $prolog ) = @_; | |||||||||||||||||||||||||||
| 86 | 94 | ▶ | while ( $prolog =~ $expression ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 23 | my ( $old_expression, $lhs, $comp, $rhs ) = ( $1, $2, $3, $4 ); | |||||||||||||||||||||||||||
| 88 | 23 | my $new_rhs = $class->_parse( $class->_lex($rhs) ); | |||||||||||||||||||||||||||
| 89 | my $new_expression = sprintf | ||||||||||||||||||||||||||||
| 90 | 23 | "%s(%s, %s)" => $convert{$comp}, | |||||||||||||||||||||||||||
| 91 | $lhs, $new_rhs; | ||||||||||||||||||||||||||||
| 92 | 23 | $prolog =~ s/\Q$old_expression\E/$new_expression/g; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 94 | return $prolog; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 32 | ▶ | sub _lex { | ||||||||||||||||||||||||||
|
Subroutine
_lex: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 32 | my ( $class, $rhs ) = @_; | |||||||||||||||||||||||||||
| 99 | 32 | my $lexer = _lexer($rhs); | |||||||||||||||||||||||||||
| 100 | 32 | my @tokens; | |||||||||||||||||||||||||||
| 101 | 32 | ▶ | while ( my $token = $lexer->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 184 | push @tokens => $token; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 32 | return \@tokens; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 32 | ▶ | sub _lexer { | ||||||||||||||||||||||||||
|
Subroutine
_lexer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 32 | my $rhs = shift; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # the entire "$prev_op" thing is to allow the lexer to be aware of '7 + -3' | ||||||||||||||||||||||||||||
| 111 | # $op_ok is false on the first pass because it can never be first, but we | ||||||||||||||||||||||||||||
| 112 | # might have '-7 * (-2 + 3)' | ||||||||||||||||||||||||||||
| 113 | 32 | my $op_ok = 0; | |||||||||||||||||||||||||||
| 114 | 216 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 115 | LEXER: { | ||||||||||||||||||||||||||||
| 116 | 216 | ▶ | $op_ok = 0, return [ 'OP', $1 ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op_ok and $rhs =~ /\G ($op) /cgx
MC/DC100%2/2
$op_ok and $rhs =~ /\G ($op) /cgx
$op_ok
$rhs =~ /\G ($op) /cgx
Truth table100%3/3
A and B
A= $op_okB= $rhs =~ /\G ($op) /cgx
| |||||||||||||||||||||||||||||
| 117 | if $op_ok && $rhs =~ /\G ($op) /gcx; | ||||||||||||||||||||||||||||
| 118 | 258 | ▶ | $op_ok = 1, return [ 'ATOM', $1 ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | if $rhs =~ /\G ($simple_math_term) /gcx; | ||||||||||||||||||||||||||||
| 120 | 170 | ▶ | $op_ok = 0, return [ 'LPAREN', '(' ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | if $rhs =~ /\G $lparen /gcx; | ||||||||||||||||||||||||||||
| 122 | 150 | ▶ | $op_ok = 1, return [ 'RPAREN', ')' ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | if $rhs =~ /\G $rparen /gcx; | ||||||||||||||||||||||||||||
| 124 | 130 | ▶ | redo LEXER if $rhs =~ /\G \s+ /gcx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 32 | }; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 31 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 130 | 31 | my ( $class, $tokens ) = @_; | |||||||||||||||||||||||||||
| 131 | 31 | my $parens_left = 1; | |||||||||||||||||||||||||||
| 132 | 31 | ▶ | REDUCE: while ($parens_left) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 46 | my ( $first, $last ); | |||||||||||||||||||||||||||
| 134 | 46 | for my $i ( 0 .. $#$tokens ) { | |||||||||||||||||||||||||||
| 135 | 222 | my $token = $tokens->[$i]; | |||||||||||||||||||||||||||
| 136 | 222 | ▶ | next unless $token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 222 | ▶ | if ( "(" eq _as_string($token) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 19 | $first = $i; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 222 | ▶ | if ( ")" eq _as_string($token) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 15 | ▶ | unless ( defined $first ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # XXX I should probably cache the string and show it. | ||||||||||||||||||||||||||||
| 144 | # XXX But it doesn't matter because that shouldn't happen here | ||||||||||||||||||||||||||||
| 145 | 0 | croak( | |||||||||||||||||||||||||||
| 146 | "Parse error in math pre-processor. Mismatched parens" | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 15 | $last = $i; | |||||||||||||||||||||||||||
| 150 | $tokens->[$first] = $class->_parse_group( | ||||||||||||||||||||||||||||
| 151 | 15 | [ @{$tokens}[ $first + 1 .. $last - 1 ] ] ); | |||||||||||||||||||||||||||
| 152 | 15 | undef $tokens->[$_] for $first + 1 .. $last; | |||||||||||||||||||||||||||
| 153 | 15 | @$tokens = grep $_ => @$tokens; | |||||||||||||||||||||||||||
| 154 | 15 | undef $first; | |||||||||||||||||||||||||||
| 155 | 15 | undef $last; | |||||||||||||||||||||||||||
| 156 | 15 | redo REDUCE; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 31 | ▶ | $parens_left = 0 unless defined $first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 31 | return _as_string( $class->_parse_group($tokens) ); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 46 | ▶ | sub _parse_group { | ||||||||||||||||||||||||||
|
Subroutine
_parse_group: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 165 | 46 | my ( $class, $tokens ) = @_; | |||||||||||||||||||||||||||
| 166 | 46 | foreach my $op_re ( qr{(?:\*\*|[*/])}, qr{[+-]}, qr/\%/ ) { | |||||||||||||||||||||||||||
| 167 | 138 | for my $i ( 0 .. $#$tokens ) { | |||||||||||||||||||||||||||
| 168 | 334 | my $token = $tokens->[$i]; | |||||||||||||||||||||||||||
| 169 | 334 | ▶ | if ( ref $token && "@$token" =~ /OP ($op_re)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $token and join($", @$token) =~ /OP ($op_re)/
MC/DC100%2/2
ref $token and join($", @$token) =~ /OP ($op_re)/
ref $token
join($", @$token) =~ /OP ($op_re)/
Truth table100%3/3
A and B
A= ref $tokenB= join($", @$token) =~ /OP ($op_re)/
| |||||||||||||||||||||||||||||
| 170 | 53 | my $curr_op = $1; | |||||||||||||||||||||||||||
| 171 | 53 | my $prev = _prev_token( $tokens, $i ); | |||||||||||||||||||||||||||
| 172 | 53 | my $next = _next_token( $tokens, $i ); | |||||||||||||||||||||||||||
| 173 | $tokens->[$i] = sprintf | ||||||||||||||||||||||||||||
| 174 | 53 | "%s(%s, %s)" => $convert{$curr_op}, | |||||||||||||||||||||||||||
| 175 | _as_string( $tokens->[$prev] ), | ||||||||||||||||||||||||||||
| 176 | _as_string( $tokens->[$next] ); | ||||||||||||||||||||||||||||
| 177 | 53 | undef $tokens->[$prev]; | |||||||||||||||||||||||||||
| 178 | 53 | undef $tokens->[$next]; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 138 | @$tokens = grep $_ => @$tokens; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | #main::diag Dumper $tokens; | ||||||||||||||||||||||||||||
| 185 | 46 | return $tokens->[0]; # should never have more than on token left | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 53 | ▶ | sub _prev_token { | ||||||||||||||||||||||||||
|
Subroutine
_prev_token: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 189 | 53 | my ( $tokens, $index ) = @_; | |||||||||||||||||||||||||||
| 190 | 53 | for my $i ( reverse 0 .. $index - 1 ) { | |||||||||||||||||||||||||||
| 191 | 57 | ▶ | return $i if defined $tokens->[$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 53 | ▶ | sub _next_token { | ||||||||||||||||||||||||||
|
Subroutine
_next_token: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 196 | 53 | my ( $tokens, $index ) = @_; | |||||||||||||||||||||||||||
| 197 | 53 | for my $i ( $index + 1 .. $#$tokens ) { | |||||||||||||||||||||||||||
| 198 | 53 | ▶ | return $i if defined $tokens->[$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 581 | ▶ | sub _as_string { ref $_[0] ? $_[0][1] : $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
_as_string: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub match { shift; shift =~ $expression } | ||||||||||||||||||||||||||
|
Subroutine
match: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # The following are testing hooks | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 8 | ▶ | sub _compare { shift; shift =~ /^$compare$/ } | ||||||||||||||||||||||||||
|
Subroutine
_compare: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 209 | 10 | ▶ | sub _op { shift; shift =~ /^$op$/ } | ||||||||||||||||||||||||||
|
Subroutine
_op: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 210 | 12 | ▶ | sub _simple_rhs { shift; shift =~ /^$simple_rhs$/ } | ||||||||||||||||||||||||||
|
Subroutine
_simple_rhs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | 10 | ▶ | sub _simple_group_term { shift; shift =~ /^$simple_group_term$/ } | ||||||||||||||||||||||||||
|
Subroutine
_simple_group_term: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 15 | ▶ | sub _simple_math_term { shift; shift =~ /^$simple_math_term$/ } | ||||||||||||||||||||||||||
|
Subroutine
_simple_math_term: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 213 | 15 | ▶ | sub _math_term { shift; shift =~ /^$math_term$/ } | ||||||||||||||||||||||||||
|
Subroutine
_math_term: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 15 | ▶ | sub _complex_rhs { shift; shift =~ /^$complex_rhs$/ } | ||||||||||||||||||||||||||
|
Subroutine
_complex_rhs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 215 | 20 | ▶ | sub _complex_group_term { shift; shift =~ /^$complex_group_term$/ } | ||||||||||||||||||||||||||
|
Subroutine
_complex_group_term: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 14 | 1; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||