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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3652 - (download) (as text) (annotate)
Sat Sep 24 00:47:30 2005 UTC (14 years, 5 months ago) by dpvc
File size: 2562 byte(s)
Added ability to have answers that are empty strings.  String("") now
will produce a valid string object regardless of the Context's defined
string values.  (You can prevent this using

       Context()->flags->set(allowEmptyStrings=>0);

if you wish).  String("")->cmp will produce an answer checker for an
empty string (it removes the blank checker that WW installs).

    1 #########################################################################
    2 #
    3 #  Implements constant string values
    4 #    (Used for things like INFINITY, and so on)
    5 #
    6 package Parser::String;
    7 use strict; use vars qw(@ISA);
    8 @ISA = qw(Parser::Item);
    9 
   10 $Parser::class->{String} = 'Parser::String';
   11 
   12 #
   13 #  Mark the created word as infinity or negative infinity, and so on.
   14 #
   15 sub new {
   16   my $self = shift; my $class = ref($self) || $self;
   17   my $equation = shift;
   18   my ($value, $ref) = @_;
   19   my $def = $equation->{context}{strings}{$value};
   20   unless ($def) {
   21     $def = $equation->{context}{strings}{uc($value)};
   22     $def = {} if $def->{caseSensitive} && $value ne uc($value);
   23   }
   24   $value = $def->{alias}, $def = $equation->{context}{strings}{$value}
   25     while defined($def->{alias});
   26   my $str = bless {
   27     value => $value, type => $Value::Type{string}, isConstant => 1,
   28     def => $def, ref => $ref, equation => $equation,
   29   }, $class;
   30   $str->{isInfinite} = 1 if ($def->{infinite});
   31   $str->{isInfinity} = 1 if ($def->{infinite} && !$def->{negative});
   32   $str->{isNegativeInfinity} = 1 if ($def->{infinite} && $def->{negative});
   33   return $str;
   34 }
   35 
   36 sub newInfinity {
   37   my $self = shift; my $equation = shift; my $value = shift;
   38   my $neg = ($value =~ s/^-//);
   39   $self = $self->new($equation,$value,@_);
   40   if ($neg) {$self->{isInfinity} = 0; $self->{isNegativeInfinity} = 1}
   41   return $self;
   42 }
   43 
   44 #
   45 #  Make a Value::String or Value::Infinity object
   46 #
   47 sub eval {
   48   my $self = shift;
   49   return Value::String->make($self->{value}) unless $self->{isInfinite};
   50   my $I = Value::Infinity->new();
   51   $I = $I->neg if $self->{isNegativeInfinity};
   52   return $I;
   53 }
   54 
   55 #
   56 #  Return the replacement string if there is one,
   57 #    or let Value handle it if we can, otherwise return the string
   58 #
   59 sub string {
   60   my $self = shift;
   61   return $self->{def}{string} if defined($self->{def}{string});
   62   my $value = $self->eval;
   63   return $value unless Value::isValue($value);
   64   return $value->string($self->{equation});
   65 }
   66 
   67 #
   68 #  Typeset the value in \rm
   69 #
   70 sub TeX {
   71   my $self = shift;
   72   return $self->{def}{TeX} if defined($self->{def}{TeX});
   73   my $value = $self->eval; $value =~ s/([ _])/\\$1/g;
   74   return '{\rm '.$value.'}' unless Value::isValue($value);
   75   return $value->TeX($self->{equation});
   76 }
   77 
   78 #
   79 #  Put the value in quotes
   80 #
   81 sub perl {
   82   my $self = shift;
   83   return $self->{def}{perl} if defined($self->{def}{perl});
   84   my $value = $self->eval;
   85   return "'".$value."'" unless Value::isValue($value);
   86   return $value->perl;
   87 }
   88 
   89 #########################################################################
   90 
   91 1;
   92 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9