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

Annotation of /trunk/pg/lib/Parser/Context/Variables.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : sh002i 2558 #########################################################################
2 :     #
3 :     # Implements the list of known variables and their types
4 :     #
5 :     package Parser::Context::Variables;
6 :     use strict;
7 : dpvc 4994 our @ISA = qw(Value::Context::Data);
8 : sh002i 2558
9 :     #
10 : dpvc 2666 # The named types for variables
11 :     # (you can use arbitary types by supplying an
12 :     # instance of the type rather than a name)
13 : sh002i 2558 #
14 : dpvc 4994 our %type = (
15 : sh002i 2558 'Real' => $Value::Type{number},
16 :     'Complex' => $Value::Type{complex},
17 : dpvc 2666 'Point2D' => Value::Type('Point',2,$Value::Type{number}),
18 :     'Point3D' => Value::Type('Point',3,$Value::Type{number}),
19 :     'Vector2D' => Value::Type('Vector',2,$Value::Type{number}),
20 :     'Vector3D' => Value::Type('Vector',3,$Value::Type{number}),
21 : dpvc 2687 'Parameter' => $Value::Type{number},
22 : sh002i 2558 );
23 :    
24 :     sub init {
25 :     my $self = shift;
26 :     $self->{dataName} = 'variables';
27 :     $self->{name} = 'variable';
28 :     $self->{Name} = 'Variable';
29 : dpvc 5124 $self->{namePattern} = qr/[a-zA-Z][a-zA-Z0-9]*/;
30 : dpvc 5116 $self->{tokenType} = 'var';
31 :     $self->{precedence} = 5;
32 :     $self->{patterns}{$self->{namePattern}} = [$self->{precedence},$self->{tokenType}];
33 : sh002i 2558 }
34 :    
35 : dpvc 5116 #sub addToken {} # no tokens needed
36 :     #sub removeToken {}
37 : sh002i 2558
38 :     #
39 :     # If the type is one of the names ones, use it's known type
40 :     # Otherwise if it is a Value object use its type,
41 :     # Otherwise, if it is a signed number, use the Real type
42 :     # Otherwise report an error
43 :     #
44 :     sub create {
45 : dpvc 2687 my $self = shift; my $value = shift; my @extra;
46 : dpvc 2622 return $value if ref($value) eq 'HASH';
47 : dpvc 5237 ($value,@extra) = @{$value} if ref($value) eq 'ARRAY';
48 : sh002i 2558 if (defined($type{$value})) {
49 : dpvc 5237 push(@extra,(parameter => 1)) if $value eq 'Parameter';
50 : sh002i 2558 $value = $type{$value};
51 :     } elsif (Value::isValue($value)) {
52 :     $value = $value->typeRef;
53 :     } elsif ($value =~ m/$self->{context}{pattern}{signedNumber}/) {
54 :     $value = $type{'Real'};
55 :     } else {
56 : dpvc 3370 Value::Error("Unrecognized variable type '%s'",$value);
57 : sh002i 2558 }
58 : dpvc 2687 return {type => $value, @extra};
59 : sh002i 2558 }
60 : dpvc 3510 sub uncreate {shift; (shift)->{type}};
61 : sh002i 2558
62 :     #
63 :     # Return a variable's type
64 :     #
65 :     sub type {
66 :     my $self = shift; my $x = shift;
67 :     return $self->{context}{variables}{$x}{type};
68 :     }
69 :    
70 : dpvc 2687 #
71 : dpvc 5116 # Return a parameter's value
72 :     #
73 :     sub value {
74 :     my $self = shift; my $x = shift;
75 :     return $self->{context}{variables}{$x}{value};
76 :     }
77 :    
78 :     #
79 : dpvc 2687 # Get the names of all variables
80 :     #
81 :     sub variables {
82 :     my $self = shift; my @names;
83 :     foreach my $x ($self->SUPER::names)
84 :     {push(@names,$x) unless $self->{context}{variables}{$x}{parameter}}
85 :     return @names;
86 :     }
87 :    
88 :     #
89 :     # Get the names of all parameters
90 :     #
91 :     sub parameters {
92 :     my $self = shift; my @names;
93 :     foreach my $x ($self->SUPER::names)
94 :     {push(@names,$x) if $self->{context}{variables}{$x}{parameter}}
95 :     return @names;
96 :     }
97 :    
98 : sh002i 2558 #########################################################################
99 :    
100 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9