[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 5992 - (download) (as text) (annotate)
Thu Feb 5 15:01:44 2009 UTC (11 years ago) by dpvc
File size: 3606 byte(s)
Removed unused variable

    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; no strict "refs";
    8 our @ISA = qw(Parser::Item);
    9 
   10 $Parser::class->{Value} = 'Parser::Value';
   11 
   12 #
   13 #  Get the Value.pm type of the constant
   14 #  Return it if it is an equation
   15 #  Make a new string or number if it is one of those
   16 #  Error if we don't know what it is
   17 #  Otherwise, get a Value object for the item and use it.
   18 #
   19 sub new {
   20   my $self = shift; my $class = ref($self) || $self;
   21   my $equation = shift; my $context = $equation->{context};
   22   my ($value,$ref) = @_;
   23   $value = $value->[0] if ref($value) eq 'ARRAY' && scalar(@{$value}) == 1;
   24   my $type = Value::getType($equation,$value);
   25   return $value->{tree}->copy($equation) if ($type eq 'Formula');
   26   return $self->Item("String",$context)->new($equation,$value,$ref) if ($type eq 'String');
   27   return $self->Item("String",$context)->newInfinity($equation,$value,$ref) if ($type eq 'Infinity');
   28   return $self->Item("Number",$context)->new($equation,$value,$ref) if ($type eq 'Number');
   29   return $self->Item("Number",$context)->new($equation,$value->{data},$ref)
   30     if ($type eq 'value' && $value->class eq 'Complex');
   31   $equation->Error(["Can't convert %s to a constant",Value::showClass($value)],$ref)
   32     if ($type eq 'unknown');
   33   $type = $context->Package($type), $value = $type->new($context,@{$value}) unless $type eq 'value';
   34   $type = $value->typeRef;
   35 
   36   my $c = bless {
   37     value => $value, type => $type, isConstant => 1,
   38     ref => $ref, equation => $equation,
   39   }, $class;
   40   $c->weaken;
   41   $c->check;
   42   return $c;
   43 }
   44 
   45 #
   46 #  Set flags for the object
   47 #
   48 sub check {
   49   my $self = shift; my $value = $self->{value};
   50   $self->{isZero} = $value->isZero;
   51   $self->{isOne}  = $value->isOne;
   52 }
   53 
   54 #
   55 #  Return the Value object
   56 #
   57 sub eval {(shift)->{value}}
   58 
   59 #
   60 #  Call the Value object's reduce method and reset the flags
   61 #
   62 sub reduce {
   63   my $self = shift;
   64   $self->{value} = $self->{value}->reduce;
   65   $self->check;
   66   return $self;
   67 }
   68 
   69 #
   70 #  Pass on the request to the Value object
   71 #
   72 sub canBeInUnion {(shift)->{value}->canBeInUnion}
   73 
   74 #
   75 #  Return the item's list of coordinates
   76 #    (for points, vectors, matrices, etc.)
   77 #
   78 sub coords {
   79   my $self = shift;
   80   return [$self->{value}] unless $self->typeRef->{list};
   81   my @coords = (); my $equation = $self->{equation};
   82   my $value = $self->Item("Value");
   83   foreach my $x (@{$self->{value}->data}) {push(@coords,$value->new($equation,[$x]))}
   84   return [@coords];
   85 }
   86 
   87 #
   88 #  Call the appropriate formatter from Value.pm
   89 #
   90 sub string {
   91   my $self = shift; my $precedence = shift;
   92   my $string = $self->{value}->string($self->{equation},$self->{open},$self->{close},$precedence);
   93   return $string;
   94 }
   95 sub TeX {
   96   my $self = shift; my $precedence = shift;
   97   my $TeX = $self->{value}->TeX($self->{equation},$self->{open},$self->{close},$precedence);
   98   return $TeX;
   99 }
  100 sub perl {
  101   my $self = shift; my $parens = shift; my $matrix = shift;
  102   my $perl = $self->{value}->perl(0,$matrix);
  103   $perl = '('.$perl.')' if $parens;
  104   return $perl;
  105 }
  106 
  107 sub ijk {(shift)->{value}->ijk}
  108 
  109 #
  110 #  Convert the value to a Matrix object
  111 #
  112 sub makeMatrix {
  113   my $self = shift; my $context = $self->context;
  114   my ($name,$open,$close) = @_;
  115   $self->{type}{name} = $name;
  116   $self->{value} = $self->Package("Matrix",$context)->new($context,$self->{value}->value);
  117 }
  118 
  119 #
  120 #  Get a Union object's data
  121 #
  122 sub makeUnion {@{shift->{value}{data}}}
  123 
  124 #########################################################################
  125 
  126 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9