[system] / trunk / pg / macros / contextString.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/contextString.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2855 - (download) (as text) (annotate)
Sat Oct 2 11:55:31 2004 UTC (15 years, 3 months ago) by dpvc
File size: 1823 byte(s)
added generic string-only context, and converted contextABCD and
contextTF to use it.

    1 loadMacros("Parser.pl");
    2 
    3 sub _contextString_init {}; # don't load it again
    4 
    5 ##########################################################
    6 #
    7 #  Implements contexts for string-valued answers.
    8 #
    9 #  You can add new strings to the context as needed
   10 #  via the Context()->strings->add() method.  E.g.,
   11 #
   12 # Context("String")->strings->add(Foo=>{}, Bar=>{alias=>"Foo"});
   13 #
   14 #  Use string_cmp() to produce the answer checker(s) for your
   15 #  correct values.  Eg.
   16 #
   17 # ANS(string_cmp("Foo"));
   18 #
   19 #
   20 
   21 package contextString::Variable;
   22 
   23 sub new {
   24   my $self = shift; my $equation = shift;
   25   my $context = $equation->{context};
   26   my @strings = grep {not defined($context->strings->get($_)->{alias})}
   27                   $context->strings->names;
   28   my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
   29   $equation->Error("Your answer should be one of $strings");
   30 }
   31 
   32 package contextString::Formula;
   33 our @ISA = qw(Value::Formula Parser Value);
   34 
   35 sub parse {
   36   my $self = shift;
   37   foreach my $ref (@{$self->{tokens}}) {
   38     $self->{ref} = $ref;
   39     contextString::Variable->new($self) if ($ref->[0] eq 'error'); # display the error
   40   }
   41   $self->SUPER::parse(@_);
   42 }
   43 
   44 package main;
   45 
   46 $context{String} = Context("Numeric");
   47 $context{String}->parens->undefine('|','{','(','[');
   48 $context{String}->variables->clear();
   49 $context{String}->constants->clear();
   50 $context{String}->operators->clear();
   51 $context{String}->functions->clear();
   52 $context{String}->strings->clear();
   53 $context{String}->{parser}{Variable} = 'contextString::Variable';
   54 $context{String}->{parser}{Formula}  = 'contextString::Formula';
   55 
   56 Context("String");
   57 
   58 sub string_cmp {
   59   my $strings = shift;
   60   $strings = [$strings,@_] if (scalar(@_));
   61   $strings = [$strings] unless ref($strings) eq 'ARRAY';
   62   return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
   63 }
   64 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9