[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 4806 - (download) (as text) (annotate)
Thu Mar 1 22:43:38 2007 UTC (12 years, 11 months ago) by gage
File size: 1853 byte(s)
Minor documentation changes.
Use html entity to represent dollar sign on the web.

    1 loadMacros("Parser.pl");
    2 
    3 sub _contextString_init {}; # don't load it again
    4 
    5 =head1 DESCRIPTION
    6 ##########################################################
    7 #
    8 #  Implements contexts for string-valued answers.
    9 #
   10 #  You can add new strings to the context as needed
   11 #  via the Context()->strings->add() method.  E.g.,
   12 #
   13 # Context("String")->strings->add(Foo=>{}, Bar=>{alias=>"Foo"});
   14 #
   15 #  Use string_cmp() to produce the answer checker(s) for your
   16 #  correct values.  Eg.
   17 #
   18 # ANS(string_cmp("Foo"));
   19 #
   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} = Context("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 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9