[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 3172 - (download) (as text) (annotate)
Tue Feb 15 21:58:54 2005 UTC (14 years, 11 months ago) by dpvc
File size: 5367 byte(s)
Updated the answer checkers so that you can more easily specify how
the correct answer shoudl be displayed.  In the past, you could use
something like Real(sqrt(2))->cmp(correct_ans=>"sqrt(2)") to do this,
but that is awkward.  Now the Compute() function (which parses and
then evaluates a string) sets things up so that the original string
will be what is used as the correct answer.  That means
Compute("sqrt(2)")->cmp will have the same result as the example
above.

You can also set the {correct_ans} properly of any Parser object to
have that value used as the correct answer.  For example

     $x = Real(sqrt(2));
     $x->{correct_ans} = "sqrt(2)";
     ANS($x->cmp)

would also produce the same answer checker as the two previous
examples.  All three methods should work.  Use the one that is most
convenient for you.

    1 ###########################################################################
    2 ##
    3 ##  Set up the functions needed by the Parser.
    4 ##
    5 
    6 if (!$Parser::installed) {
    7   die "\n************************************************************\n" .
    8         "* This problem requires the Parser.pm package, which doesn't\n".
    9         "* seem to be installed.  Please contact your WeBWorK system\n".
   10         "* administrator and ask him or her to install it first.\n".
   11         "************************************************************\n\n"
   12 }
   13 if (!$Value::installed) {
   14   die "\n************************************************************\n" .
   15         "* This problem requires the Value.pm package, which doesn't\n".
   16         "* seem to be installed.  Please contact your WeBWorK system\n".
   17         "* administrator and ask him or her to install it first.\n".
   18         "************************************************************\n\n"
   19 }
   20 
   21 loadMacros("Value.pl");
   22 loadMacros("PGcommonFunctions.pl");
   23 
   24 #
   25 #  The main way to get a formula
   26 #
   27 sub Formula {Value::Formula->new(@_)}
   28 
   29 #
   30 #  Parse a formula and evaluate it
   31 #
   32 sub Compute {
   33   my $string = shift;
   34   my $formula = Formula($string)->eval(@_);
   35   $formula->{correct_ans} = $string;
   36   return $formula;
   37 }
   38 
   39 #
   40 #  Deal with contexts
   41 #
   42 sub Context {Parser::Context->current(\%context,@_)}
   43 %context = ();  # locally defined contexts, including 'current' context
   44 Context();  # Initialize context (for persistent mod_perl)
   45 
   46 ###########################################################################
   47 #
   48 # stubs for trigonometric functions
   49 #
   50 
   51 package Ignore;  ## let PGauxiliaryFunctions.pl do these
   52 
   53 #sub sin {Parser::Function->call('sin',@_)}    # Let overload handle it
   54 #sub cos {Parser::Function->call('cos',@_)}    # Let overload handle it
   55 sub tan {Parser::Function->call('tan',@_)}
   56 sub sec {Parser::Function->call('sec',@_)}
   57 sub csc {Parser::Function->call('csc',@_)}
   58 sub cot {Parser::Function->call('cot',@_)}
   59 
   60 sub asin {Parser::Function->call('asin',@_)}
   61 sub acos {Parser::Function->call('acos',@_)}
   62 sub atan {Parser::Function->call('atan',@_)}
   63 sub asec {Parser::Function->call('asec',@_)}
   64 sub acsc {Parser::Function->call('acsc',@_)}
   65 sub acot {Parser::Function->call('acot',@_)}
   66 
   67 sub arcsin {Parser::Function->call('asin',@_)}
   68 sub arccos {Parser::Function->call('acos',@_)}
   69 sub arctan {Parser::Function->call('atan',@_)}
   70 sub arcsec {Parser::Function->call('asec',@_)}
   71 sub arccsc {Parser::Function->call('acsc',@_)}
   72 sub arccot {Parser::Function->call('acot',@_)}
   73 
   74 ###########################################################################
   75 #
   76 # stubs for hyperbolic functions
   77 #
   78 
   79 sub sinh {Parser::Function->call('sinh',@_)}
   80 sub cosh {Parser::Function->call('cosh',@_)}
   81 sub tanh {Parser::Function->call('tanh',@_)}
   82 sub sech {Parser::Function->call('sech',@_)}
   83 sub csch {Parser::Function->call('csch',@_)}
   84 sub coth {Parser::Function->call('coth',@_)}
   85 
   86 sub asinh {Parser::Function->call('asinh',@_)}
   87 sub acosh {Parser::Function->call('acosh',@_)}
   88 sub atanh {Parser::Function->call('atanh',@_)}
   89 sub asech {Parser::Function->call('asech',@_)}
   90 sub acsch {Parser::Function->call('acsch',@_)}
   91 sub acoth {Parser::Function->call('acoth',@_)}
   92 
   93 sub arcsinh {Parser::Function->call('asinh',@_)}
   94 sub arccosh {Parser::Function->call('acosh',@_)}
   95 sub arctanh {Parser::Function->call('atanh',@_)}
   96 sub arcsech {Parser::Function->call('asech',@_)}
   97 sub arccsch {Parser::Function->call('acsch',@_)}
   98 sub arccoth {Parser::Function->call('acoth',@_)}
   99 
  100 ###########################################################################
  101 #
  102 # stubs for numeric functions
  103 #
  104 
  105 #sub log   {Parser::Function->call('log',@_)}    # Let overload handle it
  106 sub log10 {Parser::Function->call('log10',@_)}
  107 #sub exp   {Parser::Function->call('exp',@_)}    # Let overload handle it
  108 #sub sqrt  {Parser::Function->call('sqrt',@_)}    # Let overload handle it
  109 #sub abs   {Parser::Function->call('abs',@_)}    # Let overload handle it
  110 sub int   {Parser::Function->call('int',@_)}
  111 sub sgn   {Parser::Function->call('sgn',@_)}
  112 
  113 sub ln     {Parser::Function->call('log',@_)}
  114 sub logten {Parser::Function->call('log10',@_)}
  115 
  116 package main;  ##  back to main
  117 
  118 sub log10 {Parser::Function->call('log10',@_)}
  119 sub Factorial {Parser::UOP::factorial->call(@_)}
  120 
  121 ###########################################################################
  122 #
  123 # stubs for special functions
  124 #
  125 
  126 #sub atan2 {Parser::Function->call('atan2',@_)}    # Let overload handle it
  127 
  128 ###########################################################################
  129 #
  130 # stubs for numeric functions
  131 #
  132 
  133 sub arg  {Parser::Function->call('arg',@_)}
  134 sub mod  {Parser::Function->call('mod',@_)}
  135 sub Re   {Parser::Function->call('Re',@_)}
  136 sub Im   {Parser::Function->call('Im',@_)}
  137 sub conj {Parser::Function->call('conj',@_)}
  138 
  139 ###########################################################################
  140 #
  141 # stubs for vector functions
  142 #
  143 
  144 sub norm {Parser::Function->call('norm',@_)}
  145 sub unit {Parser::Function->call('unit',@_)}
  146 
  147 #
  148 #  These need to be in dangerousMacros.pl for some reason
  149 #
  150 #sub i () {Compute('i')}
  151 #sub j () {Compute('j')}
  152 #sub k () {Compute('k')}
  153 
  154 ###########################################################################
  155 
  156 $_parser_loaded = 1;  #  use this to tell if Parser.pl is loaded
  157 
  158 sub _Parser_init {}; # don't let loadMacros load it again
  159 
  160 ###########################################################################
  161 
  162 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9