[system] / trunk / pg / lib / Parser / Context / Default.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/Context/Default.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3587 - (download) (as text) (annotate)
Tue Aug 30 00:36:55 2005 UTC (14 years, 3 months ago) by dpvc
File size: 13617 byte(s)
Added a Point context that is a copy of the Vector context.  This will
allow users to override some settings in Vector context (namely, how
they are entered and displayed) without preventing users from being
able to enter points (in Point context).

    1 #########################################################################
    2 
    3 package Parser::Context::Default;
    4 use vars qw($operators $parens $lists $constants $variables $functions $strings $flags);
    5 use strict;
    6 
    7 #
    8 #  The default operators, functions, etc.
    9 #
   10 $operators = {
   11    ',' => {precedence => 0, associativity => 'left', type => 'bin', string => ',',
   12            class => 'Parser::BOP::comma', isComma => 1},
   13 
   14    '+' => {precedence => 1, associativity => 'left', type => 'both', string => '+',
   15            class => 'Parser::BOP::add'},
   16 
   17    '-' => {precedence => 1, associativity => 'left', type => 'both', string => '-',
   18            perl => '- ', class => 'Parser::BOP::subtract', rightparens => 'same'},
   19 
   20    'U' => {precedence => 1.5, associativity => 'left', type => 'bin', isUnion => 1,
   21            string => ' U ', TeX => '\cup ', class => 'Parser::BOP::union'},
   22 
   23    '><'=> {precedence => 2, associativity => 'left', type => 'bin',
   24            string => ' >< ', TeX => '\times ', perl => ' x ', fullparens => 1,
   25            class => 'Parser::BOP::cross'},
   26 
   27    '.' => {precedence => 2, associativity => 'left', type => 'bin',
   28            string => '.', TeX => '\cdot ', class => 'Parser::BOP::dot'},
   29 
   30    '*' => {precedence => 3, associativity => 'left', type => 'bin', space => ' *',
   31            string => '*', TeX => '', class => 'Parser::BOP::multiply'},
   32 
   33    '/' => {precedence => 3, associativity => 'left', type => 'bin', string => '/',
   34            class => 'Parser::BOP::divide', space => ' /',
   35            rightparens => 'all', leftparens => 'extra', fullparens => 1},
   36 
   37    ' /' => {precedence => 2.8, associativity => 'left', type => 'bin', string => '/',
   38            class => 'Parser::BOP::divide',
   39            rightparens => 'all', leftparens => 'extra', fullparens => 1, hidden => 1},
   40 
   41    '/ ' => {precedence => 2.8, associativity => 'left', type => 'bin', string => '/',
   42            class => 'Parser::BOP::divide',
   43            rightparens => 'all', leftparens => 'extra', fullparens => 1},
   44 
   45    ' *'=> {precedence => 2.8, associativity => 'left', type => 'bin', string => '*',
   46            class => 'Parser::BOP::multiply', TeX => '', hidden => 1},
   47 
   48    '* '=> {precedence => 2.8, associativity => 'left', type => 'bin', string => '*',
   49            class => 'Parser::BOP::multiply', TeX => ''},
   50 
   51    'fn'=> {precedence => 2.9, associativity => 'left', type => 'unary', string => '',
   52            parenPrecedence => 5, hidden => 1},
   53 
   54    ' ' => {precedence => 3.1, associativity => 'left', type => 'bin', string => '*',
   55            class => 'Parser::BOP::multiply', space => ' *', hidden => 1},
   56 
   57    'u+'=> {precedence => 6, associativity => 'left', type => 'unary', string => '+',
   58            class => 'Parser::UOP::plus', hidden => 1, allowInfinite => 1, nofractionparens => 1},
   59 
   60    'u-'=> {precedence => 6, associativity => 'left', type => 'unary', string => '-', perl => '- ',
   61            class => 'Parser::UOP::minus', hidden => 1, allowInfinite => 1, nofractionparens => 1},
   62 
   63    '^' => {precedence => 7, associativity => 'right', type => 'bin', string => '^', perl => '**',
   64            class => 'Parser::BOP::power', leftf => 1, fullparens => 1, isInverse => 1},
   65 
   66    '**'=> {precedence => 7, associativity => 'right', type => 'bin', string => '^', perl => '**',
   67            class => 'Parser::BOP::power', leftf => 1, fullparens => 1, isInverse => 1},
   68 
   69    '!' => {precedence => 8, associativity => 'right', type => 'unary', string => '!',
   70            class => 'Parser::UOP::factorial', perl => 'Factorial'},
   71 
   72    '_' => {precedence => 9, associativity => 'left', type => 'bin', string => '_',
   73            class => 'Parser::BOP::underscore', leftparens => 'all'},
   74 };
   75 
   76 $parens = {
   77    '(' => {close => ')', type => 'Point', formMatrix => 1, formInterval => ']',
   78            formList => 1, removable => 1, emptyOK => 1, function => 1},
   79    '[' => {close => ']', type => 'Point', formMatrix => 1, formInterval => ')', removable => 1},
   80    '<' => {close => '>', type => 'Vector', formMatrix => 1},
   81    '{' => {close => '}', type => 'Point', removable => 1},
   82    '|' => {close => '|', type => 'AbsoluteValue'},
   83    'start' => {close => 'start', type => 'List', formList => 1,
   84                removable => 1, emptyOK => 1, hidden => 1},
   85    'interval' => {type => 'Interval', hidden => 1},
   86    'list'     => {type => 'List', hidden => 1},
   87 };
   88 
   89 $lists = {
   90    'Point'         => {class =>'Parser::List::Point',         open => '(', close => ')', separator => ','},
   91    'Vector'        => {class =>'Parser::List::Vector',        open => '<', close => '>', separator => ','},
   92    'Matrix'        => {class =>'Parser::List::Matrix',        open => '[', close => ']', separator => ','},
   93    'List'          => {class =>'Parser::List::List',          open => '(', close => ')', separator => ', '},
   94    'Interval'      => {class =>'Parser::List::Interval',      open => '(', close => ')', separator => ','},
   95    'Set'           => {class =>'Parser::List::Set',           open => '{', close => '}', separator => ','},
   96    'Union'         => {class =>'Parser::List::Union',         open => '',  close => '',  separator => ' U '},
   97    'AbsoluteValue' => {class =>'Parser::List::AbsoluteValue', open => '|', close => '|', separator => ''},
   98 };
   99 
  100 $constants = {
  101    'e'  => exp(1),
  102    'pi' => 4*atan2(1,1),
  103    'i'  => Value::Complex->new(0,1),
  104    'j'  => Value::Vector->new(0,1,0),
  105    'k'  => Value::Vector->new(0,0,1),
  106 };
  107 
  108 $variables = {
  109    'x' => 'Real',
  110    'y' => 'Real',
  111    'z' => 'Real',
  112 };
  113 
  114 $functions = {
  115    'sin'   => {class => 'Parser::Function::trig', TeX => '\sin', inverse => 'asin', simplePowers => 1},
  116    'cos'   => {class => 'Parser::Function::trig', TeX => '\cos', inverse => 'acos', simplePowers => 1},
  117    'tan'   => {class => 'Parser::Function::trig', TeX => '\tan', inverse => 'atan', simplePowers => 1},
  118    'sec'   => {class => 'Parser::Function::trig', TeX => '\sec', inverse => 'asec', simplePowers => 1},
  119    'csc'   => {class => 'Parser::Function::trig', TeX => '\csc', inverse => 'acsc', simplePowers => 1},
  120    'cot'   => {class => 'Parser::Function::trig', TeX => '\cot', inverse => 'acot', simplePowers => 1},
  121    'asin'  => {class => 'Parser::Function::trig', TeX => '\sin^{-1}'},
  122    'acos'  => {class => 'Parser::Function::trig', TeX => '\cos^{-1}'},
  123    'atan'  => {class => 'Parser::Function::trig', TeX => '\tan^{-1}'},
  124    'asec'  => {class => 'Parser::Function::trig', TeX => '\sec^{-1}'},
  125    'acsc'  => {class => 'Parser::Function::trig', TeX => '\csc^{-1}'},
  126    'acot'  => {class => 'Parser::Function::trig', TeX => '\cot^{-1}'},
  127 
  128    'sinh'   => {class => 'Parser::Function::hyperbolic', TeX => '\sinh',
  129     inverse => 'asinh', simplePowers => 1},
  130    'cosh'   => {class => 'Parser::Function::hyperbolic', TeX => '\cosh',
  131     inverse => 'acosh', simplePowers => 1},
  132    'tanh'   => {class => 'Parser::Function::hyperbolic', TeX => '\tanh',
  133     inverse => 'atanh', simplePowers => 1},
  134    'sech'   => {class => 'Parser::Function::hyperbolic', inverse => 'asech', simplePowers => 1},
  135    'csch'   => {class => 'Parser::Function::hyperbolic', inverse => 'acsch', simplePowers => 1},
  136    'coth'   => {class => 'Parser::Function::hyperbolic', TeX => '\coth',
  137     inverse => 'acoth', simplePowers => 1},
  138    'asinh'  => {class => 'Parser::Function::hyperbolic', TeX => '\sinh^{-1}'},
  139    'acosh'  => {class => 'Parser::Function::hyperbolic', TeX => '\cosh^{-1}'},
  140    'atanh'  => {class => 'Parser::Function::hyperbolic', TeX => '\tanh^{-1}'},
  141    'asech'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm sech}^{-1}'},
  142    'acsch'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm csch}^{-1}'},
  143    'acoth'  => {class => 'Parser::Function::hyperbolic', TeX => '\coth^{-1}'},
  144 
  145    'ln'    => {class => 'Parser::Function::numeric', inverse => 'exp',
  146          TeX => '\ln', simplePowers => 1},
  147    'log'   => {class => 'Parser::Function::numeric', TeX => '\log', simplePowers => 1},
  148    'log10' => {class => 'Parser::Function::numeric', nocomplex => 1, TeX => '\log_{10}'},
  149    'exp'   => {class => 'Parser::Function::numeric', inverse => 'log', TeX => '\exp'},
  150    'sqrt'  => {class => 'Parser::Function::numeric', braceTeX => 1, TeX => '\sqrt'},
  151    'abs'   => {class => 'Parser::Function::numeric'},
  152    'int'   => {class => 'Parser::Function::numeric'},
  153    'sgn'   => {class => 'Parser::Function::numeric', nocomplex => 1},
  154 
  155    'atan2' => {class => 'Parser::Function::numeric2'},
  156 
  157    'norm'  => {class => 'Parser::Function::vector', vectorInput => 1},
  158    'unit'  => {class => 'Parser::Function::vector', vectorInput => 1},
  159 
  160    'arg'   => {class => 'Parser::Function::complex'},
  161    'mod'   => {class => 'Parser::Function::complex'},
  162    'Re'    => {class => 'Parser::Function::complex', TeX => '\Re'},
  163    'Im'    => {class => 'Parser::Function::complex', TeX => '\Im'},
  164    'conj'  => {class => 'Parser::Function::complex', complex => 1, TeX=>'\overline', braceTeX => 1},
  165 
  166    # Det, Inverse, Transpose, Floor, Ceil
  167 
  168    'arcsin' => {alias => 'asin'},
  169    'arccos' => {alias => 'acos'},
  170    'arctan' => {alias => 'atan'},
  171    'arcsec' => {alias => 'asec'},
  172    'arccsc' => {alias => 'acsc'},
  173    'arccot' => {alias => 'acot'},
  174 
  175    'arcsinh' => {alias => 'asinh'},
  176    'arccosh' => {alias => 'acosh'},
  177    'arctanh' => {alias => 'atanh'},
  178    'arcsech' => {alias => 'asech'},
  179    'arccsch' => {alias => 'acsch'},
  180    'arccoth' => {alias => 'acoth'},
  181 
  182    'logten' => {alias => 'log10'},
  183 };
  184 
  185 $strings = {
  186    'infinity'  => {infinite => 1},
  187    'inf'  => {alias => 'infinity'},
  188    'NONE' => {},
  189    'DNE'  => {},
  190 #   'T' => {true => 1},
  191 #   'F' => {false => 1},
  192 };
  193 
  194 $flags = {
  195   ijk => 0,                     # 1 = show vectors in ijk form
  196   reduceConstants => 1,         # 1 = automatically combine constants
  197   reduceConstantFunctions => 1, # 1 = compute function values of constants
  198   showExtraParens => 0,         # 1 = make things painfully unambiguous
  199 };
  200 
  201 ############################################################################
  202 ############################################################################
  203 #
  204 #  Special purpose contexts
  205 #
  206 
  207 use vars qw(%context);
  208 use vars qw($fullContext $numericContext $complexContext $pointContext
  209       $vectorContext $matrixContext $intervalContext);
  210 
  211 #
  212 #  The default Context
  213 #
  214 $fullContext = new Parser::Context(
  215   operators => $operators,
  216   functions => $functions,
  217   constants => $constants,
  218   variables => $variables,
  219   strings   => $strings,
  220   parens    => $parens,
  221   lists     => $lists,
  222   flags     => $flags,
  223   reduction => $Parser::reduce,
  224 );
  225 
  226 $fullContext->constants->set(
  227   pi => {TeX => '\pi ', perl => 'pi'},
  228   i => {isConstant => 1, perl => 'i'},
  229   j => {TeX => '\boldsymbol{j}', perl => 'j'},
  230   k => {TeX => '\boldsymbol{k}', perl => 'k'},
  231 );
  232 
  233 $fullContext->usePrecedence('Standard');
  234 
  235 #
  236 #  Numeric context (no vectors, matrices or complex numbers)
  237 #
  238 $numericContext = $fullContext->copy;
  239 $numericContext->variables->are(x=>'Real');
  240 $numericContext->operators->undefine('><','.');
  241 $numericContext->functions->undefine('norm','unit','arg','mod','Re','Im','conj');
  242 $numericContext->constants->remove('i','j','k');
  243 $numericContext->parens->remove('<');
  244 $numericContext->parens->set(
  245    '(' => {type => 'List', formMatrix => 0},
  246    '[' => {type => 'List', formMatrix => 0},
  247    '{' => {type => 'List'},
  248 );
  249 
  250 #
  251 #  Complex context (no vectors or matrices)
  252 #
  253 $complexContext = $fullContext->copy;
  254 $complexContext->variables->are(z=>'Complex');
  255 $complexContext->operators->undefine('><','.');
  256 $complexContext->functions->undefine('norm','unit');
  257 $complexContext->constants->remove('j','k');
  258 $complexContext->parens->remove('<');
  259 $complexContext->parens->set(
  260    '(' => {type => 'List', formMatrix => 0},
  261    '[' => {type => 'List', formMatrix => 0},
  262    '{' => {type => 'List'},
  263 );
  264 $complexContext->operators->set(
  265   '^'  => {class => 'Parser::Function::complex_power', negativeIsComplex => 1},
  266   '**' => {class => 'Parser::Function::complex_power', negativeIsComplex => 1},
  267 );
  268 $complexContext->functions->set(
  269   'sqrt' => {class => 'Parser::Function::complex_numeric', negativeIsComplex => 1},
  270   'log'  => {class => 'Parser::Function::complex_numeric', negativeIsComplex => 1},
  271 );
  272 
  273 
  274 #
  275 #  Vector context (no complex numbers)
  276 #
  277 $vectorContext = $fullContext->copy;
  278 $vectorContext->variables->are(x=>'Real',y=>'Real',z=>'Real');
  279 $vectorContext->functions->undefine('arg','mod','Re','Im','conj');
  280 $vectorContext->constants->replace(i=>Value::Vector->new(1,0,0));
  281 $vectorContext->constants->set(i=>{TeX=>'\boldsymbol{i}', perl=>'i'});
  282 
  283 #
  284 #  Point context (for symmetry)
  285 #
  286 $pointContext = $vectorContext->copy;
  287 
  288 #
  289 #  Matrix context (square brackets make matrices in preference to points or intervals)
  290 #
  291 $matrixContext = $vectorContext->copy;
  292 $matrixContext->parens->set('[' => {type => 'Matrix', removable => 0});
  293 
  294 #
  295 #  Interval context (make intervals rather than lists)
  296 #
  297 $intervalContext = $numericContext->copy;
  298 $intervalContext->parens->set(
  299    '(' => {type => 'Interval'},
  300    '[' => {type => 'Interval'},
  301    '{' => {type => 'Set', removable => 0, emptyOK => 1},
  302 );
  303 my $infinity = Value::Infinity->new();
  304 $intervalContext->constants->add(
  305   R => Value::Interval->new('(',-$infinity,$infinity,')'),
  306 );
  307 $intervalContext->constants->set(R => {TeX => '{\bf R}'});
  308 
  309 #########################################################################
  310 
  311 #
  312 #  list of all default contexts (users can add more)
  313 #
  314 %context = (
  315   Full     => $fullContext,
  316   Numeric  => $numericContext,
  317   Complex  => $complexContext,
  318   Point    => $pointContext,
  319   Vector   => $vectorContext,
  320   Matrix   => $matrixContext,
  321   Interval => $intervalContext,
  322 );
  323 
  324 #########################################################################
  325 
  326 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9