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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6187 - (download) (as text) (annotate)
Sun Jan 24 02:13:38 2010 UTC (10 years ago) by dpvc
File size: 5267 byte(s)
Removed deletion of i,j,k which are not in the original Point context

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/contextLimitedPoint.pl,v 1.14 2009/06/25 23:28:44 gage 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 =head1 NAME
   18 
   19 contextLimitedPoint.pl - Allow point entry but no point operations.
   20 
   21 =head1 DESCRIPTION
   22 
   23 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 
   28   Context("LimitedPoint")
   29 
   30 =cut
   31 
   32 loadMacros("MathObjects.pl");
   33 
   34 sub _contextLimitedPoint_init {LimitedPoint::Init()}; # don't load it again
   35 
   36 ##################################################
   37 #
   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   $self->Error("In this context, '%s' can only be used with Numbers",$bop);
   53 }
   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   $self->Error("In this context, '%s' can only be used with Numbers",$uop);
   96 }
   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 package LimitedPoint;
  129 
  130 sub Init {
  131   #
  132   #  Build the new context that calls the
  133   #  above classes rather than the usual ones
  134   #
  135 
  136   my $context = $main::context{LimitedPoint} = Parser::Context->getCopy("Point");
  137   $context->{name} = "LimitedPoint";
  138   $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 
  165   main::Context("LimitedPoint");  ### FIXME:  probably should require author to set this explicitly
  166 }
  167 
  168 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9