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

View of /trunk/pg/lib/Value/String.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2800 - (download) (as text) (annotate)
Sun Sep 19 14:27:39 2004 UTC (15 years, 5 months ago) by dpvc
File size: 1574 byte(s)
Added isZero and isOne checks for Parser::Value objects (i.e., for
constants within formulas).  These now correctly handle vector and
matrices, in particular.  The isOne and isZero checks are used in the
reduce() method to simplify formulas.

    1 ###########################################################################
    2 
    3 package Value::String;
    4 my $pkg = 'Value::String';
    5 
    6 use strict;
    7 use vars qw(@ISA);
    8 @ISA = qw(Value);
    9 
   10 use overload
   11        '.'   => \&Value::_dot,
   12        '<=>' => \&compare,
   13        'cmp' => \&compare,
   14   'nomethod' => \&Value::nomethod,
   15         '""' => \&Value::stringify;
   16 
   17 #
   18 #  Create a string object
   19 #
   20 sub new {
   21   my $self = shift; my $class = ref($self) || $self;
   22   my $x = join('',@_);
   23   if ($Parser::installed) {
   24     Value::Error("Unrecognized string '$x'")
   25       unless $$Value::context->{strings}{$x};
   26   }
   27   bless {data => [$x]}, $class;
   28 }
   29 
   30 #
   31 #  Return the appropriate data.
   32 #
   33 sub length {1}
   34 sub typeRef {$Value::Type{string}}
   35 sub value {shift->{data}[0]}
   36 
   37 sub isOne {0}
   38 sub isZero {0}
   39 
   40 ##################################################
   41 
   42 #
   43 #  Convert to a string object
   44 #
   45 sub promote {
   46   my $x = shift; $x = [$x,@_] if scalar(@_) > 0;
   47   $x = Value::makeValue($x); $x = join('',@{$x}) if ref($x) eq 'ARRAY';
   48   $x = $pkg->make($x) unless Value::isValue($x);
   49   return $x;
   50 }
   51 
   52 ############################################
   53 #
   54 #  Operations on strings
   55 #
   56 
   57 sub compare {
   58   my ($l,$r,$flag) = @_;
   59   if ($l->promotePrecedence($r)) {return $r->compare($l,!$flag)}
   60   $r = promote($r); if ($flag) {my $tmp = $l; $l = $r; $r = $tmp}
   61   return $l->value cmp $r->value;
   62 }
   63 
   64 ############################################
   65 #
   66 #  Generate the various output formats
   67 #
   68 
   69 sub TeX {'{\rm '.shift->value.'}'}
   70 sub perl {"'".shift->value."'"}
   71 
   72 ###########################################################################
   73 
   74 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9