Expr/Node.pm

filter: Statementstmt 92.5% 62 / 67 Branchbran 67.5% 27 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 10 totaltotal 78.7% 104 / 132 CC 21 SCAR 13.1
CC
21
Cov
83%
Match
6.8
IntoTable
5.0
SubMatch
4.4
CRAP 3.7
« Expr/MatchSet.pm ↑ Summary Expr/Num.pm »
1 #!/usr/bin/perl
2  
3 # Node.pm - A node in the expretion tree
4 # (c) Copyright 1998 Hakan Ardo <hakan@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  
20  
21 =head1 NAME
22  
23 Math::Expr::Num - A node in the expretion tree, used as superclass only
24  
25 =head1 SYNOPSIS
26  
27 package Math::Expr::Num;
28 require Math::Expr::Node;
29  
30 use Math::Expr::Node;
31 use vars qw(@ISA);
32 @ISA = qw(Math::Expr::Node);
33  
34  
35 =head1 DESCRIPTION
36  
37 Each expretion is represented by a tree where each opperation and variable
38 is a separate node. This class contain the common code for all those noeds.
39  
40 It also defines all the common methods used in those node classes and does
41 some typecheckinig for them. Therefor the methodname in the subclasses
42 should start with a '_'-char followed by the actually method name. This
43 method will be called by the acutall method in this class after the
44 typecheking is done.
45  
46 =cut
47  
48 package Math::Expr::Node;
49 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # Type checking and result caching
52  
53 178 ▶sub Subs{
Subroutine Subs: called CC=2 SCAR=7.1
Pod undocumented
54 178 my ($self, $vars) = @_;
55 178 ▶ $vars->isa("Math::Expr::VarSet")|| warn("Bad param vars: $vars");
Branch: 1 branch
BranchTrueFalseTotal
$vars->isa("Math::Expr::VarSet") or warn "Bad param vars: $vars" 178 0 178
56 178 $self->_Subs($vars);
57 }
58  
59 653 ▶sub Breakable{shift->_Breakable(@_)}
Subroutine Breakable: called CC=1 SCAR=0.0
Pod undocumented
60 60 ▶sub toMathML{shift->_toMathML}
Subroutine toMathML: called CC=1 SCAR=0.0
Pod undocumented
61  
62 68 ▶sub Set{
Subroutine Set: called CC=4 SCAR=14.6
Pod undocumented
63 68 my ($self, $pos, $val) = @_;
64  
65 68 ▶ defined $pos || warn("Bad param pos: $pos");
Branch: 1 branch
BranchTrueFalseTotal
defined $pos or warn "Bad param pos: $pos" 68 0 68
66 68 ▶ $val->isa("Math::Expr::Node") || warn("Bad param val: $val");
Branch: 1 branch
BranchTrueFalseTotal
$val->isa("Math::Expr::Node") or warn "Bad param val: $val" 68 0 68
67 68 ▶ !$self->InTable || warn "Can't edit items in the table";
Branch: 1 branch
BranchTrueFalseTotal
$self->InTable and warn "Can't edit items in the table" 0 68 68
68
69 68 $self->_Set($pos,$val);
70 }
71  
72 75 ▶sub Match{
Subroutine Match: called CC=6 SCAR=19.2
Pod undocumented
73 75 my ($self,$rule,$pos,$pre)= @_;
74 75 my $key=$rule->tostr.'§'.$pos;
75  
76 75 ▶ $rule->isa("Math::Expr::Node") || warn "Bad param rule: $rule";
Branch: 1 branch
BranchTrueFalseTotal
$rule->isa("Math::Expr::Node") or warn "Bad param rule: $rule" 75 0 75
77 75 ▶ if (ref $pre) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pre) { } 67 8 75
elsif (defined $pre) { } 0 8 8
78 67 ▶ $pre->isa("Math::Expr::VarSet") || warn "Bad param pre: $pre";
Branch: 1 branch
BranchTrueFalseTotal
$pre->isa("Math::Expr::VarSet") or warn "Bad param pre: $pre" 67 0 67
79 67 $key.='§'.$pre->tostr;
80 }
81 elsif(defined $pre) {
82 0 warn "Bad param pre: $pre";
83 }
84  
85  
86 0 ▶ if ($self->{'Matches'}{$key}) {return $self->{'Matches'}{$key};}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Matches"}{$key}) 0 75 75
87 75 $self->{'Matches'}{$key}=$self->_Match($rule,$pos,$pre);
88 75 $self->{'Matches'}{$key};
89 }
90  
91 75 ▶sub SubMatch{
Subroutine SubMatch: called CC=4 SCAR=14.8
Pod undocumented
92 75 my ($self, $rule, $mset) = @_;
93  
94 75 ▶ $rule->isa("Math::Expr::Node") || warn "Bad param rule: $rule";
Branch: 1 branch
BranchTrueFalseTotal
$rule->isa("Math::Expr::Node") or warn "Bad param rule: $rule" 75 0 75
95 75 ▶ $mset->isa("Math::Expr::MatchSet") || warn "Bad param mset: $mset";
Branch: 1 branch
BranchTrueFalseTotal
$mset->isa("Math::Expr::MatchSet") or warn "Bad param mset: $mset" 75 0 75
96  
97 75 my $key=$rule->tostr.'§'.$mset->tostr;
98 0 ▶ if ($self->{'SubMatches'}{$key}) {return $self->{'SubMatches'}{$key};}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SubMatches"}{$key}) 0 75 75
99 75 $self->{'SubMatches'}{$key}=$self->_SubMatch($rule,$mset);
100 75 $self->{'SubMatches'}{$key};
101 }
102  
103 217 ▶sub Copy{shift->_Copy}
Subroutine Copy: called CC=1 SCAR=0.0
Pod undocumented
104  
105 # Default actions
106  
107 204 ▶sub Simplify {shift->IntoTable;}
Subroutine Simplify: called CC=1 SCAR=0.0
Pod undocumented
108  
109 23 ▶sub _Subs {shift;}
Subroutine _Subs: called CC=1 SCAR=0.0
110  
111 14 ▶sub _Breakable{0;}
Subroutine _Breakable: called CC=1 SCAR=0.0
112  
113 10 ▶sub _Set {
Subroutine _Set: called CC=2 SCAR=7.6
114 10 my ($self, $pos, $val)=@_;
115  
116 0 ▶ if ($pos ne "") {warn "Bad pos: $pos"}
Branch: 1 branch
BranchTrueFalseTotal
if ($pos ne "") 0 10 10
117 10 $val;
118 }
119  
120 38 ▶sub _Match {
Subroutine _Match: called CC=3 SCAR=11.1
121 38 my ($self, $rule,$pos,$pri) = @_;
122 38 my $mset=new Math::Expr::MatchSet;
123  
124 0 ▶ if (!defined $pri) {$pri=new Math::Expr::VarSet}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pri) 0 38 38
125  
126 38 $mset->Set($pos, $pri);
127 38 ▶ if (!$self->SubMatch($rule, $mset)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SubMatch($rule, $mset)) 28 10 38
128 28 $mset->del($pos);
129 }
130 38 $mset;
131 }
132  
133 # Table handling
134 #use MLDBM;
135 #use Fcntl;
136 1 my %table;
137 #tie %table, 'MLDBM','testmldbm', O_CREAT|O_RDWR, 0640 or die $!;
138  
139 1256 ▶sub InTable {
Subroutine InTable: called CC=2 SCAR=6.9
Pod undocumented
140 1256 my $self=shift;
141 1256 ▶ if (defined $self->{'TableKey'}) {return 1} else {return 0}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'TableKey'}) { } 632 624 1256
142 }
143  
144 713 ▶sub IntoTable {
Subroutine IntoTable: called CC=5 SCAR=16.1
Pod undocumented
145 713 my $self=shift;
146  
147 713 ▶ if ($self->InTable) {return $self;}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->InTable) 408 305 713
148  
149 305 my $key=$self->tostr;
150 305 ▶ if (defined $table{$key}) {return $table{$key};}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table{$key}) 202 103 305
151
152 103 ▶ if ($self->isa("Math::Expr::Opp")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("Math::Expr::Opp")) 93 10 103
153 93 ▶ for (my $i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 174 93 267
154 174 $self->{'Opps'}[$i]=$self->{'Opps'}[$i]->IntoTable;
155 }
156 }
157 103 $self->{'TableKey'}=$key;
158 103 $table{$key}=$self;
159  
160 103 $self;
161 }
162 1 1;
« Expr/MatchSet.pm ↑ Summary Expr/Num.pm »