[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 2666 - (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 : sh002i 2558 );
23 :    
24 :     sub init {
25 :     my $self = shift;
26 :     $self->{dataName} = 'variables';
27 :     $self->{name} = 'variable';
28 :     $self->{Name} = 'Variable';
29 :     $self->{namePattern} = '[a-zA-Z]';
30 : dpvc 2658 $self->{pattern} = $self->{namePattern};
31 : sh002i 2558 }
32 :    
33 :     #
34 :     # Our pattern should match ANY variable name
35 :     # (Parser takes care of reporting unknown ones)
36 :     #
37 :     sub update {
38 :     my $self = shift;
39 :     $self->{pattern} = $self->{namePattern};
40 :     }
41 :    
42 :     #
43 :     # If the type is one of the names ones, use it's known type
44 :     # Otherwise if it is a Value object use its type,
45 :     # Otherwise, if it is a signed number, use the Real type
46 :     # Otherwise report an error
47 :     #
48 :     sub create {
49 :     my $self = shift; my $value = shift;
50 : dpvc 2622 return $value if ref($value) eq 'HASH';
51 : sh002i 2558 if (defined($type{$value})) {
52 :     $value = $type{$value};
53 :     } elsif (Value::isValue($value)) {
54 :     $value = $value->typeRef;
55 :     } elsif ($value =~ m/$self->{context}{pattern}{signedNumber}/) {
56 :     $value = $type{'Real'};
57 :     } else {
58 :     Value::Error("Unrecognized variable type '$value'");
59 :     }
60 :     return {type => $value};
61 :     }
62 :    
63 :     #
64 :     # Return a variable's type
65 :     #
66 :     sub type {
67 :     my $self = shift; my $x = shift;
68 :     return $self->{context}{variables}{$x}{type};
69 :     }
70 :    
71 :     #########################################################################
72 :    
73 :     1;
74 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9