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

View of /trunk/pg/macros/contextABCD.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: 2537 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 _contextABCD_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 A, B, C, D,
    9 #  and so on).
   10 #
   11 #  There are two contexts defined here,
   12 #
   13 # Context("ABCD");
   14 # Context("ABCD-List");
   15 #
   16 #  The second allows the students to enter lists of strings,
   17 #  while the first does not.
   18 #
   19 #  You can add new strings to the context as needed (or remove old ones)
   20 #  via the Context()->strings->add() and Context()-strings->remove()
   21 #  methods, eg.
   22 #
   23 # Context("ABCD-List")->strings->add(E=>{},e=>{alias=>"E"});
   24 #
   25 #  Use string_cmp() to produce the answer checker(s) for your
   26 #  correct values.  Eg.
   27 #
   28 # ANS(string_cmp("A","B"));
   29 #
   30 #  when there are two answers, the first being "A" and the second being "B".
   31 #
   32 
   33 package contextABCD::Variable;
   34 
   35 sub new {
   36   my $self = shift; my $equation = shift;
   37   my $context = $equation->{context};
   38   my @strings = grep {not defined($context->strings->get($_)->{alias})}
   39                   $context->strings->names;
   40   my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
   41   $equation->Error("Your answer should be one of $strings");
   42 }
   43 
   44 package contextABCD::Formula;
   45 our @ISA = qw(Value::Formula Parser Value);
   46 
   47 sub parse {
   48   my $self = shift;
   49   foreach my $ref (@{$self->{tokens}}) {
   50     $self->{ref} = $ref;
   51     contextABCD::Variable->new($self) if ($ref->[0] eq 'error'); # display the error
   52   }
   53   $self->SUPER::parse(@_);
   54 }
   55 
   56 package main;
   57 
   58 $context{ABCD} = Context("Numeric");
   59 $context{ABCD}->parens->undefine('|','{','(','[');
   60 $context{ABCD}->variables->clear();
   61 $context{ABCD}->constants->clear();
   62 $context{ABCD}->operators->clear();
   63 $context{ABCD}->functions->clear();
   64 $context{ABCD}->strings->are(
   65  "A" => {}, "a" => {alias => "A"},
   66  "B" => {}, "b" => {alias => "B"},
   67  "C" => {}, "c" => {alias => "C"},
   68  "D" => {}, "d" => {alias => "D"},
   69 );
   70 $context{'ABCD'}->{parser}{Variable} = 'contextABCD::Variable';
   71 $context{'ABCD'}->{parser}{Formula}  = 'contextABCD::Formula';
   72 
   73 $context{'ABCD-List'} = $context{ABCD}->copy;
   74 $context{'ABCD-List'}->operators->add(
   75   ',' => $Parser::Context::Default::fullContext->operators->get(','),
   76 );
   77 $context{'ABCD-List'}->strings->add("NONE"=>{});
   78 
   79 Context("ABCD");
   80 
   81 sub string_cmp {
   82   my $strings = shift;
   83   $strings = [$strings,@_] if (scalar(@_));
   84   $strings = [$strings] unless ref($strings) eq 'ARRAY';
   85   return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
   86 }
   87 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9