[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 5441 - (view) (download) (as text)

1 : dpvc 5371 loadMacros("MathObjects.pl");
2 : dpvc 2855
3 : dpvc 5386 sub _contextString_init {context::String::Init()}; # don't load it again
4 : dpvc 2855
5 : gage 4997 =head3 Context("String")
6 :    
7 : dpvc 5373 ##########################################################
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 : dpvc 2855
22 : gage 4806 =cut
23 :    
24 : dpvc 5386 ##################################################
25 : dpvc 2855
26 : dpvc 5386 package context::String::Variable;
27 :    
28 : dpvc 2855 sub new {
29 :     my $self = shift; my $equation = shift;
30 :     my $context = $equation->{context};
31 :     my @strings = grep {not defined($context->strings->get($_)->{alias})}
32 :     $context->strings->names;
33 :     my $strings = join(', ',@strings[0..$#strings-1]).' or '.$strings[-1];
34 : dpvc 3503 $equation->Error(["Your answer should be one of %s",$strings]);
35 : dpvc 2855 }
36 :    
37 : dpvc 5386 ##################################################
38 : dpvc 2855
39 : dpvc 5386 package context::String::Formula;
40 :     our @ISA = qw(Value::Formula);
41 :    
42 : dpvc 2855 sub parse {
43 :     my $self = shift;
44 :     foreach my $ref (@{$self->{tokens}}) {
45 :     $self->{ref} = $ref;
46 : dpvc 5386 context::String::Variable->new($self->{equation}) if ($ref->[0] eq 'error'); # display the error
47 : dpvc 2855 }
48 :     $self->SUPER::parse(@_);
49 :     }
50 :    
51 : dpvc 5386 package context::String::BOP::mult;
52 :     our @ISA = qw(Parser::BOP);
53 : dpvc 2855
54 : dpvc 5386 sub _check {
55 :     my $self = shift;
56 :     context::String::Variable->new($self->{equation}); # report an error
57 :     }
58 : dpvc 2855
59 : dpvc 5386 ##################################################
60 : dpvc 2855
61 : dpvc 5386 package context::String;
62 :    
63 :     sub Init {
64 :     my $context = $main::context{String} = Parser::Context->getCopy("Numeric");
65 : dpvc 5441 $context->{name} = "String";
66 : dpvc 5386 $context->parens->clear();
67 :     $context->variables->clear();
68 :     $context->constants->clear();
69 :     $context->operators->clear();
70 :     $context->functions->clear();
71 :     $context->strings->clear();
72 :     $context->{parser}{Variable} = 'context::String::Variable';
73 :     $context->{parser}{Formula} = 'context::String::Formula';
74 :     $context->operators->add(
75 :     ' ' => {precedence => 3, associativity=>"left", type=>"bin", string => "*", class => 'context::String::BOP::mult'},
76 :     '*' => {precedence => 3, associativity=>"left", type=>"bin", class => 'context::String::BOP::mult'}
77 :     );
78 :    
79 :     main::PG_restricted_eval(<<' END_EVAL');
80 :     sub string_cmp {
81 :     my $strings = shift;
82 :     $strings = [$strings,@_] if (scalar(@_));
83 :     $strings = [$strings] unless ref($strings) eq 'ARRAY';
84 :     return map {String($_)->cmp(showHints=>0,showLengthHints=>0)} @{$strings};
85 :     }
86 :     END_EVAL
87 :    
88 :     main::Context("String"); ### FIXME: probably should require author to set this explicitly
89 : dpvc 2855 }
90 :    
91 : dpvc 5373 1;
92 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9