[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 5441 - (download) (as text) (annotate)
Tue Aug 28 22:40:15 2007 UTC (12 years, 5 months ago) by dpvc
File size: 4351 byte(s)
Add context names for the context(s) created here.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9