[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 2658 - (download) (as text) (annotate)
Fri Aug 20 11:17:06 2004 UTC (15 years, 5 months ago) by dpvc
File size: 1683 byte(s)
Make blank patterns be ^$ (which should never match) rather than empty
(which always matches).  this fixes a problem where if a context has
no functions, for example, the parser would attempt to create function
calls to a function named ''.

    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(Value::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   $self->{pattern} = $self->{namePattern};
   27 }
   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   return $value if ref($value) eq 'HASH';
   47   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