[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 4997 - (download) (as text) (annotate)
Mon Jun 11 18:16:40 2007 UTC (12 years, 8 months ago) by gage
File size: 1860 byte(s)
Fixing docementation so that it can be read from the web.

    1 loadMacros("Parser.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 
   23 =cut
   24 
   25 package contextString::Variable;
   26 
   27 sub new {
   28   my $self = shift; my $equation = shift;
   29   my $context = $equation->{context};
   30   my @strings = grep {not defined($context->strings->get($_)->{alias})}
   31                   $context->strings->names;
   32   my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
   33   $equation->Error(["Your answer should be one of %s",$strings]);
   34 }
   35 
   36 package contextString::Formula;
   37 our @ISA = qw(Value::Formula Parser Value);
   38 
   39 sub parse {
   40   my $self = shift;
   41   foreach my $ref (@{$self->{tokens}}) {
   42     $self->{ref} = $ref;
   43     contextString::Variable->new($self) if ($ref->[0] eq 'error'); # display the error
   44   }
   45   $self->SUPER::parse(@_);
   46 }
   47 
   48 package main;
   49 
   50 $context{String} = Context("Numeric");
   51 $context{String}->parens->undefine('|','{','(','[');
   52 $context{String}->variables->clear();
   53 $context{String}->constants->clear();
   54 $context{String}->operators->clear();
   55 $context{String}->functions->clear();
   56 $context{String}->strings->clear();
   57 $context{String}->{parser}{Variable} = 'contextString::Variable';
   58 $context{String}->{parser}{Formula}  = 'contextString::Formula';
   59 
   60 Context("String");
   61 
   62 sub string_cmp {
   63   my $strings = shift;
   64   $strings = [$strings,@_] if (scalar(@_));
   65   $strings = [$strings] unless ref($strings) eq 'ARRAY';
   66   return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
   67 }
   68 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9