[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 2678 - (download) (as text) (annotate)
Mon Aug 23 23:55:37 2004 UTC (15 years, 6 months ago) by dpvc
File size: 1249 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 #  Implement addition.
    4 #
    5 package Parser::BOP::add;
    6 use strict; use vars qw(@ISA);
    7 @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   return $self->{lop} if ($self->{rop}{isZero});
   34   return $self->{rop} if ($self->{lop}{isZero});
   35   my $equation = $self->{equation};
   36   if ($self->{rop}->isNeg) {
   37     $self = $equation->{context}{parser}{BOP}->new($equation,'-',$self->{lop},$self->{rop}{op});
   38     $self = $self->reduce;
   39   } elsif ($self->{lop}->isNeg) {
   40     $self = $equation->{context}{parser}{BOP}->new($equation,'-',$self->{rop},$self->{lop}{op});
   41     $self = $self->reduce;
   42   }
   43   return $self;
   44 }
   45 
   46 #########################################################################
   47 
   48 1;
   49 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9