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

View of /trunk/pg/lib/Parser/Value.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, 4 months ago) by dpvc
File size: 2989 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 #  Class that allows Value.pm objects to be included in formulas
    4 #    (used to store constant Vector values, etc.)
    5 #
    6 package Parser::Value;
    7 use strict; use vars qw(@ISA);
    8 @ISA = qw(Parser::Item);
    9 
   10 #
   11 #  Get the Value.pm type of the constant
   12 #  Return it if it is an equation
   13 #  Make a new string or number if it is one of those
   14 #  Error if we don't know what it is
   15 #  Otherwise, get a Value object for the item and use it.
   16 #
   17 sub new {
   18   my $self = shift; my $class = ref($self) || $self;
   19   my $equation = shift;
   20   my ($value,$ref) = @_;
   21   $value = $value->[0] if ref($value) eq 'ARRAY' && scalar(@{$value}) == 1;
   22   my $type = Value::getType($equation,$value);
   23   return $value->{tree}->copy($equation) if ($type eq 'Formula');
   24   return Parser::String->new($equation,$value,$ref) if ($type eq 'String');
   25   return Parser::Number->new($equation,$value,$ref) if ($type eq 'Number');
   26   return Parser::Number->new($equation,$value->{data},$ref)
   27     if ($type eq 'value' && $value->class eq 'Complex');
   28   $equation->Error("Can't convert ".Value::showClass($value)." to a constant",$ref)
   29     if ($type eq 'unknown');
   30   $type = 'Value::'.$type, $value = $type->new(@{$value}) unless $type eq 'value';
   31   my $type = $value->typeRef;
   32   my $c = bless {
   33     value => $value, type => $type, isConstant => 1,
   34     ref => $ref, equation => $equation,
   35   }, $class;
   36   $c->{canBeInterval} = 1 if ($value->class eq 'Point' && $type->{length} == 2);
   37   ## check for isZero  (method of $value?)
   38   ## (hack for now)
   39   $c->{isZero} = 1 if $type->{name} eq 'Number' && $value == 0;
   40   $c->{isOne}  = 1 if $type->{name} eq 'Number' && $value eq 1;
   41   return $c;
   42 }
   43 
   44 #
   45 #  Return the Value.pm object
   46 #
   47 sub eval {return (shift)->{value}}
   48 
   49 #
   50 #  Return the item's list of coordinates
   51 #    (for points, vectors, matrices, etc.)
   52 #
   53 sub coords {
   54   my $self = shift;
   55   return [$self->{value}] unless $self->typeRef->{list};
   56   my @coords = ();
   57   foreach my $x (@{$self->{value}->data})
   58     {push(@coords,Parser::Value->new($self->{equation},[$x]))}
   59   return [@coords];
   60 }
   61 
   62 #
   63 #  Call the appropriate formatter from Value.pm
   64 #
   65 sub string {
   66   my $self = shift; my $precedence = shift;
   67   my $string = $self->{value}->string($self->{equation},$self->{open},$self->{close},$precedence);
   68   return $string;
   69 }
   70 sub TeX {
   71   my $self = shift; my $precedence = shift;
   72   my $TeX = $self->{value}->TeX($self->{equation},$self->{open},$self->{close},$precedence);
   73   return $TeX;
   74 }
   75 sub perl {
   76   my $self = shift; my $parens = shift; my $matrix = shift;
   77   my $perl = $self->{value}->perl(0,$matrix);
   78   $perl = '('.$perl.')' if $parens;
   79   return $perl;
   80 }
   81 
   82 sub ijk {(shift)->{value}->ijk}
   83 
   84 #
   85 #  Convert the value to a Matrix object
   86 #
   87 sub makeMatrix {
   88   my $self = shift;
   89   my ($name,$open,$close) = @_;
   90   $self->{type}{name} = $name;
   91   $self->{value} = Value::Matrix->new($self->{value}->value);
   92 }
   93 
   94 #########################################################################
   95 
   96 1;
   97 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9