[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 2653 - (download) (as text) (annotate)
Thu Aug 19 17:31:18 2004 UTC (15 years, 3 months ago) by dpvc
File size: 11683 byte(s)
Fixed error with handling of +infinity and -infinity.
(They had been defined as strings, but now we can let the + and -
operators handle them).

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9