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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5001 - (download) (as text) (annotate)
Tue Jun 12 04:05:56 2007 UTC (6 years ago) by dpvc
File size: 1460 byte(s)
More fixes for creating items in the corret context.  Also added a
method for looking up the package associated with a particular Parser
class (for consistency with the Value->Package call).

    1 #########################################################################
    2 #
    3 #  Implement addition.
    4 #
    5 package Parser::BOP::add;
    6 use strict;
    7 our @ISA = qw(Parser::BOP);
    8 
    9 #
   10 #  Check that the operand types are compatible.
   11 #
   12 sub _check {
   13   my $self = shift;
   14   return if ($self->checkStrings());
   15   return if ($self->checkLists());
   16   return if ($self->checkNumbers());
   17   my ($ltype,$rtype) = $self->promotePoints();
   18   if (Parser::Item::typeMatch($ltype,$rtype)) {$self->{type} = $ltype}
   19   else {$self->matchError($ltype,$rtype)}
   20 }
   21 
   22 #
   23 #  Do addition.
   24 #
   25 sub _eval {$_[1] + $_[2]}
   26 
   27 #
   28 #  Remove addition with zero.
   29 #  Turn addition of negative into subtraction.
   30 #
   31 sub _reduce {
   32   my $self = shift;
   33   my $equation = $self->{equation};
   34   my $reduce = $equation->{context}{reduction};
   35   return $self->{lop} if $self->{rop}{isZero} && $reduce->{'x+0'};
   36   return $self->{rop} if $self->{lop}{isZero} && $reduce->{'0+x'};
   37   if ($self->{rop}->isNeg && $reduce->{'x+(-y)'}) {
   38     $self = $self->Item("BOP")->new($equation,'-',$self->{lop},$self->{rop}{op});
   39     $self = $self->reduce;
   40   } elsif ($self->{lop}->isNeg && $reduce->{'(-x)+y'}) {
   41     $self = $self->Item("BOP")->new($equation,'-',$self->{rop},$self->{lop}{op});
   42     $self = $self->reduce;
   43   }
   44   return $self;
   45 }
   46 
   47 $Parser::reduce->{'0+x'} = 1;
   48 $Parser::reduce->{'x+0'} = 1;
   49 $Parser::reduce->{'x+(-y)'} = 1;
   50 $Parser::reduce->{'(-x)+y'} = 1;
   51 
   52 #########################################################################
   53 
   54 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9