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

Annotation of /trunk/pg/macros/contextString.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6058 - (view) (download) (as text)

1 : sh002i 5556 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4 : gage 6058 # $CVSHeader$
5 : sh002i 5556 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 :     ################################################################################
16 :    
17 : sh002i 5551 =head1 NAME
18 : dpvc 2855
19 : sh002i 5551 contextString.pl - Allow string-valued answers.
20 : dpvc 2855
21 : sh002i 5551 =head1 DESCRIPTION
22 : gage 4997
23 : sh002i 5551 Implements contexts for string-valued answers.
24 : dpvc 2855
25 : sh002i 5551 You can add new strings to the context as needed
26 :     via the Context()->strings->add() method. E.g.,
27 :    
28 : sh002i 5555 Context("String")->strings->add(Foo=>{}, Bar=>{alias=>"Foo"});
29 : sh002i 5551
30 :     Use string_cmp() to produce the answer checker(s) for your
31 :     correct values. Eg.
32 :    
33 : sh002i 5555 ANS(string_cmp("Foo"));
34 : sh002i 5551
35 : gage 4806 =cut
36 :    
37 : sh002i 5551 loadMacros("MathObjects.pl");
38 :    
39 :     sub _contextString_init {context::String::Init()}; # don't load it again
40 :    
41 : dpvc 5386 ##################################################
42 : dpvc 2855
43 : dpvc 5386 package context::String::Variable;
44 :    
45 : dpvc 2855 sub new {
46 :     my $self = shift; my $equation = shift;
47 :     my $context = $equation->{context};
48 :     my @strings = grep {not defined($context->strings->get($_)->{alias})}
49 :     $context->strings->names;
50 :     my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
51 : dpvc 3503 $equation->Error(["Your answer should be one of %s",$strings]);
52 : dpvc 2855 }
53 :    
54 : dpvc 5386 ##################################################
55 : dpvc 2855
56 : dpvc 5386 package context::String::Formula;
57 :     our @ISA = qw(Value::Formula);
58 :    
59 : dpvc 2855 sub parse {
60 :     my $self = shift;
61 :     foreach my $ref (@{$self->{tokens}}) {
62 :     $self->{ref} = $ref;
63 : dpvc 5386 context::String::Variable->new($self->{equation}) if ($ref->[0] eq 'error'); # display the error
64 : dpvc 2855 }
65 :     $self->SUPER::parse(@_);
66 :     }
67 :    
68 : dpvc 5386 package context::String::BOP::mult;
69 :     our @ISA = qw(Parser::BOP);
70 : dpvc 2855
71 : dpvc 5386 sub _check {
72 :     my $self = shift;
73 :     context::String::Variable->new($self->{equation}); # report an error
74 :     }
75 : dpvc 2855
76 : dpvc 5386 ##################################################
77 : dpvc 2855
78 : dpvc 5386 package context::String;
79 :    
80 :     sub Init {
81 :     my $context = $main::context{String} = Parser::Context->getCopy("Numeric");
82 : dpvc 5441 $context->{name} = "String";
83 : dpvc 6034 $context->{pattern}{number} = qr/^$/;
84 : dpvc 5386 $context->parens->clear();
85 :     $context->variables->clear();
86 :     $context->constants->clear();
87 :     $context->operators->clear();
88 :     $context->functions->clear();
89 :     $context->strings->clear();
90 :     $context->{parser}{Variable} = 'context::String::Variable';
91 :     $context->{parser}{Formula} = 'context::String::Formula';
92 :     $context->operators->add(
93 :     ' ' => {precedence => 3, associativity=>"left", type=>"bin", string => "*", class => 'context::String::BOP::mult'},
94 :     '*' => {precedence => 3, associativity=>"left", type=>"bin", class => 'context::String::BOP::mult'}
95 :     );
96 :    
97 :     main::PG_restricted_eval(<<' END_EVAL');
98 :     sub string_cmp {
99 :     my $strings = shift;
100 :     $strings = [$strings,@_] if (scalar(@_));
101 :     $strings = [$strings] unless ref($strings) eq 'ARRAY';
102 :     return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
103 :     }
104 :     END_EVAL
105 :    
106 :     main::Context("String"); ### FIXME: probably should require author to set this explicitly
107 : dpvc 2855 }
108 :    
109 : dpvc 5373 1;
110 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9