[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 2622 - (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 :     # The types that variables can be
12 :     # @@@ Should also include domain ranges for when
13 :     # we use these in answer checkers @@@
14 :     #
15 :     %type = (
16 :     'Real' => $Value::Type{number},
17 :     'Complex' => $Value::Type{complex},
18 :     );
19 :    
20 :     sub init {
21 :     my $self = shift;
22 :     $self->{dataName} = 'variables';
23 :     $self->{name} = 'variable';
24 :     $self->{Name} = 'Variable';
25 :     $self->{namePattern} = '[a-zA-Z]';
26 :     }
27 :    
28 :     #
29 :     # Our pattern should match ANY variable name
30 :     # (Parser takes care of reporting unknown ones)
31 :     #
32 :     sub update {
33 :     my $self = shift;
34 :     $self->{pattern} = $self->{namePattern};
35 :     }
36 :    
37 :     #
38 :     # If the type is one of the names ones, use it's known type
39 :     # Otherwise if it is a Value object use its type,
40 :     # Otherwise, if it is a signed number, use the Real type
41 :     # Otherwise report an error
42 :     #
43 :     sub create {
44 :     my $self = shift; my $value = shift;
45 : dpvc 2622 return $value if ref($value) eq 'HASH';
46 : sh002i 2558 if (defined($type{$value})) {
47 :     $value = $type{$value};
48 :     } elsif (Value::isValue($value)) {
49 :     $value = $value->typeRef;
50 :     } elsif ($value =~ m/$self->{context}{pattern}{signedNumber}/) {
51 :     $value = $type{'Real'};
52 :     } else {
53 :     Value::Error("Unrecognized variable type '$value'");
54 :     }
55 :     return {type => $value};
56 :     }
57 :    
58 :     #
59 :     # Return a variable's type
60 :     #
61 :     sub type {
62 :     my $self = shift; my $x = shift;
63 :     return $self->{context}{variables}{$x}{type};
64 :     }
65 :    
66 :     #########################################################################
67 :    
68 :     1;
69 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9