Files Defining MathObjects

From WeBWorK_wiki
Jump to navigation Jump to search

Files defining MathObjects

The MathObjects library is contained in two Perl packages: the Parser package and the Value package. The first of these implements the parser that converts student answers (or strings passed to Compute() or the MathObject constructor functions) into the corresponding MathObjects. The second implements the mathematical operations and functions for the various object types. The files that define the Parser package are pg/lib/Parser.pm and the files in the directory pg/lib/Parser/, while the Value package is defined in pg/lib/Value.pm and the directory pg/lib/Value/. There are also two macro files pg/macros/Parser.pl and pg/macros/Value.pl that define the object constructors and other values needed by PG problems that use MathObjects. The pg/macros/MathObjects.pl file is a wrapper that loads these two, and is the file that you need to include in your loadMacros() call in order to use MathObjects in a problem you are writing.

The pg/lib/Value/ directory includes files for each of the MathObject types (e.g., Real.pm, Complex.pm, etc.). These define the various object types and how they operate. For example, these implement how operations like addition and multiplication work for each object class, how functions like sin() and sqrt() work, how to compare two objects for equality and numeric order, how to display the object in TeX and string form, how to covert the object to Perl code, and so on.

The pg/lib/Parser/ directory contains files that make it possible to break up a string into its various tokens and map them to build the parse tree for the expression. For example, there are binary-operator and unary-operator classes that form the nodes of an expression tree. There are also function classes to handle function calls, list classes to handle points, vectors, and so on, and classes for the various values that can be produced, like numbers and strings. The Context stores information that the parser uses to create the parse tree, so the various entities like variables, constants, strings, operators, functions, parens, and so on that you can set in the Context correspond directly to the classes defined in pg/lib/Parser/. The Parser classes are related to the ones defined in the Value package, but are not the same. The former are used to build the parse tree, while that latter are used to compute specific instances of the objects.