[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 2626 - (download) (as text) (annotate)
Mon Aug 16 19:44:26 2004 UTC (15 years, 4 months ago) by dpvc
File size: 3090 byte(s)
One more fix for handling intervals properly (I think it's really
right this time).  Also, named constants that end in numbers will
produce TeX output with the number as a subscript (this was already
true for variable names).

    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 = 'Closed('.$perl.')'
   79     if $self->{canBeInterval} && $self->{open}.$self->{close} eq '[]';
   80   $perl = '('.$perl.')' if $parens;
   81   return $perl;
   82 }
   83 
   84 sub ijk {(shift)->{value}->ijk}
   85 
   86 #
   87 #  Convert the value to a Matrix object
   88 #
   89 sub makeMatrix {
   90   my $self = shift;
   91   my ($name,$open,$close) = @_;
   92   $self->{type}{name} = $name;
   93   $self->{value} = Value::Matrix->new($self->{value}->value);
   94 }
   95 
   96 #########################################################################
   97 
   98 1;
   99 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9