Expr/Var.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 6 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 66.6% 4 / 6 totaltotal 94.1% 64 / 68 CC 5 SCAR 4.9
CC
5
Cov
98%
SubMatch
3.0
new
2.0
_Subs
2.0
CRAP 1.6
« Expr/Num.pm ↑ Summary
1 #!/usr/bin/perl
2  
3 # Var.pm - A perl representation of variables.
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 =head1 NAME
21  
22 Math::Expr::Var - Represents one variable in a parsed expression tree
23  
24 =head1 SYNOPSIS
25  
26 require Math::Expr::Opp;
27 require Math::Expr::Var;
28 require Math::Expr::Num;
29
30 # To represent the expression "x+7":
31 $n=new Math::Expr::Opp("+");
32 $n->SetOpp(0,new Math::Expr::Var("x"));
33 $n->SetOpp(1,new Math::Expr::Num(7));
34 print $n->tostr . "\n";
35  
36  
37 =head1 DESCRIPTION
38  
39 Used by the Math::Expr to represent variables.
40  
41 =head1 METHODS
42  
43 =cut
44  
45 package Math::Expr::Var;
46 1 require Math::Expr::Node;
47 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use Math::Expr::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 @ISA = qw(Math::Expr::Node);
52  
53 =head2 $n=new Math::Expr::Var($name)
54  
55 Creates a new representation of the variable named $name.
56  
57 =cut
58  
59 213 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
60 213 my($class, $val) = @_;
61 213 my $self = bless { }, $class;
62  
63 213 ($self->{'Val'},$self->{'VarType'})=split(/:/,$val);
64 213 ▶ if (!$self->{'VarType'}) {$self->{'VarType'}="Real";}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"VarType"}) 101 112 213
65 213 $self;
66 }
67  
68 =head2 $n->tostr
69  
70 Returns the string representation of the variable, that is the
71 variable name.
72  
73 =cut
74  
75 1627 ▶sub tostr {
Subroutine tostr: called CC=1 SCAR=0.0
Pod documented
76 1627 my $self = shift;
77 1627 $self->{'Val'}. ":" . $self->{'VarType'};
78 }
79  
80 61 ▶sub toText {
Subroutine toText: called CC=1 SCAR=0.0
Pod undocumented
81 61 my $self = shift;
82 61 $self->{'Val'};
83 }
84  
85 =head2 $n->strtype
86  
87 Returns the type of the variable.
88  
89 =cut
90  
91 1340 ▶sub strtype {
Subroutine strtype: called CC=1 SCAR=0.0
Pod documented
92 1340 my $self = shift;
93 1340 $self->{'VarType'};
94 }
95  
96 =head2 $n->BaseType
97  
98 Simply cals strtype, its needed to be compatible with the other
99 elements in the structure.
100  
101 =cut
102  
103 1340 ▶sub BaseType {shift->strtype(@_)}
Subroutine BaseType: called CC=1 SCAR=0.0
Pod documented
104  
105 =head2 $n->Match
106  
107 Mathces a rule expression with the variable, and returns an array of
108 VarSet objects with this variable name set to the expression if there
109 types match.
110  
111 =cut
112  
113 =head2 $n->SubMatch
114  
115 Used by upper level Match procedure to match an entire expression.
116  
117 =cut
118  
119 68 ▶sub SubMatch {
Subroutine SubMatch: called CC=3 SCAR=11.0
Pod documented
120 68 my ($self, $rule, $mset) = @_;
121  
122 68 ▶ if ($rule->isa('Math::Expr::Var') && $self->BaseType eq $rule->BaseType) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->isa("Math::Expr::Var") and $self->BaseType eq $rule->BaseType) 34 34 68
Condition66%2/3
$rule->isa("Math::Expr::Var") and $self->BaseType eq $rule->BaseType
!ll&&!rl&&r
34034
MC/DC50%1/2
$rule->isa("Math::Expr::Var") and $self->BaseType eq $rule->BaseType
$rule->isa("Math::Expr::Var") $self->BaseType eq $rule->BaseType
Truth table66%2/3
A and B
A= $rule->isa("Math::Expr::Var")B= $self->BaseType eq $rule->BaseType
ABresult
0X0
100
111
123 34 $mset->SetAll($rule->{'Val'},$self);
124 34 return 1;
125 }
126 34 return 0;
127 }
128  
129 =head2 $n->Subs($vars)
130  
131 Returns this variables vaule taken from $vars or a new copy of itselfe
132 if it does not excist.
133  
134 =cut
135  
136 73 ▶sub _Subs {
Subroutine _Subs: called CC=2 SCAR=6.9
137 73 my ($self, $vars) = @_;
138 73 my $v=$vars->Get($self->{'Val'});
139  
140 73 ▶ if ($v) {return $v} else {return new Math::Expr::Var($self->{'Val'}.":".
Branch: 1 branch
BranchTrueFalseTotal
if ($v) { } 69 4 73
141 4 $self->{'VarType'})}
142 }
143  
144 =head2 $n->Copy
145  
146 Returns a new copy of itself.
147  
148 =cut
149  
150 108 ▶sub _Copy {
Subroutine _Copy: called CC=1 SCAR=0.0
151 108 my $self= shift;
152  
153 108 new Math::Expr::Var($self->{'Val'}.":".$self->{'VarType'});
154 }
155  
156 30 ▶sub _toMathML {
Subroutine _toMathML: called CC=1 SCAR=0.0
157 30 my $self = shift;
158  
159 30 "<mi>".$self->{'Val'}."</mi>";
160 }
161  
162 =head1 AUTHOR
163  
164 Hakan Ardo <hakan@debian.org>
165  
166 =head1 SEE ALSO
167  
168 L<Math::Expr::Opp>
169  
170 =cut
171  
172 1 1;
« Expr/Num.pm ↑ Summary