[system] / trunk / pg / macros / Parser.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/Parser.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5399 - (download) (as text) (annotate)
Wed Aug 22 19:49:36 2007 UTC (12 years, 6 months ago) by dpvc
File size: 6765 byte(s)
Allow contexts to override the MathObject constructors separately from
the ones generated by the parser.  That way, you can define Real(),
for example, to produce ScientificNotation, without screwing up the
reals used in the parser itself.

    1 
    2 
    3 =head1 DESCRIPTION
    4 
    5  ###########################################################################
    6  ##
    7  ##  Set up the functions needed by the Parser.
    8  ##
    9 
   10 =cut
   11 
   12 if (!$Parser::installed) {
   13   die "\n************************************************************\n" .
   14         "* This problem requires the Parser.pm package, which doesn't\n".
   15         "* seem to be installed.  Please contact your WeBWorK system\n".
   16         "* administrator and ask him or her to install it first.\n".
   17         "************************************************************\n\n"
   18 }
   19 if (!$Value::installed) {
   20   die "\n************************************************************\n" .
   21         "* This problem requires the Value.pm package, which doesn't\n".
   22         "* seem to be installed.  Please contact your WeBWorK system\n".
   23         "* administrator and ask him or her to install it first.\n".
   24         "************************************************************\n\n"
   25 }
   26 
   27 loadMacros("Value.pl");
   28 loadMacros("PGcommonFunctions.pl");
   29 
   30 =head3 Formula("formula")
   31 
   32  #
   33  #  The main way to get a MathObject Formula object (an equation
   34  #  that depends on one or more variables).
   35  #
   36 
   37 =cut
   38 
   39 
   40 sub Formula {Value->Package("Formula()")->new(@_)}
   41 
   42 
   43 
   44 =head3 Compute("formula"[,var=>value,...])
   45 
   46  #
   47  #  Compute the value of a formula and return a MathObject appropriate
   48  #  to its value.  Set the object so that the correct answer will be
   49  #  shown exatly as in the given string rather than by its usual
   50  #  stringification.  If the value is a Formula and any var=>value
   51  #  pairs are specified, then the formula will be evaluated using
   52  #  the given variable values.  E.g.,
   53  #
   54  #    $x = Compute("x+3",x=>2)
   55  #
   56  #  will produce the equivalent of $x = Real(5).
   57  #
   58  #  The original parsed formula will be saved in the object's
   59  #  original_formula field, and can be obtained by
   60  #
   61  #    $x->{original_formula};
   62  #
   63  #  if needed later in the problem.
   64  #
   65 
   66 =cut
   67 
   68 sub Compute {
   69   my $string = shift;
   70   my $formula = Formula($string);
   71   $formula = $formula->{tree}->Compute if $formula->{tree}{canCompute};
   72   if (scalar(@_) || $formula->isConstant) {
   73     my $f = $formula;
   74     $formula = $formula->eval(@_);
   75     $formula->{original_formula} = $f;
   76   }
   77   $formula->{correct_ans} = $string;
   78   return $formula;
   79 }
   80 
   81 =head3 Context(), Context(name) or Context(context)
   82 
   83  #
   84  #  Set or get the current context.  When a name is given, the context
   85  #  with that name is selected as the current context.  When a context
   86  #  reference is provided, that context is set as the current one.  In
   87  #  all three cases, the current context (after setting) is returned.
   88  #
   89 
   90 =cut
   91 
   92 sub Context {Parser::Context->current(\%context,@_)}
   93 %context = ();  # Locally defined contexts, including 'current' context
   94 Context();      # Initialize context (for persistent mod_perl)
   95 
   96 ###########################################################################
   97 #
   98 # stubs for trigonometric functions
   99 #
  100 
  101 package Ignore;  ## let PGauxiliaryFunctions.pl do these
  102 
  103 #sub sin {Parser::Function->call('sin',@_)}    # Let overload handle it
  104 #sub cos {Parser::Function->call('cos',@_)}    # Let overload handle it
  105 sub tan {Parser::Function->call('tan',@_)}
  106 sub sec {Parser::Function->call('sec',@_)}
  107 sub csc {Parser::Function->call('csc',@_)}
  108 sub cot {Parser::Function->call('cot',@_)}
  109 
  110 sub asin {Parser::Function->call('asin',@_)}
  111 sub acos {Parser::Function->call('acos',@_)}
  112 sub atan {Parser::Function->call('atan',@_)}
  113 sub asec {Parser::Function->call('asec',@_)}
  114 sub acsc {Parser::Function->call('acsc',@_)}
  115 sub acot {Parser::Function->call('acot',@_)}
  116 
  117 sub arcsin {Parser::Function->call('asin',@_)}
  118 sub arccos {Parser::Function->call('acos',@_)}
  119 sub arctan {Parser::Function->call('atan',@_)}
  120 sub arcsec {Parser::Function->call('asec',@_)}
  121 sub arccsc {Parser::Function->call('acsc',@_)}
  122 sub arccot {Parser::Function->call('acot',@_)}
  123 
  124 ###########################################################################
  125 #
  126 # stubs for hyperbolic functions
  127 #
  128 
  129 sub sinh {Parser::Function->call('sinh',@_)}
  130 sub cosh {Parser::Function->call('cosh',@_)}
  131 sub tanh {Parser::Function->call('tanh',@_)}
  132 sub sech {Parser::Function->call('sech',@_)}
  133 sub csch {Parser::Function->call('csch',@_)}
  134 sub coth {Parser::Function->call('coth',@_)}
  135 
  136 sub asinh {Parser::Function->call('asinh',@_)}
  137 sub acosh {Parser::Function->call('acosh',@_)}
  138 sub atanh {Parser::Function->call('atanh',@_)}
  139 sub asech {Parser::Function->call('asech',@_)}
  140 sub acsch {Parser::Function->call('acsch',@_)}
  141 sub acoth {Parser::Function->call('acoth',@_)}
  142 
  143 sub arcsinh {Parser::Function->call('asinh',@_)}
  144 sub arccosh {Parser::Function->call('acosh',@_)}
  145 sub arctanh {Parser::Function->call('atanh',@_)}
  146 sub arcsech {Parser::Function->call('asech',@_)}
  147 sub arccsch {Parser::Function->call('acsch',@_)}
  148 sub arccoth {Parser::Function->call('acoth',@_)}
  149 
  150 ###########################################################################
  151 #
  152 # stubs for numeric functions
  153 #
  154 
  155 #sub log   {Parser::Function->call('log',@_)}    # Let overload handle it
  156 sub log10 {Parser::Function->call('log10',@_)}
  157 #sub exp   {Parser::Function->call('exp',@_)}    # Let overload handle it
  158 #sub sqrt  {Parser::Function->call('sqrt',@_)}    # Let overload handle it
  159 #sub abs   {Parser::Function->call('abs',@_)}    # Let overload handle it
  160 sub int   {Parser::Function->call('int',@_)}
  161 sub sgn   {Parser::Function->call('sgn',@_)}
  162 
  163 sub ln     {Parser::Function->call('log',@_)}
  164 sub logten {Parser::Function->call('log10',@_)}
  165 
  166 package main;  ##  back to main
  167 
  168 sub log10 {Parser::Function->call('log10',@_)}
  169 sub Factorial {Parser::UOP::factorial->call(@_)}
  170 
  171 ###########################################################################
  172 #
  173 # stubs for special functions
  174 #
  175 
  176 #sub atan2 {Parser::Function->call('atan2',@_)}    # Let overload handle it
  177 
  178 ###########################################################################
  179 #
  180 # stubs for numeric functions
  181 #
  182 
  183 sub arg  {Parser::Function->call('arg',@_)}
  184 sub mod  {Parser::Function->call('mod',@_)}
  185 sub Re   {Parser::Function->call('Re',@_)}
  186 sub Im   {Parser::Function->call('Im',@_)}
  187 sub conj {Parser::Function->call('conj',@_)}
  188 
  189 ###########################################################################
  190 #
  191 # stubs for vector functions
  192 #
  193 
  194 sub norm {Parser::Function->call('norm',@_)}
  195 sub unit {Parser::Function->call('unit',@_)}
  196 
  197 #
  198 #  These need to be in dangerousMacros.pl for some reason
  199 #
  200 #sub i () {Compute('i')}
  201 #sub j () {Compute('j')}
  202 #sub k () {Compute('k')}
  203 
  204 ###########################################################################
  205 
  206 $_parser_loaded = 1;  #  use this to tell if Parser.pl is loaded
  207 
  208 sub _Parser_init {}; # don't let loadMacros load it again
  209 
  210 loadMacros("parserCustomization.pl");
  211 
  212 ###########################################################################
  213 
  214 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9