[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 6058 - (download) (as text) (annotate)
Thu Jun 25 23:28:44 2009 UTC (10 years, 7 months ago) by gage
File size: 3554 byte(s)
syncing pg HEAD with pg2.4.7 on 6/25/2009

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader$
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 contextString.pl - Allow string-valued answers.
   20 
   21 =head1 DESCRIPTION
   22 
   23 Implements contexts for string-valued answers.
   24 
   25 You can add new strings to the context as needed
   26 via the Context()->strings->add() method.  E.g.,
   27 
   28   Context("String")->strings->add(Foo=>{}, Bar=>{alias=>"Foo"});
   29 
   30 Use string_cmp() to produce the answer checker(s) for your
   31 correct values.  Eg.
   32 
   33   ANS(string_cmp("Foo"));
   34 
   35 =cut
   36 
   37 loadMacros("MathObjects.pl");
   38 
   39 sub _contextString_init {context::String::Init()}; # don't load it again
   40 
   41 ##################################################
   42 
   43 package context::String::Variable;
   44 
   45 sub new {
   46   my $self = shift; my $equation = shift;
   47   my $context = $equation->{context};
   48   my @strings = grep {not defined($context->strings->get($_)->{alias})}
   49                   $context->strings->names;
   50   my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
   51   $equation->Error(["Your answer should be one of %s",$strings]);
   52 }
   53 
   54 ##################################################
   55 
   56 package context::String::Formula;
   57 our @ISA = qw(Value::Formula);
   58 
   59 sub parse {
   60   my $self = shift;
   61   foreach my $ref (@{$self->{tokens}}) {
   62     $self->{ref} = $ref;
   63     context::String::Variable->new($self->{equation}) if ($ref->[0] eq 'error'); # display the error
   64   }
   65   $self->SUPER::parse(@_);
   66 }
   67 
   68 package context::String::BOP::mult;
   69 our @ISA = qw(Parser::BOP);
   70 
   71 sub _check {
   72   my $self = shift;
   73   context::String::Variable->new($self->{equation}); # report an error
   74 }
   75 
   76 ##################################################
   77 
   78 package context::String;
   79 
   80 sub Init {
   81   my $context = $main::context{String} = Parser::Context->getCopy("Numeric");
   82   $context->{name} = "String";
   83   $context->{pattern}{number} = qr/^$/;
   84   $context->parens->clear();
   85   $context->variables->clear();
   86   $context->constants->clear();
   87   $context->operators->clear();
   88   $context->functions->clear();
   89   $context->strings->clear();
   90   $context->{parser}{Variable} = 'context::String::Variable';
   91   $context->{parser}{Formula}  = 'context::String::Formula';
   92   $context->operators->add(
   93     ' ' => {precedence => 3, associativity=>"left", type=>"bin", string => "*", class => 'context::String::BOP::mult'},
   94     '*' => {precedence => 3, associativity=>"left", type=>"bin", class => 'context::String::BOP::mult'}
   95   );
   96 
   97   main::PG_restricted_eval(<<'  END_EVAL');
   98     sub string_cmp {
   99       my $strings = shift;
  100       $strings = [$strings,@_] if (scalar(@_));
  101       $strings = [$strings] unless ref($strings) eq 'ARRAY';
  102       return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
  103     }
  104   END_EVAL
  105 
  106   main::Context("String");  ### FIXME:  probably should require author to set this explicitly
  107 }
  108 
  109 1;
  110 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9