[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 5551 - (download) (as text) (annotate)
Tue Oct 2 20:48:05 2007 UTC (12 years, 2 months ago) by sh002i
File size: 4367 byte(s)
improved formatting for docs -- these were in pod sections but were all
formatted as verbatim sections, and i moved them into normal paragraphs,
lists, etc. should make things more readable from the web.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9