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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2645 - (download) (as text) (annotate)
Thu Aug 19 12:02:15 2004 UTC (15 years, 3 months ago) by dpvc
File size: 1695 byte(s)
Make sure equality always returns a defined value, and
put spaces around the equal sign in string output.

    1 #########################################################################
    2 #
    3 #  Implements equality
    4 #
    5 package Parser::BOP::equality;
    6 use strict; use vars qw(@ISA);
    7 @ISA = qw(Parser::BOP);
    8 
    9 #
   10 #  Check that the operand types are numbers.
   11 #
   12 sub _check {
   13   my $self = shift; my $name = $self->{def}{string} || $self->{bop};
   14   $self->Error("Only one equality is allowed in an equation")
   15     if ($self->{lop}->type eq 'Equality' || $self->{rop}->type eq 'Equality');
   16   $self->Error("Operands of '$name' must be Numbers") unless $self->checkNumbers();
   17   $self->{type} = Value::Type('Equality',1); # Make it not a number, to get errors with other operations.
   18 }
   19 
   20 #
   21 #  Determine if the two sides are equal (use fuzzy reals)
   22 #
   23 sub _eval {
   24   my $self = shift; my ($a,$b) = @_;
   25   $a = Value::makeValue($a) unless ref($a);
   26   $b = Value::makeValue($b) unless ref($b);
   27   return ($a == $b)? 1 : 0;
   28 }
   29 
   30 #
   31 #  Add/Remove the equality operator to/from a context
   32 #
   33 sub Allow {
   34   my $self = shift; my $context = shift || $$Value::context;
   35   my $allow = shift; $allow = 1 unless defined($allow);
   36   if ($allow) {
   37     my $prec = $context->{operators}{','}{precedence};
   38     $prec = 1 unless defined($prec);
   39     $context->operators->add(
   40       '=' => {
   41          class => 'Parser::BOP::equality',
   42          precedence => $prec+.25,  #  just above comma
   43          associativity => 'left',  #  computed left to right
   44          type => 'bin',            #  binary operator
   45          string => ' = ',          #  output string for it
   46          perl => '==',             #  perl string
   47       }
   48     );
   49   } else {$context->operators->remove('=')}
   50   return;
   51 }
   52 
   53 #########################################################################
   54 
   55 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9