From: dpvc v. a. <we...@ma...> - 2007-08-28 21:59:47
|
Log Message: ----------- Updated contexts to include a "name" field that at least tracks what context you started with (though it can be modified and no longer be the same as the original context). Remove the individual named variables in the Default.pm file; they are now available only through the %Parser::Context::Default::context hash. Remove the >< and . operators, the <...> parentheses, the norm and unit functions, and the i, j, and k constants from the Point context. So the Point context no longer includes vectors and vector operaterations. Modified Files: -------------- pg/lib/Value: Context.pm pg/lib/Parser: Context.pm pg/lib/Parser/Context: Default.pm Functions.pm pg/lib/Parser/Legacy: LimitedComplex.pm LimitedNumeric.pm Numeric.pm pg/macros: answerVariableList.pl contextLimitedPoint.pl Revision Data ------------- Index: Context.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Context.pm,v retrieving revision 1.19 retrieving revision 1.20 diff -Llib/Value/Context.pm -Llib/Value/Context.pm -u -r1.19 -r1.20 --- lib/Value/Context.pm +++ lib/Value/Context.pm @@ -88,6 +88,7 @@ $context->{error}{msg} = {%{$self->{error}{msg}}}; $context->{error}{convert} = $self->{error}{convert} if defined $self->{error}{convert}; + $context->{name} = $self->{name}; $context->{_initialized} = 1; return $context; } Index: Context.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Context.pm,v retrieving revision 1.22 retrieving revision 1.23 diff -Llib/Parser/Context.pm -Llib/Parser/Context.pm -u -r1.22 -r1.23 --- lib/Parser/Context.pm +++ lib/Parser/Context.pm @@ -156,7 +156,7 @@ $contextTable->{current} = $context; $Value::context = \$contextTable->{current}; } elsif (!defined($contextTable->{current})) { - $contextTable->{current} = $Parser::Context::Default::numericContext->copy; + $contextTable->{current} = $Parser::Context::Default::context{Numeric}->copy; $Value::context = \$contextTable->{current}; } return $contextTable->{current}; @@ -174,7 +174,9 @@ my $name = shift; my $context = $contextTable->{$name}; $context = $Parser::Context::Default::context{$name} unless $context; return unless $context; - return $context->copy; + $context = $context->copy; + $context->{name} = $name; + return $context; } # Index: Functions.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Context/Functions.pm,v retrieving revision 1.10 retrieving revision 1.11 diff -Llib/Parser/Context/Functions.pm -Llib/Parser/Context/Functions.pm -u -r1.10 -r1.11 --- lib/Parser/Context/Functions.pm +++ lib/Parser/Context/Functions.pm @@ -81,7 +81,7 @@ $list = $Category{$name}; $list = [$name] if !$list && $context->{functions}{$name}; unless (defined($list)) {warn "Undefined function or category '$name'"; next} - if ($list->[0] eq '_alias_') + if ($list->[0] eq '_alias_') {unshift @names, @{$list}[1..scalar(@{$list})-1]; next} $context->functions->undefine(@{$list}); } @@ -90,16 +90,16 @@ sub enable {Enable(@_)} sub Enable { my $context = Parser::Context->current; - my $functions = $Parser::Context::Default::fullContext->{functions}; + my $functions = $Parser::Context::Default::context{Full}->{functions}; if (ref($_[0]) ne "") {$context = (shift)->{context}} my @names = @_; my ($list,$name); while ($name = shift(@names)) { $list = $Category{$name}; $list = [$name] if !$list && $context->{functions}{$name}; unless (defined($list)) {warn "Undefined function or category '$name'"; next} - if ($list->[0] eq '_alias_') + if ($list->[0] eq '_alias_') {unshift @names, @{$list}[1..scalar(@{$list})-1]; next} - my @fn; foreach my $f (@{$list}) + my @fn; foreach my $f (@{$list}) {push @fn, $f => {class => $functions->{$f}{class}}} $context->functions->set(@fn); } Index: Default.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Context/Default.pm,v retrieving revision 1.40 retrieving revision 1.41 diff -Llib/Parser/Context/Default.pm -Llib/Parser/Context/Default.pm -u -r1.40 -r1.41 --- lib/Parser/Context/Default.pm +++ lib/Parser/Context/Default.pm @@ -242,13 +242,12 @@ # use vars qw(%context); -use vars qw($fullContext $numericContext $complexContext $pointContext - $vectorContext $vector2DContext $matrixContext $intervalContext); +my $context; # # The default Context # -$fullContext = new Parser::Context( +$context = $context{Full} = new Parser::Context( operators => $operators, functions => $functions, constants => $constants, @@ -260,116 +259,111 @@ reduction => $Parser::reduce, ); -$fullContext->constants->set( +$context->constants->set( pi => {TeX => '\pi ', perl => 'pi'}, i => {isConstant => 1, perl => 'i'}, j => {TeX => '\boldsymbol{j}', perl => 'j'}, k => {TeX => '\boldsymbol{k}', perl => 'k'}, ); -$fullContext->usePrecedence('Standard'); +$context->usePrecedence('Standard'); +$context->{name} = "Full"; # # Numeric context (no vectors, matrices or complex numbers) # -$numericContext = $fullContext->copy; -$numericContext->variables->are(x=>'Real'); -$numericContext->operators->undefine('><','.'); -$numericContext->functions->undefine('norm','unit','arg','mod','Re','Im','conj'); -$numericContext->constants->remove('i','j','k'); -$numericContext->parens->remove('<'); -$numericContext->parens->set( +$context = $context{Numeric} = $context{Full}->copy; +$context->variables->are(x=>'Real'); +$context->operators->undefine('><','.'); +$context->functions->undefine('norm','unit','arg','mod','Re','Im','conj'); +$context->constants->remove('i','j','k'); +$context->parens->remove('<'); +$context->parens->set( '(' => {type => 'List', formMatrix => 0}, '[' => {type => 'List', formMatrix => 0}, '{' => {type => 'List'}, ); +$context->{name} = "Numeric"; # # Complex context (no vectors or matrices) # -$complexContext = $fullContext->copy; -$complexContext->variables->are(z=>'Complex'); -$complexContext->operators->undefine('><','.'); -$complexContext->functions->undefine('norm','unit'); -$complexContext->constants->remove('j','k'); -$complexContext->parens->remove('<'); -$complexContext->parens->set( +$context = $context{Complex} = $context{Full}->copy; +$context->variables->are(z=>'Complex'); +$context->operators->undefine('><','.'); +$context->functions->undefine('norm','unit'); +$context->constants->remove('j','k'); +$context->parens->remove('<'); +$context->parens->set( '(' => {type => 'List', formMatrix => 0}, '[' => {type => 'List', formMatrix => 0}, '{' => {type => 'List'}, ); -$complexContext->operators->set( +$context->operators->set( '^' => {class => 'Parser::Function::complex_power', negativeIsComplex => 1}, '**' => {class => 'Parser::Function::complex_power', negativeIsComplex => 1}, ); -$complexContext->functions->set( +$context->functions->set( 'sqrt' => {class => 'Parser::Function::complex_numeric', negativeIsComplex => 1}, 'log' => {class => 'Parser::Function::complex_numeric', negativeIsComplex => 1}, ); +$context->{name} = "Complex"; # # Vector context (no complex numbers) # -$vectorContext = $fullContext->copy; -$vectorContext->variables->are(x=>'Real',y=>'Real',z=>'Real'); -$vectorContext->functions->undefine('arg','mod','Re','Im','conj'); -$vectorContext->constants->replace(i=>Value::Vector->new(1,0,0)); -$vectorContext->constants->set(i=>{TeX=>'\boldsymbol{i}', perl=>'i'}); -$vectorContext->parens->set('(' => {formMatrix => 0}); +$context = $context{Vector} = $context{Full}->copy; +$context->variables->are(x=>'Real',y=>'Real',z=>'Real'); +$context->functions->undefine('arg','mod','Re','Im','conj'); +$context->constants->replace(i=>Value::Vector->new(1,0,0)); +$context->constants->set(i=>{TeX=>'\boldsymbol{i}', perl=>'i'}); +$context->parens->set('(' => {formMatrix => 0}); -$vector2DContext = $vectorContext->copy; -$vector2DContext->constants->replace( +$context = $context{Vector2D} = $context{Vector}->copy; +$context->constants->replace( i => Value::Vector->new(1,0), j => Value::Vector->new(0,1), ); -$vector2DContext->constants->set(i => {TeX=>'\boldsymbol{i}', perl=>'i'}); -$vector2DContext->constants->remove("k"); +$context->constants->set(i => {TeX=>'\boldsymbol{i}', perl=>'i'}); +$context->constants->remove("k"); +$context->{name} = "Vector2D"; # # Point context (for symmetry) # -$pointContext = $vectorContext->copy; +$context = $context{Point} = $context{Vector}->copy; +$context->operators->undefine("><","."); +$context->functions->undefine('norm','unit'); +$context->constants->remove('i','j','k'); +$context->parens->remove("<"); +$context->{name} = "Point"; # # Matrix context (square brackets make matrices in preference to points or intervals) # -$matrixContext = $vectorContext->copy; -$matrixContext->parens->set( +$context = $context{Matrix} = $context{Vector}->copy; +$context->parens->set( '(' => {formMatrix => 1}, '[' => {type => 'Matrix', removable => 0}, ); +$context->{name} = "Vector"; # # Interval context (make intervals rather than lists) # -$intervalContext = $numericContext->copy; -$intervalContext->parens->set( +$context = $context{Interval} = $context{Numeric}->copy; +$context->parens->set( '(' => {type => 'Interval'}, '[' => {type => 'Interval'}, '{' => {type => 'Set', removable => 0, emptyOK => 1}, ); my $infinity = Value::Infinity->new(); -$intervalContext->constants->add( +$context->constants->add( R => Value::Interval->new('(',-$infinity,$infinity,')'), ); -$intervalContext->constants->set(R => {TeX => '{\bf R}'}); - -######################################################################### - -# -# list of all default contexts (users can add more) -# -%context = ( - Full => $fullContext, - Numeric => $numericContext, - Complex => $complexContext, - Point => $pointContext, - Vector => $vectorContext, - Vector2D => $vector2DContext, - Matrix => $matrixContext, - Interval => $intervalContext, -); +$context->constants->set(R => {TeX => '{\bf R}'}); +$context->{name} = "Interval"; ######################################################################### Index: LimitedNumeric.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Legacy/LimitedNumeric.pm,v retrieving revision 1.3 retrieving revision 1.4 diff -Llib/Parser/Legacy/LimitedNumeric.pm -Llib/Parser/Legacy/LimitedNumeric.pm -u -r1.3 -r1.4 --- lib/Parser/Legacy/LimitedNumeric.pm +++ lib/Parser/Legacy/LimitedNumeric.pm @@ -16,8 +16,10 @@ # # Context("LimitedNumeric-StrictFraction"); # +########################################################## +################################################## # # Minus can only appear in front of a number # @@ -35,6 +37,7 @@ sub class {'MINUS'}; +################################################## # # Divides can only appear between numbers or a negative # number and a number @@ -53,6 +56,7 @@ sub class {'DIVIDE'}; +################################################## # # Distinguish integers from decimals # @@ -66,6 +70,8 @@ } +################################################## + package Parser::Legacy::LimitedNumeric; # @@ -74,6 +80,8 @@ # my $context = $Parser::Context::Default::context{Numeric}->copy; $Parser::Context::Default::context{'LimitedNumeric'} = $context; +$context->{name} = 'LimitedNumeric'; + $context->operators->set('u-' => {class => 'Parser::Legacy::LimitedNumeric::UOP::minus'}); $context->operators->undefine( '+', '-', '*', '* ', ' *', ' ', '/', '/ ', ' /', '^', '**', @@ -82,12 +90,15 @@ $context->parens->undefine('|','{','(','['); $context->functions->disable('All'); +################################################## # # For the Fraction versions, allow the modified division, and # make sure numbers used in fractions are just integers # $context = $Parser::Context::Default::context{Numeric}->copy; $Parser::Context::Default::context{'LimitedNumeric-Fraction'} = $context; +$context->{name} = "LimitedNumeric-Fraction"; + $context->operators->set( 'u-' => {class => 'Parser::Legacy::LimitedNumeric::UOP::minus'}, '/' => {class => 'Parser::Legacy::LimitedNumeric::BOP::divide'}, @@ -102,11 +113,15 @@ $context->functions->disable('All'); $context->{parser}{Number} = "Parser::Legacy::LimitedNumeric::Number"; +################################################## # # For strict fractions, don't allow decimal numbers # $context = $context->copy; $Parser::Context::Default::context{'LimitedNumeric-StrictFraction'} = $context; Parser::Number::NoDecimals($context); +$context->{name} = "LimitedNumeric-StrictFractions"; + +###################################################################### 1; Index: LimitedComplex.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Legacy/LimitedComplex.pm,v retrieving revision 1.2 retrieving revision 1.3 diff -Llib/Parser/Legacy/LimitedComplex.pm -Llib/Parser/Legacy/LimitedComplex.pm -u -r1.2 -r1.3 --- lib/Parser/Legacy/LimitedComplex.pm +++ lib/Parser/Legacy/LimitedComplex.pm @@ -220,6 +220,11 @@ my $context = $Parser::Context::Default::context{Complex}->copy; $Parser::Context::Default::context{LimitedComplex} = $context; +$context->{name} = "LimtedComplex"; + +# +# Override operator classes +# $context->operators->set( '+' => {class => 'Parser::Legacy::LimitedComplex::BOP::add'}, '-' => {class => 'Parser::Legacy::LimitedComplex::BOP::subtract'}, @@ -249,27 +254,44 @@ # $context->flags->set(complex_format => 'either'); +################################################## + $context = $context->copy; $Parser::Context::Default::context{'LimitedComplex-cartesian'} = $context; $context->flags->set(complex_format => 'cartesian'); +$context->{name} = "LimtedComplex-cartesian"; + +################################################## $context = $context->copy; $Parser::Context::Default::context{'LimitedComplex-cartesian-strict'} = $context; $context->flags->set(strict_numeric => 1); $context->functions->disable('All'); +$context->{name} = "LimtedComplex-cartesian-strinct"; + +################################################## $context = $Parser::Context::Default::context{'LimitedComplex'}->copy; $Parser::Context::Default::context{'LimitedComplex-polar'} = $context; $context->flags->set(complex_format => 'polar'); +$context->{name} = "LimtedComplex-polar"; + +################################################## $context = $context->copy; $Parser::Context::Default::context{'LimitedComplex-polar-strict'} = $context; $context->flags->set(strict_numeric => 1); $context->functions->disable('All'); +$context->{name} = "LimtedComplex-polar-strict"; + +################################################## $context = $Parser::Context::Default::context{'LimitedComplex'}->copy; $Parser::Context::Default::context{'LimitedComplex-strict'} = $context; $context->flags->set(strict_numeric => 1); $context->functions->disable('All'); +$context->{name} = "LimtedComplex-strict"; + +################################################## 1; Index: Numeric.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Legacy/Numeric.pm,v retrieving revision 1.3 retrieving revision 1.4 diff -Llib/Parser/Legacy/Numeric.pm -Llib/Parser/Legacy/Numeric.pm -u -r1.3 -r1.4 --- lib/Parser/Legacy/Numeric.pm +++ lib/Parser/Legacy/Numeric.pm @@ -25,3 +25,6 @@ step => {class => 'Parser::Legacy::Numeric', perl => 'Parser::Legacy::Numeric::do_step'}, fact => {class => 'Parser::Legacy::Numeric', perl => 'Parser::Legacy::Numeric::do_fact'}, ); +$context->{name} = "LegacyNumeric"; + +1; Index: contextLimitedPoint.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/contextLimitedPoint.pl,v retrieving revision 1.8 retrieving revision 1.9 diff -Lmacros/contextLimitedPoint.pl -Lmacros/contextLimitedPoint.pl -u -r1.8 -r1.9 --- macros/contextLimitedPoint.pl +++ macros/contextLimitedPoint.pl @@ -141,7 +141,6 @@ '(' => {formMatrix => 0}, '[' => {formMatrix => 0}, ); - $context->parens->remove('<'); $context->variables->are(x=>'Real'); $context->constants->remove('i','j','k'); Index: answerVariableList.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/answerVariableList.pl,v retrieving revision 1.8 retrieving revision 1.9 diff -Lmacros/answerVariableList.pl -Lmacros/answerVariableList.pl -u -r1.8 -r1.9 --- macros/answerVariableList.pl +++ macros/answerVariableList.pl @@ -36,7 +36,7 @@ # A new context for variable lists # $main::context{VariableList} = Parser::Context->new( - operators => {',' => $Parser::Context::Default::fullContext->operators->get(',')}, + operators => {',' => $Parser::Context::Default::context{Full}->operators->get(',')}, lists => {'List' => {class =>'Parser::List::List'}}, parens => { '(' => {close => ')', type => 'List', formList => 1}, |