[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 5663 - (download) (as text) (annotate)
Thu May 8 00:37:31 2008 UTC (11 years, 9 months ago) by sh002i
File size: 10753 byte(s)
add "my" and "our" specifiers to ^variable definitions. (This breaks
compatibility with the current version of ww-symbol-map, but it is the
format that the upcoming version will use. I hope to commit that new
version soon but testing is taking longer than expected.)

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/Parser.pl,v 1.15 2008/05/03 17:43:29 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 Parser.pl - Macro-based fronted to the MathObjects system.
   20 
   21 =cut
   22 
   23 ###########################################################################
   24 ##
   25 ##  Set up the functions needed by the Parser.
   26 ##
   27 
   28 # ^uses $Parser::installed
   29 # ^uses $Value::installed
   30 if (!$Parser::installed) {
   31   die "\n************************************************************\n" .
   32         "* This problem requires the Parser.pm package, which doesn't\n".
   33         "* seem to be installed.  Please contact your WeBWorK system\n".
   34         "* administrator and ask him or her to install it first.\n".
   35         "************************************************************\n\n"
   36 }
   37 if (!$Value::installed) {
   38   die "\n************************************************************\n" .
   39         "* This problem requires the Value.pm package, which doesn't\n".
   40         "* seem to be installed.  Please contact your WeBWorK system\n".
   41         "* administrator and ask him or her to install it first.\n".
   42         "************************************************************\n\n"
   43 }
   44 
   45 # ^uses loadMacros
   46 loadMacros("Value.pl");
   47 loadMacros("PGcommonFunctions.pl");
   48 
   49 =head1 MACROS
   50 
   51 =head1 Formula
   52 
   53   Formula("formula");
   54 
   55 The main way to get a MathObject Formula object (an equation that depends on one
   56 or more variables).
   57 
   58 =cut
   59 
   60 # ^function Formula
   61 # ^uses Value::Package
   62 sub Formula {Value->Package("Formula()")->new(@_)}
   63 
   64 =head2 Compute
   65 
   66   Compute("formula"[, var=>value, ...]);
   67 
   68 Compute the value of a formula and return a MathObject appropriate to its
   69 value.  Set the object so that the correct answer will be shown exatly as in the
   70 given string rather than by its usual stringification.  If the value is a
   71 Formula and any var=>value pairs are specified, then the formula will be
   72 evaluated using the given variable values.  E.g.,
   73 
   74   $x = Compute("x+3",x=>2)
   75 
   76 will produce the equivalent of $x = Real(5).
   77 
   78 The original parsed formula will be saved in the object's original_formula
   79 field, and can be obtained by
   80 
   81   $x->{original_formula};
   82 
   83 if needed later in the problem.
   84 
   85 =cut
   86 
   87 # ^function Compute
   88 # ^uses Formula
   89 sub Compute {
   90   my $string = shift;
   91   my $formula = Formula($string);
   92   $formula = $formula->{tree}->Compute if $formula->{tree}{canCompute};
   93   if (scalar(@_) || $formula->isConstant) {
   94     my $f = $formula;
   95     $formula = $formula->eval(@_);
   96     $formula->{original_formula} = $f;
   97   }
   98   $formula->{correct_ans} = $string;
   99   return $formula;
  100 }
  101 
  102 =head2 Context
  103 
  104   Context();
  105   Context($name);
  106   Context($context);
  107 
  108 Set or get the current context.  When a name is given, the context with that
  109 name is selected as the current context.  When a context reference is provided,
  110 that context is set as the current one.  In all three cases, the current context
  111 (after setting) is returned.
  112 
  113 =cut
  114 
  115 # ^function Context
  116 # ^uses Parser::Context::current
  117 # ^uses %context
  118 sub Context {Parser::Context->current(\%context,@_)}
  119 # ^variable our %context
  120 %context = ();  # Locally defined contexts, including 'current' context
  121 # ^uses Context
  122 Context();      # Initialize context (for persistent mod_perl)
  123 
  124 ###########################################################################
  125 #
  126 # stubs for trigonometric functions
  127 #
  128 
  129 # ^package Ignore
  130 package Ignore;  ## let PGauxiliaryFunctions.pl do these
  131 
  132 # ^#function sin
  133 # ^#uses Parser::Function::call
  134 #sub sin {Parser::Function->call('sin',@_)}    # Let overload handle it
  135 # ^#function cos
  136 # ^#uses Parser::Function::call
  137 #sub cos {Parser::Function->call('cos',@_)}    # Let overload handle it
  138 # ^function tan
  139 # ^uses Parser::Function::call
  140 sub tan {Parser::Function->call('tan',@_)}
  141 # ^function sec
  142 # ^uses Parser::Function::call
  143 sub sec {Parser::Function->call('sec',@_)}
  144 # ^function csc
  145 # ^uses Parser::Function::call
  146 sub csc {Parser::Function->call('csc',@_)}
  147 # ^function cot
  148 # ^uses Parser::Function::call
  149 sub cot {Parser::Function->call('cot',@_)}
  150 
  151 # ^function asin
  152 # ^uses Parser::Function::call
  153 sub asin {Parser::Function->call('asin',@_)}
  154 # ^function acos
  155 # ^uses Parser::Function::call
  156 sub acos {Parser::Function->call('acos',@_)}
  157 # ^function atan
  158 # ^uses Parser::Function::call
  159 sub atan {Parser::Function->call('atan',@_)}
  160 # ^function asec
  161 # ^uses Parser::Function::call
  162 sub asec {Parser::Function->call('asec',@_)}
  163 # ^function acsc
  164 # ^uses Parser::Function::call
  165 sub acsc {Parser::Function->call('acsc',@_)}
  166 # ^function acot
  167 # ^uses Parser::Function::call
  168 sub acot {Parser::Function->call('acot',@_)}
  169 
  170 # ^function arcsin
  171 # ^uses Parser::Function::call
  172 sub arcsin {Parser::Function->call('asin',@_)}
  173 # ^function arccos
  174 # ^uses Parser::Function::call
  175 sub arccos {Parser::Function->call('acos',@_)}
  176 # ^function arctan
  177 # ^uses Parser::Function::call
  178 sub arctan {Parser::Function->call('atan',@_)}
  179 # ^function arcsec
  180 # ^uses Parser::Function::call
  181 sub arcsec {Parser::Function->call('asec',@_)}
  182 # ^function arccsc
  183 # ^uses Parser::Function::call
  184 sub arccsc {Parser::Function->call('acsc',@_)}
  185 # ^function arccot
  186 # ^uses Parser::Function::call
  187 sub arccot {Parser::Function->call('acot',@_)}
  188 
  189 ###########################################################################
  190 #
  191 # stubs for hyperbolic functions
  192 #
  193 
  194 # ^function sinh
  195 # ^uses Parser::Function::call
  196 sub sinh {Parser::Function->call('sinh',@_)}
  197 # ^function cosh
  198 # ^uses Parser::Function::call
  199 sub cosh {Parser::Function->call('cosh',@_)}
  200 # ^function tanh
  201 # ^uses Parser::Function::call
  202 sub tanh {Parser::Function->call('tanh',@_)}
  203 # ^function sech
  204 # ^uses Parser::Function::call
  205 sub sech {Parser::Function->call('sech',@_)}
  206 # ^function csch
  207 # ^uses Parser::Function::call
  208 sub csch {Parser::Function->call('csch',@_)}
  209 # ^function coth
  210 # ^uses Parser::Function::call
  211 sub coth {Parser::Function->call('coth',@_)}
  212 
  213 # ^function asinh
  214 # ^uses Parser::Function::call
  215 sub asinh {Parser::Function->call('asinh',@_)}
  216 # ^function acosh
  217 # ^uses Parser::Function::call
  218 sub acosh {Parser::Function->call('acosh',@_)}
  219 # ^function atanh
  220 # ^uses Parser::Function::call
  221 sub atanh {Parser::Function->call('atanh',@_)}
  222 # ^function asech
  223 # ^uses Parser::Function::call
  224 sub asech {Parser::Function->call('asech',@_)}
  225 # ^function acsch
  226 # ^uses Parser::Function::call
  227 sub acsch {Parser::Function->call('acsch',@_)}
  228 # ^function acoth
  229 # ^uses Parser::Function::call
  230 sub acoth {Parser::Function->call('acoth',@_)}
  231 
  232 # ^function arcsinh
  233 # ^uses Parser::Function::call
  234 sub arcsinh {Parser::Function->call('asinh',@_)}
  235 # ^function arccosh
  236 # ^uses Parser::Function::call
  237 sub arccosh {Parser::Function->call('acosh',@_)}
  238 # ^function arctanh
  239 # ^uses Parser::Function::call
  240 sub arctanh {Parser::Function->call('atanh',@_)}
  241 # ^function arcsech
  242 # ^uses Parser::Function::call
  243 sub arcsech {Parser::Function->call('asech',@_)}
  244 # ^function arccsch
  245 # ^uses Parser::Function::call
  246 sub arccsch {Parser::Function->call('acsch',@_)}
  247 # ^function arccoth
  248 # ^uses Parser::Function::call
  249 sub arccoth {Parser::Function->call('acoth',@_)}
  250 
  251 ###########################################################################
  252 #
  253 # stubs for numeric functions
  254 #
  255 
  256 # ^#function log
  257 # ^#uses Parser::Function::call
  258 #sub log   {Parser::Function->call('log',@_)}    # Let overload handle it
  259 # ^function log10
  260 # ^uses Parser::Function::call
  261 sub log10 {Parser::Function->call('log10',@_)}
  262 # ^#function exp
  263 # ^#uses Parser::Function::call
  264 #sub exp   {Parser::Function->call('exp',@_)}    # Let overload handle it
  265 # ^#function sqrt
  266 # ^#uses Parser::Function::call
  267 #sub sqrt  {Parser::Function->call('sqrt',@_)}    # Let overload handle it
  268 # ^#function abs
  269 # ^#uses Parser::Function::call
  270 #sub abs   {Parser::Function->call('abs',@_)}    # Let overload handle it
  271 # ^function int
  272 # ^uses Parser::Function::call
  273 sub int   {Parser::Function->call('int',@_)}
  274 # ^function sgn
  275 # ^uses Parser::Function::call
  276 sub sgn   {Parser::Function->call('sgn',@_)}
  277 
  278 # ^function ln
  279 # ^uses Parser::Function::call
  280 sub ln     {Parser::Function->call('log',@_)}
  281 # ^function logten
  282 # ^uses Parser::Function::call
  283 sub logten {Parser::Function->call('log10',@_)}
  284 
  285 # ^package main
  286 package main;  ##  back to main
  287 
  288 # ^function log10
  289 # ^uses Parser::Function::call
  290 sub log10 {Parser::Function->call('log10',@_)}
  291 # ^function Factorial
  292 # ^uses Parser::UOP::factorial::call
  293 sub Factorial {Parser::UOP::factorial->call(@_)}
  294 
  295 ###########################################################################
  296 #
  297 # stubs for special functions
  298 #
  299 
  300 # ^#function atan2
  301 # ^#usesParser::Function::call
  302 #sub atan2 {Parser::Function->call('atan2',@_)}    # Let overload handle it
  303 
  304 ###########################################################################
  305 #
  306 # stubs for numeric functions
  307 #
  308 
  309 # ^function arg
  310 # ^uses Parser::Function::call
  311 sub arg  {Parser::Function->call('arg',@_)}
  312 # ^function mod
  313 # ^uses Parser::Function::call
  314 sub mod  {Parser::Function->call('mod',@_)}
  315 # ^function Re
  316 # ^uses Parser::Function::call
  317 sub Re   {Parser::Function->call('Re',@_)}
  318 # ^function Im
  319 # ^uses Parser::Function::call
  320 sub Im   {Parser::Function->call('Im',@_)}
  321 # ^function conj
  322 # ^uses Parser::Function::call
  323 sub conj {Parser::Function->call('conj',@_)}
  324 
  325 ###########################################################################
  326 #
  327 # stubs for vector functions
  328 #
  329 
  330 # ^function norm
  331 # ^uses Parser::Function::call
  332 sub norm {Parser::Function->call('norm',@_)}
  333 # ^function unit
  334 # ^uses Parser::Function::call
  335 sub unit {Parser::Function->call('unit',@_)}
  336 
  337 #
  338 #  These need to be in dangerousMacros.pl for some reason
  339 #
  340 #sub i () {Compute('i')}
  341 #sub j () {Compute('j')}
  342 #sub k () {Compute('k')}
  343 
  344 ###########################################################################
  345 
  346 # ^variable our $_parser_loaded
  347 $_parser_loaded = 1;  #  use this to tell if Parser.pl is loaded
  348 
  349 # ^function _Parser_init
  350 sub _Parser_init {}; # don't let loadMacros load it again
  351 
  352 # ^uses loadMacros
  353 loadMacros("parserCustomization.pl");
  354 
  355 ###########################################################################
  356 
  357 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9