[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 5386 - (download) (as text) (annotate)
Sun Aug 19 20:38:30 2007 UTC (12 years, 6 months ago) by dpvc
File size: 2649 byte(s)
Move the context creation into the _init routine.

Don't generate the error about implied multiplication, since that
doesn't make sense here (produce the standard error instead).

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9