[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 2626 - (download) (as text) (annotate)
Mon Aug 16 19:44:26 2004 UTC (15 years, 4 months ago) by dpvc
File size: 1509 byte(s)
One more fix for handling intervals properly (I think it's really
right this time).  Also, named constants that end in numbers will
produce TeX output with the number as a subscript (this was already
true for variable names).

    1 #########################################################################
    2 #
    3 #  Implements named constants (e, pi, etc.)
    4 #
    5 package Parser::Constant;
    6 use strict; use vars qw(@ISA);
    7 @ISA = qw(Parser::Item);
    8 
    9 #
   10 #  If a constant is marked with isConstant, then it will
   11 #  be combined with other constants automatically as formulas
   12 #  are built, to only mark it if you want that to happen.
   13 #
   14 sub new {
   15   my $self = shift; my $class = ref($self) || $self;
   16   my $equation = shift;
   17   my ($name,$ref) = @_;
   18   my $const = $equation->{context}{constants}{$name};
   19   my ($value,$type) = Value::getValueType($equation,$const->{value});
   20   my $c = bless {
   21     name => $name, constant => $value, type => $type,
   22     def => $const, ref => $ref, equation => $equation
   23   }, $class;
   24   $c->{isConstant} = 1 if $const->{isConstant};
   25   return $c;
   26 }
   27 
   28 #
   29 #  Return the value of the constant
   30 #
   31 sub eval {
   32   my $self = shift; my $data = $self->{constant};
   33   return $data unless ref($data) eq 'ARRAY';
   34   return @{$data};
   35 }
   36 
   37 #
   38 #  Return the constant's name
   39 #
   40 sub string {(shift)->{name}}
   41 
   42 sub TeX {
   43   my $self = shift; my $name = $self->{name};
   44   return $self->{def}{TeX} if defined($self->{def}{TeX});
   45   $name = $1.'_{'.$2.'}' if ($name =~ m/^(\D+)(\d+)$/);
   46   return $name;
   47 }
   48 
   49 sub perl {
   50   my $self = shift;
   51   return $self->{def}{perl} if defined($self->{def}{perl});
   52   return $self->{constant}->perl(@_) if ref($self->{constant});
   53   return '$'.$self->{name};
   54 }
   55 
   56 #########################################################################
   57 
   58 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9