[system] / trunk / pg / macros / contextTF.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/contextTF.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2824 - (download) (as text) (annotate)
Thu Sep 23 21:31:41 2004 UTC (15 years, 2 months ago) by dpvc
File size: 2068 byte(s)
Two new Parser contexts, one for matching problems involving answers
like A, B, C and D, the other for T/F answers.  These contexts have an
advantage over the standard str_cmp() checker in that it will give
additional error messages when the student enters an unrecognized answer.

    1 loadMacros("Parser.pl");
    2 
    3 sub _contextTF_init {}; # don't load it again
    4 
    5 ##########################################################
    6 #
    7 #  Implements contexts for string-valued answers especially
    8 #  for matching problems (where you match against T and F).
    9 #
   10 # Context("TF");
   11 #
   12 #  You can add new strings to the context as needed (or remove old ones)
   13 #  via the Context()->strings->add() and Context()-strings->remove()
   14 #  methods
   15 #
   16 # ANS(TF_cmp("T","F"));
   17 #
   18 #  when there are two answers, the first being "T" and the second being "F".
   19 #
   20 
   21 package contextTF::Variable;
   22 
   23 sub new {
   24   my $self = shift; my $equation = shift;
   25   my $context = $equation->{context};
   26   my @strings = grep {not defined($context->strings->get($_)->{alias})}
   27                   $context->strings->names;
   28   my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
   29   $equation->Error("Your answer should be one of $strings");
   30 }
   31 
   32 package contextTF::Formula;
   33 our @ISA = qw(Value::Formula Parser Value);
   34 
   35 sub parse {
   36   my $self = shift;
   37   foreach my $ref (@{$self->{tokens}}) {
   38     $self->{ref} = $ref;
   39     contextTF::Variable->new($self) if ($ref->[0] eq 'error'); # display the error
   40   }
   41   $self->SUPER::parse(@_);
   42 }
   43 
   44 package main;
   45 
   46 $context{TF} = Context("Numeric");
   47 $context{TF}->parens->undefine('|','{','(','[');
   48 $context{TF}->variables->clear();
   49 $context{TF}->constants->clear();
   50 $context{TF}->operators->clear();
   51 $context{TF}->functions->clear();
   52 $context{TF}->strings->are(
   53  "T" => {value => 1}, "t" => {alias => "T"},
   54  "F" => {value => 0}, "f" => {alias => "F"},
   55  "True" => {alias => "T"}, "False" => {alias => "F"},
   56  "TRUE" => {alias => "T"}, "FALSE" => {alias => "F"},
   57  "true" => {alias => "T"}, "false" => {alias => "F"},
   58 );
   59 $context{'TF'}->{parser}{Variable} = 'contextTF::Variable';
   60 $context{'TF'}->{parser}{Formula}  = 'contextTF::Formula';
   61 
   62 Context("TF");
   63 
   64 sub TF_cmp {
   65   my $strings = shift;
   66   $strings = [$strings,@_] if (scalar(@_));
   67   $strings = [$strings] unless ref($strings) eq 'ARRAY';
   68   return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
   69 }
   70 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9