[system] / trunk / pg / lib / Parser / BOP / power.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/BOP/power.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2678 - (download) (as text) (annotate)
Mon Aug 23 23:55:37 2004 UTC (15 years, 5 months ago) by dpvc
File size: 1807 byte(s)
Modified the parser so that the classes for the various object
constructors are stored in the context table rather than hard-coded
into the parser.  That way, you can override the default classes with
your own.  This gives you even more complete control to modify the
parser.  (You had been able to replace the definitions of operators,
functions and list-like objects, but could not override the behaviour
of numbers, strings, variables, and so on.  Now you can.)

This effects most of the files, but only by changing the name of the
calls that create the various objects.

There are also a couple of other minor fixes.

    1 #########################################################################
    2 #
    3 #  Implements exponentiation
    4 #
    5 package Parser::BOP::power;
    6 use strict; use vars qw(@ISA);
    7 @ISA = qw(Parser::BOP);
    8 
    9 #
   10 #  Check that operand types are OK.
   11 #  For non-numbers, promote to Matrix and check
   12 #    that the sizes are OK and that the exponents are numbers
   13 sub _check {
   14   my $self = shift;
   15   return if ($self->checkStrings());
   16   return if ($self->checkLists());
   17   return if ($self->checkNumbers());
   18   my ($ltype,$rtype) = $self->promotePoints('Matrix');
   19   if ($rtype->{name} eq 'Number') {
   20     if ($ltype->{name} eq 'Matrix') {$self->checkMatrixSize($ltype,$ltype)}
   21     else {$self->Error("You can only raise a Number to a power")}
   22   } else {$self->Error("Exponents must be Numbers")}
   23 }
   24 
   25 #
   26 #  Do perl exponentiation
   27 #
   28 sub _eval {$_[1] ** $_[2]}
   29 
   30 #
   31 #  Return 1 for power of zero or base of 1.
   32 #  Return base if power is 1.
   33 #  Return 1/base if power is -1.
   34 #
   35 sub _reduce {
   36   my $self = shift; my $equation = $self->{equation};
   37   my $parser = $equation->{context}{parser};
   38   return $parser->{Number}->new($equation,1)
   39    if ($self->{rop}{isZero} || $self->{lop}{isOne});
   40   return $self->{lop} if ($self->{rop}{isOne});
   41   if ($self->{rop}->isNeg && $self->{rop}->string eq '-1') {
   42     $self = $parser->{BOP}->new($equation,'/',
   43       $parser->{Number}->new($equation,1),$self->{lop});
   44     $self = $self->reduce;
   45   }
   46   return $self;
   47 }
   48 
   49 #
   50 #  Put exponent in braces for TeX
   51 #
   52 sub TeX {
   53   my ($self,$precedence,$showparens,$position) = @_;
   54   my $TeX; my $bop = $self->{def};
   55   my $symbol = (defined($bop->{TeX}) ? $bop->{TeX} : $bop->{string});
   56   $self->{lop}->TeX($bop->{precedence},$bop->{leftparens},'left').
   57     $symbol.'{'.$self->{rop}->TeX.'}';
   58 }
   59 
   60 #########################################################################
   61 
   62 1;
   63 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9