[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 3371 - (download) (as text) (annotate)
Tue Jul 12 22:39:56 2005 UTC (14 years, 7 months ago) by dpvc
File size: 4153 byte(s)
Make error messages potentially localizable (by making them use
sprintf-style strings rather than variable subtitution).

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9