[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 3203 - (download) (as text) (annotate)
Mon Mar 28 23:46:06 2005 UTC (8 years, 1 month ago) by dpvc
File size: 12905 byte(s)
Handle extra parentheses better.

    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    'U' => {precedence => 0.5, associativity => 'left', type => 'bin', isUnion => 1,
   15            string => ' U ', TeX => '\cup ', class => 'Parser::BOP::union'},
   16 
   17    '+' => {precedence => 1, associativity => 'left', type => 'both', string => '+',
   18            class => 'Parser::BOP::add'},
   19 
   20    '-' => {precedence => 1, associativity => 'left', type => 'both', string => '-',
   21            class => 'Parser::BOP::subtract', rightparens => 'same'},
   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 => '-',
   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'},
   91    'Vector'        => {class =>'Parser::List::Vector'},
   92    'Matrix'        => {class =>'Parser::List::Matrix', open => '[', close => ']'},
   93    'List'          => {class =>'Parser::List::List'},
   94    'Interval'      => {class =>'Parser::List::Interval'},
   95    'AbsoluteValue' => {class =>'Parser::List::AbsoluteValue'},
   96 };
   97 
   98 $constants = {
   99    'e'  => exp(1),
  100    'pi' => 4*atan2(1,1),
  101    'i'  => Value::Complex->new(0,1),
  102    'j'  => Value::Vector->new(0,1,0),
  103    'k'  => Value::Vector->new(0,0,1),
  104 };
  105 
  106 $variables = {
  107    'x' => 'Real',
  108    'y' => 'Real',
  109    'z' => 'Real',
  110 };
  111 
  112 $functions = {
  113    'sin'   => {class => 'Parser::Function::trig', TeX => '\sin', inverse => 'asin', simplePowers => 1},
  114    'cos'   => {class => 'Parser::Function::trig', TeX => '\cos', inverse => 'acos', simplePowers => 1},
  115    'tan'   => {class => 'Parser::Function::trig', TeX => '\tan', inverse => 'atan', simplePowers => 1},
  116    'sec'   => {class => 'Parser::Function::trig', TeX => '\sec', inverse => 'asec', simplePowers => 1},
  117    'csc'   => {class => 'Parser::Function::trig', TeX => '\csc', inverse => 'acsc', simplePowers => 1},
  118    'cot'   => {class => 'Parser::Function::trig', TeX => '\cot', inverse => 'acot', simplePowers => 1},
  119    'asin'  => {class => 'Parser::Function::trig', TeX => '\sin^{-1}'},
  120    'acos'  => {class => 'Parser::Function::trig', TeX => '\cos^{-1}'},
  121    'atan'  => {class => 'Parser::Function::trig', TeX => '\tan^{-1}'},
  122    'asec'  => {class => 'Parser::Function::trig', TeX => '\sec^{-1}'},
  123    'acsc'  => {class => 'Parser::Function::trig', TeX => '\csc^{-1}'},
  124    'acot'  => {class => 'Parser::Function::trig', TeX => '\cot^{-1}'},
  125 
  126    'sinh'   => {class => 'Parser::Function::hyperbolic', TeX => '\sinh',
  127     inverse => 'asinh', simplePowers => 1},
  128    'cosh'   => {class => 'Parser::Function::hyperbolic', TeX => '\cosh',
  129     inverse => 'acosh', simplePowers => 1},
  130    'tanh'   => {class => 'Parser::Function::hyperbolic', TeX => '\tanh',
  131     inverse => 'atanh', simplePowers => 1},
  132    'sech'   => {class => 'Parser::Function::hyperbolic', inverse => 'asech', simplePowers => 1},
  133    'csch'   => {class => 'Parser::Function::hyperbolic', inverse => 'acsch', simplePowers => 1},
  134    'coth'   => {class => 'Parser::Function::hyperbolic', TeX => '\coth',
  135     inverse => 'acoth', simplePowers => 1},
  136    'asinh'  => {class => 'Parser::Function::hyperbolic', TeX => '\sinh^{-1}'},
  137    'acosh'  => {class => 'Parser::Function::hyperbolic', TeX => '\cosh^{-1}'},
  138    'atanh'  => {class => 'Parser::Function::hyperbolic', TeX => '\tanh^{-1}'},
  139    'asech'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm sech}^{-1}'},
  140    'acsch'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm csch}^{-1}'},
  141    'acoth'  => {class => 'Parser::Function::hyperbolic', TeX => '\coth^{-1}'},
  142 
  143    'ln'    => {class => 'Parser::Function::numeric', inverse => 'exp',
  144          TeX => '\ln', simplePowers => 1},
  145    'log'   => {class => 'Parser::Function::numeric', TeX => '\log', simplePowers => 1},
  146    'log10' => {class => 'Parser::Function::numeric', nocomplex => 1, TeX => '\log_{10}'},
  147    'exp'   => {class => 'Parser::Function::numeric', inverse => 'log', TeX => '\exp'},
  148    'sqrt'  => {class => 'Parser::Function::numeric', braceTeX => 1, TeX => '\sqrt'},
  149    'abs'   => {class => 'Parser::Function::numeric'},
  150    'int'   => {class => 'Parser::Function::numeric'},
  151    'sgn'   => {class => 'Parser::Function::numeric', nocomplex => 1},
  152 
  153    'atan2' => {class => 'Parser::Function::numeric2'},
  154 
  155    'norm'  => {class => 'Parser::Function::vector', vectorInput => 1},
  156    'unit'  => {class => 'Parser::Function::vector', vectorInput => 1},
  157 
  158    'arg'   => {class => 'Parser::Function::complex'},
  159    'mod'   => {class => 'Parser::Function::complex'},
  160    'Re'    => {class => 'Parser::Function::complex', TeX => '\Re'},
  161    'Im'    => {class => 'Parser::Function::complex', TeX => '\Im'},
  162    'conj'  => {class => 'Parser::Function::complex', complex => 1, TeX=>'\overline', braceTeX => 1},
  163 
  164    # Det, Inverse, Transpose, Floor, Ceil
  165 
  166    'arcsin' => {alias => 'asin'},
  167    'arccos' => {alias => 'acos'},
  168    'arctan' => {alias => 'atan'},
  169    'arcsec' => {alias => 'asec'},
  170    'arccsc' => {alias => 'acsc'},
  171    'arccot' => {alias => 'acot'},
  172 
  173    'arcsinh' => {alias => 'asinh'},
  174    'arccosh' => {alias => 'acosh'},
  175    'arctanh' => {alias => 'atanh'},
  176    'arcsech' => {alias => 'asech'},
  177    'arccsch' => {alias => 'acsch'},
  178    'arccoth' => {alias => 'acoth'},
  179 
  180    'logten' => {alias => 'log10'},
  181 };
  182 
  183 $strings = {
  184    'infinity'  => {infinite => 1},
  185    'INFINITY'  => {alias => 'infinity'},
  186    'inf'  => {alias => 'infinity'},
  187    'INF'  => {alias => 'infinity'},
  188 
  189    'NONE' => {},
  190    'none' => {alias => 'NONE'},
  191 
  192    'DNE'  => {},
  193    'dne'  => {alias => 'DNE'},
  194 
  195 #   'T' => {true => 1},
  196 #   'F' => {false => 1},
  197 };
  198 
  199 $flags = {
  200   ijk => 0,                     # 1 = show vectors in ijk form
  201   reduceConstants => 1,         # 1 = automatically combine constants
  202   reduceConstantFunctions => 1, # 1 = compute function values of constants
  203   showExtraParens => 0,         # 1 = make things painfully unambiguous
  204 };
  205 
  206 ############################################################################
  207 ############################################################################
  208 #
  209 #  Special purpose contexts
  210 #
  211 
  212 use vars qw(%context);
  213 use vars qw($fullContext $numericContext $complexContext
  214       $vectorContext $matrixContext $intervalContext);
  215 
  216 #
  217 #  The default Context
  218 #
  219 $fullContext = new Parser::Context(
  220   operators => $operators,
  221   functions => $functions,
  222   constants => $constants,
  223   variables => $variables,
  224   strings   => $strings,
  225   parens    => $parens,
  226   lists     => $lists,
  227   flags     => $flags,
  228   reduction => $Parser::reduce,
  229 );
  230 
  231 $fullContext->constants->set(
  232   pi => {TeX => '\pi ', perl => ' pi'},
  233   i => {isConstant => 1, perl => ' i'},
  234   j => {TeX => '\boldsymbol{j}', perl => ' j'},
  235   k => {TeX => '\boldsymbol{k}', perl => ' k'},
  236 );
  237 
  238 $fullContext->usePrecedence('Standard');
  239 
  240 #
  241 #  Numeric context (no vectors, matrices or complex numbers)
  242 #
  243 $numericContext = $fullContext->copy;
  244 $numericContext->variables->are(x=>'Real');
  245 $numericContext->operators->undefine('><','.');
  246 $numericContext->functions->undefine('norm','unit','arg','mod','Re','Im','conj');
  247 $numericContext->constants->remove('i','j','k');
  248 $numericContext->parens->remove('<');
  249 $numericContext->parens->set(
  250    '(' => {type => 'List', formMatrix => 0},
  251    '[' => {type => 'List', formMatrix => 0},
  252    '{' => {type => 'List'},
  253 );
  254 
  255 #
  256 #  Complex context (no vectors or matrices)
  257 #
  258 $complexContext = $fullContext->copy;
  259 $complexContext->variables->are(z=>'Complex');
  260 $complexContext->operators->undefine('><','.');
  261 $complexContext->functions->undefine('norm','unit');
  262 $complexContext->constants->remove('j','k');
  263 $complexContext->parens->remove('<');
  264 $complexContext->parens->set(
  265    '(' => {type => 'List', formMatrix => 0},
  266    '[' => {type => 'List', formMatrix => 0},
  267    '{' => {type => 'List'},
  268 );
  269 $complexContext->operators->set(
  270   '^'  => {class => 'Parser::Function::complex_power', negativeIsComplex => 1},
  271   '**' => {class => 'Parser::Function::complex_power', negativeIsComplex => 1},
  272 );
  273 $complexContext->functions->set(
  274   'sqrt' => {class => 'Parser::Function::complex_numeric', negativeIsComplex => 1},
  275   'log'  => {class => 'Parser::Function::complex_numeric', negativeIsComplex => 1},
  276 );
  277 
  278 
  279 #
  280 #  Vector context (no complex numbers)
  281 #
  282 $vectorContext = $fullContext->copy;
  283 $vectorContext->variables->are(x=>'Real',y=>'Real',z=>'Real');
  284 $vectorContext->functions->undefine('arg','mod','Re','Im','conj');
  285 $vectorContext->constants->replace(i=>Value::Vector->new(1,0,0));
  286 $vectorContext->constants->set(i=>{TeX=>'\boldsymbol{i}', perl => ' i'});
  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 => 'Interval'},
  302 );
  303 
  304 #########################################################################
  305 
  306 #
  307 #  list of all default contexts (users can add more)
  308 #
  309 %context = (
  310   Full     => $fullContext,
  311   Numeric  => $numericContext,
  312   Complex  => $complexContext,
  313   Vector   => $vectorContext,
  314   Matrix   => $matrixContext,
  315   Interval => $intervalContext,
  316 );
  317 
  318 #########################################################################
  319 
  320 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9