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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9