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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5120 - (download) (as text) (annotate)
Wed Jul 4 17:00:18 2007 UTC (12 years, 5 months ago) by dpvc
File size: 2560 byte(s)
Allow constants to override their string representations.

    1 #########################################################################
    2 #
    3 #  Implements named constants (e, pi, etc.)
    4 #
    5 package Parser::Constant;
    6 use strict;
    7 our @ISA = qw(Parser::Item);
    8 
    9 $Parser::class->{Constant} = 'Parser::Constant';
   10 
   11 #
   12 #  If a constant is marked with isConstant, then it will
   13 #  be combined with other constants automatically as formulas
   14 #  are built, so only mark it if you want that to happen.
   15 #
   16 sub new {
   17   my $self = shift; my $class = ref($self) || $self;
   18   my $equation = shift;
   19   my ($name,$ref) = @_;
   20   my $const = $equation->{context}{constants}{$name};
   21   my ($value,$type) = Value::getValueType($equation,$const->{value});
   22   my $c = bless {
   23     name => $name, type => $type, def => $const,
   24     ref => $ref, equation => $equation
   25   }, $class;
   26   $c->{isConstant} = 1 if $const->{isConstant};
   27   return $c;
   28 }
   29 
   30 #
   31 #  Return the value of the constant
   32 #    (for formulas, do the same substitutions the are in
   33 #     effect for the main equation).
   34 #
   35 sub eval {
   36   my $self = shift; my $context = $self->context;
   37   my $data = $self->{def}{value};
   38   if (Value::isFormula($data)) {
   39     $data->{values} = $self->{equation}{values};
   40     my $value = $data->{tree}->eval;
   41     $data->{values} = {};
   42     return $value->inContext($self->context);
   43   } elsif (ref($data) eq 'ARRAY') {
   44     foreach my $x (@{$data}) {$x->inContext($context)}
   45     return @{$data};
   46   } else {
   47     $data = $data->inContext($context) if Value::isValue($data);
   48     return $data;
   49   }
   50 }
   51 
   52 #
   53 #  Use constant to tell if it can be in a union
   54 #
   55 sub canBeInUnion {
   56   my $self = shift;
   57   Value::isValue($self->{def}{value}) && $self->{def}{value}->canBeInUnion;
   58 }
   59 
   60 #
   61 #  Return the constant's name
   62 #
   63 sub string {
   64   my $self = shift;
   65   return $self->{def}{string} if defined($self->{def}{string});
   66   return $self->{name}
   67 }
   68 
   69 sub TeX {
   70   my $self = shift; my $name = $self->{name};
   71   return $self->{def}{TeX} if defined($self->{def}{TeX});
   72   $name = $1.'_{'.$2.'}' if ($name =~ m/^(\D+)(\d+)$/);
   73   return $name;
   74 }
   75 
   76 sub perl {
   77   my $self = shift; my $parens = shift;
   78   my $data = $self->{def}{value};
   79   return $self->{def}{perl} if defined($self->{def}{perl});
   80   if (Value::isFormula($data)) {
   81     $data->{values} = $self->{equation}{values};
   82     my $value = $data->{tree}->perl;
   83     $data->{values} = {};
   84     $value = '('.$value.')' if $parens;
   85     return $value;
   86   }
   87   $data = Value::makeValue($data,context=>$self->context);
   88   return $data->perl(@_) if Value::isValue($data);
   89   return '$'.$self->{name};
   90 }
   91 
   92 #########################################################################
   93 
   94 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9