[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 2625 - (download) (as text) (annotate)
Mon Aug 16 18:35:12 2004 UTC (15 years, 3 months ago) by dpvc
File size: 2985 byte(s)
Added string comparison to all Value object classes (to compare the
string value of an object to another string).

Overloaded perl '.' operator to do dot product when the operands are
formulas returning vectors.  (Part of the auto-generation of
formulas).

A few improvements to real and complex class output results.

Made Union class slightly more robust and removed need for makeUnion
method other than in the Union itself.

    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 makeMatrix {}       #  convert a list to a matrix
   69 
   70 sub reduce {shift}
   71 sub substitute {shift}
   72 sub string {}
   73 sub TeX {}
   74 sub perl {}
   75 
   76 sub ijk {
   77   my $self = shift;
   78   $self->Error("Can't use method 'ijk' with objects of type '".$self->type."'");
   79 }
   80 
   81 #
   82 #  Recursively copy an item, and set a new equation pointer, if any
   83 #
   84 sub copy {
   85   my $self = shift; my $equation = shift;
   86   my $new = {%{$self}};
   87   if (ref($self) ne 'HASH') {
   88     bless $new, ref($self);
   89     $new->{equation} = $equation if defined($equation);
   90     $new->{ref} = undef;
   91   }
   92   $new->{type} = copy($self->{type}) if defined($self->{type});
   93   return $new;
   94 }
   95 
   96 #
   97 #  Report an error message
   98 #
   99 sub Error {
  100   my $self = shift;
  101   $self->{equation}->Error(@_,$self->{ref}) if defined($self->{equation});
  102   Parser->Error(@_);
  103 }
  104 
  105 #########################################################################
  106 #
  107 #  Load the subclasses.
  108 #
  109 
  110 use Parser::BOP;
  111 use Parser::UOP;
  112 use Parser::List;
  113 use Parser::Function;
  114 use Parser::Variable;
  115 use Parser::Constant;
  116 use Parser::Value;
  117 use Parser::Number;
  118 use Parser::Complex;
  119 use Parser::String;
  120 
  121 #########################################################################
  122 
  123 1;
  124 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9