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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5556 - (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 :     # $CVSHeader: webwork2/lib/WeBWorK.pm,v 1.100 2007/08/13 22:59:53 sh002i Exp $
5 :     #
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 : gage 4997
19 : sh002i 5551 contextLimitedPoint.pl - Allow point entry but no point operations.
20 : dpvc 2726
21 : sh002i 5551 =head1 DESCRIPTION
22 : dpvc 2726
23 : sh002i 5551 Implements a context in which points can be entered,
24 :     but no operations are permitted between points. So
25 :     students will be able to perform operations within the
26 :     coordinates of the points, but not between points.
27 : gage 4997
28 : sh002i 5555 Context("LimitedPoint")
29 : dpvc 2726
30 : dpvc 5049 =cut
31 :    
32 : sh002i 5551 loadMacros("MathObjects.pl");
33 :    
34 :     sub _contextLimitedPoint_init {LimitedPoint::Init()}; # don't load it again
35 :    
36 : dpvc 5392 ##################################################
37 : dpvc 2726 #
38 :     # Handle common checking for BOPs
39 :     #
40 :     package LimitedPoint::BOP;
41 :    
42 :     #
43 :     # Do original check and then if the operands are numbers, its OK.
44 :     # Otherwise report an error.
45 :     #
46 :     sub _check {
47 :     my $self = shift;
48 :     my $super = ref($self); $super =~ s/LimitedPoint/Parser/;
49 :     &{$super."::_check"}($self);
50 :     return if $self->checkNumbers;
51 :     my $bop = $self->{def}{string} || $self->{bop};
52 : dpvc 3371 $self->Error("In this context, '%s' can only be used with Numbers",$bop);
53 : dpvc 2726 }
54 :    
55 :     ##############################################
56 :     #
57 :     # Now we get the individual replacements for the operators
58 :     # that we don't want to allow. We inherit everything from
59 :     # the original Parser::BOP class, except the _check
60 :     # routine, which comes from LimitedPoint::BOP above.
61 :     #
62 :    
63 :     package LimitedPoint::BOP::add;
64 :     our @ISA = qw(LimitedPoint::BOP Parser::BOP::add);
65 :    
66 :     ##############################################
67 :    
68 :     package LimitedPoint::BOP::subtract;
69 :     our @ISA = qw(LimitedPoint::BOP Parser::BOP::subtract);
70 :    
71 :     ##############################################
72 :    
73 :     package LimitedPoint::BOP::multiply;
74 :     our @ISA = qw(LimitedPoint::BOP Parser::BOP::multiply);
75 :    
76 :     ##############################################
77 :    
78 :     package LimitedPoint::BOP::divide;
79 :     our @ISA = qw(LimitedPoint::BOP Parser::BOP::divide);
80 :    
81 :     ##############################################
82 :     ##############################################
83 :     #
84 :     # Now we do the same for the unary operators
85 :     #
86 :    
87 :     package LimitedPoint::UOP;
88 :    
89 :     sub _check {
90 :     my $self = shift;
91 :     my $super = ref($self); $super =~ s/LimitedPoint/Parser/;
92 :     &{$super."::_check"}($self);
93 :     return if $self->checkNumber;
94 :     my $uop = $self->{def}{string} || $self->{uop};
95 : dpvc 3371 $self->Error("In this context, '%s' can only be used with Numbers",$uop);
96 : dpvc 2726 }
97 :    
98 :     ##############################################
99 :    
100 :     package LimitedPoint::UOP::plus;
101 :     our @ISA = qw(LimitedPoint::UOP Parser::UOP::plus);
102 :    
103 :     ##############################################
104 :    
105 :     package LimitedPoint::UOP::minus;
106 :     our @ISA = qw(LimitedPoint::UOP Parser::UOP::minus);
107 :    
108 :     ##############################################
109 :     ##############################################
110 :     #
111 :     # Absolute value does vector norm, so we
112 :     # trap that as well.
113 :     #
114 :    
115 :     package LimitedPoint::List::AbsoluteValue;
116 :     our @ISA = qw(Parser::List::AbsoluteValue);
117 :    
118 :     sub _check {
119 :     my $self = shift;
120 :     $self->SUPER::_check;
121 :     return if $self->{coords}[0]->type eq 'Number';
122 :     $self->Error("Vector norm is not allowed in this context");
123 :     }
124 :    
125 :     ##############################################
126 :     ##############################################
127 :    
128 : dpvc 5392 package LimitedPoint;
129 : dpvc 2726
130 : dpvc 5392 sub Init {
131 :     #
132 :     # Build the new context that calls the
133 :     # above classes rather than the usual ones
134 :     #
135 : dpvc 2726
136 : dpvc 5392 my $context = $main::context{LimitedPoint} = Parser::Context->getCopy("Point");
137 : dpvc 5441 $context->{name} = "LimitedPoint";
138 : dpvc 5392 $context->operators->set(
139 :     '+' => {class => 'LimitedPoint::BOP::add'},
140 :     '-' => {class => 'LimitedPoint::BOP::subtract'},
141 :     '*' => {class => 'LimitedPoint::BOP::multiply'},
142 :     '* ' => {class => 'LimitedPoint::BOP::multiply'},
143 :     ' *' => {class => 'LimitedPoint::BOP::multiply'},
144 :     ' ' => {class => 'LimitedPoint::BOP::multiply'},
145 :     '/' => {class => 'LimitedPoint::BOP::divide'},
146 :     ' /' => {class => 'LimitedPoint::BOP::divide'},
147 :     '/ ' => {class => 'LimitedPoint::BOP::divide'},
148 :     'u+' => {class => 'LimitedPoint::UOP::plus'},
149 :     'u-' => {class => 'LimitedPoint::UOP::minus'},
150 :     );
151 :     #
152 :     # Remove these operators and functions
153 :     #
154 :     $context->operators->undefine('_','U','><','.');
155 :     $context->functions->undefine('norm','unit');
156 :     $context->lists->set(
157 :     AbsoluteValue => {class => 'LimitedPoint::List::AbsoluteValue'},
158 :     );
159 :     $context->parens->set(
160 :     '(' => {formMatrix => 0},
161 :     '[' => {formMatrix => 0},
162 :     );
163 :     $context->variables->are(x=>'Real');
164 :     $context->constants->remove('i','j','k');
165 : dpvc 2726
166 : dpvc 5392 main::Context("LimitedPoint"); ### FIXME: probably should require author to set this explicitly
167 :     }
168 : dpvc 5373
169 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9