[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 5373 - (download) (as text) (annotate)
Sun Aug 19 02:01:57 2007 UTC (12 years, 3 months ago) by dpvc
File size: 4222 byte(s)
Normalized comments and headers to that they will format their POD
documentation properly.  (I know that the POD processing was supposed
to strip off the initial #, but that doesn't seem to happen, so I've
added a space throughout.)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9