[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 5551 - (download) (as text) (annotate)
Tue Oct 2 20:48:05 2007 UTC (12 years, 2 months ago) by sh002i
File size: 2644 byte(s)
improved formatting for docs -- these were in pod sections but were all
formatted as verbatim sections, and i moved them into normal paragraphs,
lists, etc. should make things more readable from the web.

    1 =head1 NAME
    2 
    3 contextString.pl - Allow string-valued answers.
    4 
    5 =head1 DESCRIPTION
    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 =cut
   20 
   21 loadMacros("MathObjects.pl");
   22 
   23 sub _contextString_init {context::String::Init()}; # don't load it again
   24 
   25 ##################################################
   26 
   27 package context::String::Variable;
   28 
   29 sub new {
   30   my $self = shift; my $equation = shift;
   31   my $context = $equation->{context};
   32   my @strings = grep {not defined($context->strings->get($_)->{alias})}
   33                   $context->strings->names;
   34   my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
   35   $equation->Error(["Your answer should be one of %s",$strings]);
   36 }
   37 
   38 ##################################################
   39 
   40 package context::String::Formula;
   41 our @ISA = qw(Value::Formula);
   42 
   43 sub parse {
   44   my $self = shift;
   45   foreach my $ref (@{$self->{tokens}}) {
   46     $self->{ref} = $ref;
   47     context::String::Variable->new($self->{equation}) if ($ref->[0] eq 'error'); # display the error
   48   }
   49   $self->SUPER::parse(@_);
   50 }
   51 
   52 package context::String::BOP::mult;
   53 our @ISA = qw(Parser::BOP);
   54 
   55 sub _check {
   56   my $self = shift;
   57   context::String::Variable->new($self->{equation}); # report an error
   58 }
   59 
   60 ##################################################
   61 
   62 package context::String;
   63 
   64 sub Init {
   65   my $context = $main::context{String} = Parser::Context->getCopy("Numeric");
   66   $context->{name} = "String";
   67   $context->parens->clear();
   68   $context->variables->clear();
   69   $context->constants->clear();
   70   $context->operators->clear();
   71   $context->functions->clear();
   72   $context->strings->clear();
   73   $context->{parser}{Variable} = 'context::String::Variable';
   74   $context->{parser}{Formula}  = 'context::String::Formula';
   75   $context->operators->add(
   76     ' ' => {precedence => 3, associativity=>"left", type=>"bin", string => "*", class => 'context::String::BOP::mult'},
   77     '*' => {precedence => 3, associativity=>"left", type=>"bin", class => 'context::String::BOP::mult'}
   78   );
   79 
   80   main::PG_restricted_eval(<<'  END_EVAL');
   81     sub string_cmp {
   82       my $strings = shift;
   83       $strings = [$strings,@_] if (scalar(@_));
   84       $strings = [$strings] unless ref($strings) eq 'ARRAY';
   85       return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
   86     }
   87   END_EVAL
   88 
   89   main::Context("String");  ### FIXME:  probably should require author to set this explicitly
   90 }
   91 
   92 1;
   93 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9