| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 66 | 68 | ▶ | $val->isa("Math::Expr::Node") || warn("Bad param val: $val"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 68 | ▶ | !$self->InTable || warn "Can't edit items in the table"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 77 | 75 | ▶ | if (ref $pre) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | 67 | ▶ | $pre->isa("Math::Expr::VarSet") || warn "Bad param pre: $pre"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 95 | 75 | ▶ | $mset->isa("Math::Expr::MatchSet") || warn "Bad param mset: $mset"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | |||||||||||||||
| 97 | 75 | my $key=$rule->tostr.'§'.$mset->tostr; | |||||||||||||
| 98 | 0 | ▶ | if ($self->{'SubMatches'}{$key}) {return $self->{'SubMatches'}{$key};} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 125 | |||||||||||||||
| 126 | 38 | $mset->Set($pos, $pri); | |||||||||||||
| 127 | 38 | ▶ | if (!$self->SubMatch($rule, $mset)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 148 | |||||||||||||||
| 149 | 305 | my $key=$self->tostr; | |||||||||||||
| 150 | 305 | ▶ | if (defined $table{$key}) {return $table{$key};} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | |||||||||||||||
| 152 | 103 | ▶ | if ($self->isa("Math::Expr::Opp")) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 93 | ▶ | for (my $i=0; $i<=$#{$self->{'Opps'}}; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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; | |||||||||||||