[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 2607 - (download) (as text) (annotate)
Sat Aug 14 11:26:49 2004 UTC (15 years, 4 months ago) by dpvc
File size: 11755 byte(s)
Allow unary + and - to work on infinities.

    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 => 'all', fullparens => 1},
   36 
   37    ' /' => {precedence => 2.8, associativity => 'left', type => 'bin', string => '/',
   38            class => 'Parser::BOP::divide',
   39            rightparens => 'all', leftparens => 'all', fullparens => 1, hidden => 1},
   40 
   41    '/ ' => {precedence => 2.8, associativity => 'left', type => 'bin', string => '/',
   42            class => 'Parser::BOP::divide',
   43            rightparens => 'all', leftparens => 'all', 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},
   59 
   60    'u-'=> {precedence => 6, associativity => 'left', type => 'unary', string => '-',
   61            class => 'Parser::UOP::minus', hidden => 1, allowInfinite => 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 => '^',
   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'},
   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 };
   87 
   88 $lists = {
   89    'Point'         => {class =>'Parser::List::Point'},
   90    'Vector'        => {class =>'Parser::List::Vector'},
   91    'Matrix'        => {class =>'Parser::List::Matrix', open => '[', close => ']'},
   92    'List'          => {class =>'Parser::List::List'},
   93    'Interval'      => {class =>'Parser::List::Interval'},
   94    'AbsoluteValue' => {class =>'Parser::List::AbsoluteValue'},
   95 };
   96 
   97 $constants = {
   98    'e'  => exp(1),
   99    'pi' => 4*atan2(1,1),
  100    'i'  => Value::Complex->new(0,1),
  101    'j'  => Value::Vector->new(0,1,0),
  102    'k'  => Value::Vector->new(0,0,1),
  103 };
  104 
  105 $variables = {
  106    'x' => 'Real',
  107    'y' => 'Real',
  108    'z' => 'Real',
  109 };
  110 
  111 $functions = {
  112    'sin'   => {class => 'Parser::Function::trig', TeX => '\sin', inverse => 'asin'},
  113    'cos'   => {class => 'Parser::Function::trig', TeX => '\cos', inverse => 'acos'},
  114    'tan'   => {class => 'Parser::Function::trig', TeX => '\tan', inverse => 'atan'},
  115    'sec'   => {class => 'Parser::Function::trig', TeX => '\sec', inverse => 'asec'},
  116    'csc'   => {class => 'Parser::Function::trig', TeX => '\csc', inverse => 'acsc'},
  117    'cot'   => {class => 'Parser::Function::trig', TeX => '\cot', inverse => 'acot'},
  118    'asin'  => {class => 'Parser::Function::trig', TeX => '\sin^{-1}'},
  119    'acos'  => {class => 'Parser::Function::trig', TeX => '\cos^{-1}'},
  120    'atan'  => {class => 'Parser::Function::trig', TeX => '\tan^{-1}'},
  121    'asec'  => {class => 'Parser::Function::trig', TeX => '\sec^{-1}'},
  122    'acsc'  => {class => 'Parser::Function::trig', TeX => '\csc^{-1}'},
  123    'acot'  => {class => 'Parser::Function::trig', TeX => '\cot^{-1}'},
  124 
  125    'sinh'   => {class => 'Parser::Function::hyperbolic', TeX => '\sinh', inverse => 'asinh'},
  126    'cosh'   => {class => 'Parser::Function::hyperbolic', TeX => '\cosh', inverse => 'acosh'},
  127    'tanh'   => {class => 'Parser::Function::hyperbolic', TeX => '\tanh', inverse => 'atanh'},
  128    'sech'   => {class => 'Parser::Function::hyperbolic', inverse => 'asech'},
  129    'csch'   => {class => 'Parser::Function::hyperbolic', inverse => 'acsch'},
  130    'coth'   => {class => 'Parser::Function::hyperbolic', TeX => '\coth', inverse => 'acoth'},
  131    'asinh'  => {class => 'Parser::Function::hyperbolic', TeX => '\sinh^{-1}'},
  132    'acosh'  => {class => 'Parser::Function::hyperbolic', TeX => '\cosh^{-1}'},
  133    'atanh'  => {class => 'Parser::Function::hyperbolic', TeX => '\tanh^{-1}'},
  134    'asech'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm sech}^{-1}'},
  135    'acsch'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm csch}^{-1}'},
  136    'acoth'  => {class => 'Parser::Function::hyperbolic', TeX => '\coth^{-1}'},
  137 
  138    'log'   => {class => 'Parser::Function::numeric', inverse => 'exp', TeX => '\log'},
  139    'log10' => {class => 'Parser::Function::numeric', nocomplex => 1, TeX => '\log_{10}'},
  140    'exp'   => {class => 'Parser::Function::numeric', inverse => 'log', TeX => '\exp'},
  141    'sqrt'  => {class => 'Parser::Function::numeric', braceTeX => 1, TeX => '\sqrt'},
  142    'abs'   => {class => 'Parser::Function::numeric'},
  143    'int'   => {class => 'Parser::Function::numeric'},
  144    'sgn'   => {class => 'Parser::Function::numeric', nocomplex => 1},
  145 
  146    'atan2' => {class => 'Parser::Function::numeric2'},
  147 
  148    'norm'  => {class => 'Parser::Function::vector', vectorInput => 1},
  149    'unit'  => {class => 'Parser::Function::vector', vectorInput => 1},
  150 
  151    'arg'   => {class => 'Parser::Function::complex'},
  152    'mod'   => {class => 'Parser::Function::complex'},
  153    'Re'    => {class => 'Parser::Function::complex', TeX => '\Re'},
  154    'Im'    => {class => 'Parser::Function::complex', TeX => '\Im'},
  155    'conj'  => {class => 'Parser::Function::complex', complex => 1, TeX=>'\overline', braceTeX => 1},
  156 
  157    # Det, Inverse, Transpose, Floor, Ceil
  158 
  159    'arcsin' => {alias => 'asin'},
  160    'arccos' => {alias => 'acos'},
  161    'arctan' => {alias => 'atan'},
  162    'arcsec' => {alias => 'asec'},
  163    'arccsc' => {alias => 'acsc'},
  164    'arccot' => {alias => 'acot'},
  165 
  166    'arcsinh' => {alias => 'asinh'},
  167    'arccosh' => {alias => 'acosh'},
  168    'arctanh' => {alias => 'atanh'},
  169    'arcsech' => {alias => 'asech'},
  170    'arccsch' => {alias => 'acsch'},
  171    'arccoth' => {alias => 'acoth'},
  172 
  173    'ln'     => {alias => 'log'},
  174    'logten' => {alias => 'log10'},
  175 };
  176 
  177 $strings = {
  178    'infinity'  => {infinite => 1},
  179    '-infinity' => {infinite => 1, negative => 1},
  180 #   'T' => {true => 1},
  181 #   'F' => {false => 1},
  182 
  183    '+infinity' => {alias => 'infinity'},
  184    'INFINITY' => {alias => 'infinity'},
  185    '+INFINITY' => {alias => 'infinity'},
  186    '-INFINITY' => {alias => '-infinity'},
  187    'inf'  => {alias => 'infinity'},
  188    '+inf' => {alias => 'infinity'},
  189    '-inf' => {alias => '-infinity'},
  190    'INF'  => {alias => 'infinity'},
  191    '+INF' => {alias => 'infinity'},
  192    '-INF' => {alias => '-infinity'},
  193 };
  194 
  195 $flags = {
  196   ijk => 0,
  197 };
  198 
  199 ############################################################################
  200 ############################################################################
  201 #
  202 #  Special purpose contexts
  203 #
  204 
  205 use vars qw(%context);
  206 use vars qw($fullContext $numericContext $complexContext $vectorContext $intervalContext);
  207 
  208 #
  209 #  The default Context
  210 #
  211 $fullContext = new Parser::Context(
  212   operators => $operators,
  213   functions => $functions,
  214   constants => $constants,
  215   variables => $variables,
  216   strings   => $strings,
  217   parens    => $parens,
  218   lists     => $lists,
  219   flags     => $flags,
  220 );
  221 
  222 $fullContext->constants->set(
  223   pi => {TeX => '\pi ', perl => 'pi'},
  224   i => {isConstant => 1, perl => 'i'},
  225   j => {TeX => '\boldsymbol{j}'},
  226   k => {TeX => '\boldsymbol{k}'},
  227 );
  228 
  229 #
  230 #  Numeric context (no vectors, matrices or complex numbers)
  231 #
  232 $numericContext = $fullContext->copy;
  233 $numericContext->variables->are(x=>'Real');
  234 $numericContext->operators->undefine('><','.');
  235 $numericContext->functions->undefine('norm','unit','arg','mod','Re','Im','conj');
  236 $numericContext->constants->remove('i','j','k');
  237 $numericContext->parens->remove('<');
  238 $numericContext->parens->set(
  239    '(' => {type => 'List', formMatrix => 0},
  240    '[' => {type => 'List', formMatrix => 0},
  241    '{' => {type => 'List'},
  242 );
  243 $numericContext->lists->remove('Point','Vector','Matrix');
  244 
  245 #
  246 #  Complex context (no vectors or matrices)
  247 #
  248 $complexContext = $fullContext->copy;
  249 $complexContext->variables->replace(z=>'Complex');
  250 $complexContext->operators->undefine('><','.');
  251 $complexContext->functions->undefine('norm','unit');
  252 $complexContext->constants->remove('j','k');
  253 $complexContext->parens->remove('<');
  254 $complexContext->parens->set(
  255    '(' => {type => 'List', formMatrix => 0},
  256    '[' => {type => 'List', formMatrix => 0},
  257    '{' => {type => 'List'},
  258 );
  259 $complexContext->lists->remove('Point','Vector','Matrix');
  260 
  261 #
  262 #  Vector context (no complex numbers)
  263 #
  264 $vectorContext = $fullContext->copy;
  265 $vectorContext->variables->are(x=>'Real',y=>'Real',z=>'Real');
  266 $vectorContext->functions->undefine('arg','mod','Re','Im','conj');
  267 $vectorContext->constants->replace(i=>Value::Vector->new(1,0,0));
  268 $vectorContext->constants->set(i=>{TeX=>'\boldsymbol{i}'});
  269 
  270 #
  271 #  Interval context (make intervals rather than lists)
  272 #
  273 $intervalContext = $numericContext->copy;
  274 $intervalContext->parens->set(
  275    '(' => {type => 'Interval'},
  276    '[' => {type => 'Interval'},
  277    '{' => {type => 'Interval'},
  278 );
  279 
  280 #########################################################################
  281 
  282 #
  283 #  list of all default contexts (users can add more)
  284 #
  285 %context = (
  286   Full     => $fullContext,
  287   Numeric  => $numericContext,
  288   Complex  => $complexContext,
  289   Vector   => $vectorContext,
  290   Interval => $intervalContext,
  291 );
  292 
  293 #########################################################################
  294 
  295 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9