[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 3466 - (download) (as text) (annotate)
Thu Aug 11 14:19:26 2005 UTC (7 years, 10 months ago) by dpvc
File size: 3409 byte(s)
Added new Set object class to the Parser.  It implements a finite set
of real numbers, for use with unions and intervals.  E.g., (1,2) U {3}
or (1,2) U {3,4,5}.  You can created Set objects in your perl code via
the Set() command, e.g, Set(3,4,5) or Set("{1,2,3}").  You should set
the Context to Context("Interval") if you plan to use Set objects, as
this defined the braces to form sets (rather than using them as
parentheses, which is the default WW behavior).  Note that in Interval
context, you can NOT use braces as parentheses.

Current, Set objects are only allowed to be sets of numbers.  It would
be possible to extend that in the future.

    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 $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 $parser = $equation->{context}{parser};
   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 $parser->{String}->new($equation,$value,$ref) if ($type eq 'String');
   27   return $parser->{String}->newInfinity($equation,$value,$ref) if ($type eq 'Infinity');
   28   return $parser->{Number}->new($equation,$value,$ref) if ($type eq 'Number');
   29   return $parser->{Number}->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 = 'Value::'.$type, $value = $type->new(@{$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->{canBeInterval} = 1
   41     if $value->{canBeInterval} ||
   42        ($value->class =~ m/Point|List/ &&
   43         $type->{length} == 2 && $type->{entryType}{name} eq 'Number');
   44 
   45   $c->{isZero} = $value->isZero;
   46   $c->{isOne}  = $value->isOne;
   47   return $c;
   48 }
   49 
   50 #
   51 #  Return the Value.pm object
   52 #
   53 sub eval {return (shift)->{value}}
   54 
   55 #
   56 #  Return the item's list of coordinates
   57 #    (for points, vectors, matrices, etc.)
   58 #
   59 sub coords {
   60   my $self = shift;
   61   return [$self->{value}] unless $self->typeRef->{list};
   62   my @coords = (); my $equation = $self->{equation};
   63   foreach my $x (@{$self->{value}->data})
   64     {push(@coords,$equation->{context}{parser}{Value}->new($equation,[$x]))}
   65   return [@coords];
   66 }
   67 
   68 #
   69 #  Call the appropriate formatter from Value.pm
   70 #
   71 sub string {
   72   my $self = shift; my $precedence = shift;
   73   my $string = $self->{value}->string($self->{equation},$self->{open},$self->{close},$precedence);
   74   return $string;
   75 }
   76 sub TeX {
   77   my $self = shift; my $precedence = shift;
   78   my $TeX = $self->{value}->TeX($self->{equation},$self->{open},$self->{close},$precedence);
   79   return $TeX;
   80 }
   81 sub perl {
   82   my $self = shift; my $parens = shift; my $matrix = shift;
   83   my $perl = $self->{value}->perl(0,$matrix);
   84   $perl = "(($perl)->with(open=>'$self->{open}',close=>'$self->{close}'))"
   85     if $self->{canBeInterval} && $self->{open}.$self->{close} eq '[]';
   86   $perl = '('.$perl.')' if $parens;
   87   return $perl;
   88 }
   89 
   90 sub ijk {(shift)->{value}->ijk}
   91 
   92 #
   93 #  Convert the value to a Matrix object
   94 #
   95 sub makeMatrix {
   96   my $self = shift;
   97   my ($name,$open,$close) = @_;
   98   $self->{type}{name} = $name;
   99   $self->{value} = Value::Matrix->new($self->{value}->value);
  100 }
  101 
  102 #
  103 #  Get a Union object's data
  104 #
  105 sub makeUnion {@{shift->{value}{data}}}
  106 
  107 #########################################################################
  108 
  109 1;
  110 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9