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

View of /trunk/pg/lib/Parser/Item.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2607 - (download) (as text) (annotate)
Sat Aug 14 11:26:49 2004 UTC (15 years, 3 months ago) by dpvc
File size: 3062 byte(s)
Allow unary + and - to work on infinities.

    1 #########################################################################
    2 #
    3 #  Implements the basic parse tree node.  Subclasses of this class
    4 #  are things like binary operator, function call, and so on.
    5 #
    6 package Parser::Item;
    7 use strict;
    8 
    9 #
   10 #  Return the class name of an item
   11 #
   12 sub class {
   13   my $self = ref(shift);
   14   $self =~ s/[^:]*:://; $self =~ s/::.*//;
   15   return $self;
   16 }
   17 
   18 #
   19 #  Get various type information
   20 #
   21 sub type {my $self = shift; return $self->{type}{name}}
   22 sub typeRef {my $self = shift; return $self->{type}}
   23 sub length {my $self = shift; return $self->{type}{length}}
   24 sub entryType {
   25   my $self = shift; my $type = $self->{type};
   26   return $type->{list} ? $type->{entryType}: $type;
   27 }
   28 
   29 #
   30 #  True if two types agree
   31 #
   32 sub typeMatch {
   33   my ($ltype,$rtype) = @_;
   34   return 0 if ($ltype->{name} ne $rtype->{name});
   35   return 1 if (!$ltype->{list} && !$rtype->{list});
   36   return 0 if ($ltype->{list} != $rtype->{list});
   37   return 0 if ($ltype->{length} ne $rtype->{length});
   38   return typeMatch($ltype->{entryType},$rtype->{entryType});
   39 }
   40 
   41 #
   42 #  Check if an item is a number, complex, etc.
   43 #
   44 sub isRealNumber {my $self = shift; return $self->isNumber && !$self->isComplex}
   45 sub isNumber {my $self = shift; return ($self->typeRef->{name} eq 'Number')}
   46 sub isComplex {
   47   my $self = shift; my $type = $self->typeRef;
   48   return ($type->{name} eq 'Number' && $type->{length} == 2);
   49 }
   50 sub isNumOrInfinity {
   51   my $self = shift;
   52   return ($self->isRealNumber || $self->{isInfinite});
   53 }
   54 
   55 #
   56 #  Check if an item is a unary negation
   57 #
   58 sub isNeg {
   59   my $self = shift;
   60   return ($self->class eq 'UOP' && $self->{uop} eq 'u-' && !$self->{op}->{isInfinite});
   61 }
   62 
   63 #
   64 #  These are stubs for the subclasses
   65 #
   66 sub getVariables {{}}   #  find out what variables are used
   67 sub makeList {shift}    #  flatten a tree of commas into a list
   68 sub makeUnion {shift}   #  flatten a tree of unions into a list of intervals
   69 sub makeMatrix {}       #  convert a list to a matrix
   70 
   71 sub reduce {shift}
   72 sub substitute {shift}
   73 sub string {}
   74 sub TeX {}
   75 sub perl {}
   76 
   77 sub ijk {
   78   my $self = shift;
   79   $self->Error("Can't use method 'ijk' with objects of type '".$self->type."'");
   80 }
   81 
   82 #
   83 #  Recursively copy an item, and set a new equation pointer, if any
   84 #
   85 sub copy {
   86   my $self = shift; my $equation = shift;
   87   my $new = {%{$self}};
   88   if (ref($self) ne 'HASH') {
   89     bless $new, ref($self);
   90     $new->{equation} = $equation if defined($equation);
   91     $new->{ref} = undef;
   92   }
   93   $new->{type} = copy($self->{type}) if defined($self->{type});
   94   return $new;
   95 }
   96 
   97 #
   98 #  Report an error message
   99 #
  100 sub Error {
  101   my $self = shift;
  102   $self->{equation}->Error(@_,$self->{ref}) if defined($self->{equation});
  103   Parser->Error(@_);
  104 }
  105 
  106 #########################################################################
  107 #
  108 #  Load the subclasses.
  109 #
  110 
  111 use Parser::BOP;
  112 use Parser::UOP;
  113 use Parser::List;
  114 use Parser::Function;
  115 use Parser::Variable;
  116 use Parser::Constant;
  117 use Parser::Value;
  118 use Parser::Number;
  119 use Parser::Complex;
  120 use Parser::String;
  121 
  122 #########################################################################
  123 
  124 1;
  125 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9