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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5001 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9