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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2558 - (download) (as text) (annotate)
Wed Jul 28 20:32:33 2004 UTC (15 years, 6 months ago) by sh002i
File size: 1599 byte(s)
merged changes from rel-2-1-a1 -- stop using that branch.

    1 #########################################################################
    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 @ISA = qw(Parser::Context::Data);
    9 
   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   if (defined($type{$value})) {
   46     $value = $type{$value};
   47   } elsif (Value::isValue($value)) {
   48     $value = $value->typeRef;
   49   } elsif ($value =~ m/$self->{context}{pattern}{signedNumber}/) {
   50     $value = $type{'Real'};
   51   } else {
   52     Value::Error("Unrecognized variable type '$value'");
   53   }
   54   return {type => $value};
   55 }
   56 
   57 #
   58 #  Return a variable's type
   59 #
   60 sub type {
   61   my $self = shift; my $x = shift;
   62   return $self->{context}{variables}{$x}{type};
   63 }
   64 
   65 #########################################################################
   66 
   67 1;
   68 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9