line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Var.pm - A perl representation of variables. |
4
|
|
|
|
|
|
|
# (c) Copyright 1998 Hakan Ardo |
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
|
|
|
|
|
|
|
require Math::Expr::Node; |
47
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
79
|
|
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
1
|
|
5
|
use Math::Expr::Node; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
50
|
1
|
|
|
1
|
|
5
|
use vars qw(@ISA); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
508
|
|
51
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
sub new { |
60
|
213
|
|
|
213
|
0
|
382
|
my($class, $val) = @_; |
61
|
213
|
|
|
|
|
560
|
my $self = bless { }, $class; |
62
|
|
|
|
|
|
|
|
63
|
213
|
|
|
|
|
1145
|
($self->{'Val'},$self->{'VarType'})=split(/:/,$val); |
64
|
213
|
100
|
|
|
|
609
|
if (!$self->{'VarType'}) {$self->{'VarType'}="Real";} |
|
101
|
|
|
|
|
4095
|
|
65
|
213
|
|
|
|
|
769
|
$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
|
|
|
|
|
|
|
sub tostr { |
76
|
1615
|
|
|
1615
|
1
|
2326
|
my $self = shift; |
77
|
1615
|
|
|
|
|
5785
|
$self->{'Val'}. ":" . $self->{'VarType'}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub toText { |
81
|
61
|
|
|
61
|
0
|
67
|
my $self = shift; |
82
|
61
|
|
|
|
|
186
|
$self->{'Val'}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 $n->strtype |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Returns the type of the variable. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub strtype { |
92
|
1340
|
|
|
1340
|
1
|
1714
|
my $self = shift; |
93
|
1340
|
|
|
|
|
3904
|
$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
|
|
|
1340
|
1
|
3658
|
sub BaseType {shift->strtype(@_)} |
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
|
|
|
|
|
|
|
sub SubMatch { |
120
|
68
|
|
|
68
|
1
|
161
|
my ($self, $rule, $mset) = @_; |
121
|
|
|
|
|
|
|
|
122
|
68
|
100
|
66
|
|
|
368
|
if ($rule->isa('Math::Expr::Var') && $self->BaseType eq $rule->BaseType) { |
123
|
34
|
|
|
|
|
108
|
$mset->SetAll($rule->{'Val'},$self); |
124
|
34
|
|
|
|
|
106
|
return 1; |
125
|
|
|
|
|
|
|
} |
126
|
34
|
|
|
|
|
179
|
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
|
|
|
|
|
|
|
sub _Subs { |
137
|
73
|
|
|
73
|
|
106
|
my ($self, $vars) = @_; |
138
|
73
|
|
|
|
|
232
|
my $v=$vars->Get($self->{'Val'}); |
139
|
|
|
|
|
|
|
|
140
|
73
|
100
|
|
|
|
165
|
if ($v) {return $v} else {return new Math::Expr::Var($self->{'Val'}.":". |
|
69
|
|
|
|
|
300
|
|
|
4
|
|
|
|
|
17
|
|
141
|
|
|
|
|
|
|
$self->{'VarType'})} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 $n->Copy |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Returns a new copy of itself. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub _Copy { |
151
|
108
|
|
|
108
|
|
125
|
my $self= shift; |
152
|
|
|
|
|
|
|
|
153
|
108
|
|
|
|
|
466
|
new Math::Expr::Var($self->{'Val'}.":".$self->{'VarType'}); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _toMathML { |
157
|
30
|
|
|
30
|
|
37
|
my $self = shift; |
158
|
|
|
|
|
|
|
|
159
|
30
|
|
|
|
|
107
|
"".$self->{'Val'}.""; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 AUTHOR |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Hakan Ardo |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SEE ALSO |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
L |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
1; |