[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 2658 - (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 : dpvc 2658 $self->{pattern} = $self->{namePattern};
27 : sh002i 2558 }
28 :    
29 :     #
30 :     # Our pattern should match ANY variable name
31 :     # (Parser takes care of reporting unknown ones)
32 :     #
33 :     sub update {
34 :     my $self = shift;
35 :     $self->{pattern} = $self->{namePattern};
36 :     }
37 :    
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 :     my $self = shift; my $value = shift;
46 : dpvc 2622 return $value if ref($value) eq 'HASH';
47 : sh002i 2558 if (defined($type{$value})) {
48 :     $value = $type{$value};
49 :     } elsif (Value::isValue($value)) {
50 :     $value = $value->typeRef;
51 :     } elsif ($value =~ m/$self->{context}{pattern}{signedNumber}/) {
52 :     $value = $type{'Real'};
53 :     } else {
54 :     Value::Error("Unrecognized variable type '$value'");
55 :     }
56 :     return {type => $value};
57 :     }
58 :    
59 :     #
60 :     # Return a variable's type
61 :     #
62 :     sub type {
63 :     my $self = shift; my $x = shift;
64 :     return $self->{context}{variables}{$x}{type};
65 :     }
66 :    
67 :     #########################################################################
68 :    
69 :     1;
70 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9