[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 5373 - (download) (as text) (annotate)
Sun Aug 19 02:01:57 2007 UTC (12 years, 3 months ago) by dpvc
File size: 1898 byte(s)
Normalized comments and headers to that they will format their POD
documentation properly.  (I know that the POD processing was supposed
to strip off the initial #, but that doesn't seem to happen, so I've
added a space throughout.)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9