This is the model class as opposed to CalculatorApplet, which is the controller class and is the real power behind the calculator: it does almost all of the actual calculations. More...
Public Member Functions | |
| Parser () | |
| OObject | evaluate (AngleType angleType) |
| This is the main evaluation function. | |
| int | size () |
| How many objects (usually PObject objects) in the expression? | |
| void | add (GObject o) |
| Put a PObject object on the end of the expression. | |
| void | add (int index, GObject o) |
| Put a GObject object at a specified position. | |
| PObject | del (int index) |
| Delete GObject at position index. | |
| String | getExpression () |
| Convert expression to a string. | |
| String | getExpression (int n) |
| Convert expression to a string. | |
| void | clearExpression () |
| When you press AC you need a new expression. | |
| boolean | isEmpty () |
| Are there any objects in the current expression. | |
| final PObject | getLast () |
| Get the last object. | |
| boolean | del () |
| Delete GObject at position index. | |
| java.util.LinkedList< PObject > | getList () |
| We need to can get a copy of the expression if we move up the history list. | |
| void | setList (java.util.LinkedList< PObject > newlist) |
| If we move up or down the history list we change the current expression using this function. | |
| final Base | base () |
| Parser keeps a copy of the Base. | |
| void | base (Base base) |
| Parser keeps a copy of the Base. | |
Static Public Member Functions | |
| static String | font_size (float fontSize) |
| May be obsolete: used to witch font size within HTML. | |
| static void | main (String args[]) |
| Test code. | |
Private Member Functions | |
| void | convertExponentsToNumerals (java.util.List< GObject > list) |
| Convert exponents into a form that can be read directly. | |
| void | convertNumerals (java.util.List< GObject > list) |
| Convert anything that is a Numeral into a Complex Exponents are also converted implicitly. | |
| void | convertContainers (java.util.List< GObject > list) |
| Convert anything that is a Container. | |
| void | convertIs (java.util.List< GObject > list) |
| Deal with all instances of I (square root of negative one). | |
| void | convertVariables (java.util.List< GObject > list) |
| Convert variables into output form. | |
| void | convertARFunctions (java.util.List< GObject > list) |
| This is where unary plus/minus is handled. | |
| void | convertLFunctions (java.util.List< GObject > list) |
| Deal with Square and like left functions. | |
| void | convertDFunctions (java.util.List< GObject > list) |
| Deal with functions like Power that have both left and right arguments. | |
| void | convertRFunctions (java.util.List< GObject > list) |
| Deal with functions like SquareRoot that have right argument only. | |
| void | convertMFunctions (java.util.List< GObject > list) |
| Deal with multiplication and division. | |
| void | convertAFunctions (java.util.List< GObject > list) |
| Deal with addition and subtraction. | |
| void | convertBoolFunctions (java.util.List< GObject > list) |
| Deal with boolean functions. | |
| void | convertToProduct (java.util.List< GObject > list) |
| Takes final expression and multiplies all Complex numbers together. | |
| boolean | stripParenthesis (java.util.List< GObject > list) |
| Tries to strip a pair of parentheses. | |
| java.util.Vector< String > | getStringVector (int q) |
| Find an array of String objects representing the first q GObject objects. | |
| java.util.Vector< String > | getListAsStringVector () |
Private Attributes | |
| java.util.LinkedList< GObject > | list |
| The expression: a list of objects. | |
| Base | base |
| The base. | |
This is the model class as opposed to CalculatorApplet, which is the controller class and is the real power behind the calculator: it does almost all of the actual calculations.
The only calculations that CalculatorApplet does are those involving memory and only then at the request of the Parser object
Parser does not itself know how to calculate anything. Rather it keeps an ordered list of PObject objects representing the expression to be evaluated. It then uses the PObject class hierarchy to decide which PObject objects to evaluate first, which next and so on. Thus it starts by taking apart parentheses and converting digits to Complex numbers. Then it carries out various functions, multiplication and division, addition and subtraction and finally boolean operations to create a result, which it gives to the CalculatorApplet object as a OObject.
| jscicalc.Parser.Parser | ( | ) |
References jscicalc.Parser.base(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.main().
| void jscicalc.Parser.add | ( | GObject | o | ) |
Put a PObject object on the end of the expression.
| o | The PObject to add |
References jscicalc.Parser.list.
Referenced by jscicalc.EntryLabel.insert(), jscicalc.Parser.main(), jscicalc.complex.Test.parse(), and jscicalc.complex.Test.parse2().
| void jscicalc.Parser.add | ( | int | index, | |
| GObject | o | |||
| ) |
Put a GObject object at a specified position.
We need this if the expression is editable. Otherwise we could not add elements at an arbitrary position.
| index | The position at which to insert the object | |
| o | The GObject to add |
References jscicalc.Parser.list.
| final Base jscicalc.Parser.base | ( | ) |
Parser keeps a copy of the Base.
We do this because the Parser object does not know about the CalculatorApplet object: the controller is hidden from the model.
References jscicalc.Parser.base.
Referenced by jscicalc.Parser.convertNumerals(), and jscicalc.Parser.Parser().
| void jscicalc.Parser.base | ( | Base | base | ) |
Parser keeps a copy of the Base.
We do this because the Parser object does not know about the CalculatorApplet object: the controller is hidden from the model. Value gets set by CalculatorApplet.
| base | The new base |
| void jscicalc.Parser.clearExpression | ( | ) |
When you press AC you need a new expression.
References jscicalc.Parser.list.
Referenced by jscicalc.EntryLabel.clear(), jscicalc.complex.Test.parse(), and jscicalc.complex.Test.parse2().
| void jscicalc.Parser.convertAFunctions | ( | java.util.List< GObject > | list | ) | [private] |
Deal with addition and subtraction.
Calls convertMFunctions first.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertMFunctions(), jscicalc.pobject.AFunction.function(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertBoolFunctions().
| void jscicalc.Parser.convertARFunctions | ( | java.util.List< GObject > | list | ) | [private] |
This is where unary plus/minus is handled.
Implicitly calls convertIs.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertVariables(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertLFunctions().
| void jscicalc.Parser.convertBoolFunctions | ( | java.util.List< GObject > | list | ) | [private] |
Deal with boolean functions.
Calls convertMFunctions first.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertAFunctions(), jscicalc.pobject.BoolFunction.function(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertToProduct().
| void jscicalc.Parser.convertContainers | ( | java.util.List< GObject > | list | ) | [private] |
Convert anything that is a Container.
Numerals and exponents are also done before containers are handled.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertNumerals(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertIs().
| void jscicalc.Parser.convertDFunctions | ( | java.util.List< GObject > | list | ) | [private] |
Deal with functions like Power that have both left and right arguments.
Calls convertLFunctions first.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertLFunctions(), jscicalc.pobject.DFunction.function(), jscicalc.Parser.list, and jscicalc.OObject.negate().
Referenced by jscicalc.Parser.convertRFunctions().
| void jscicalc.Parser.convertExponentsToNumerals | ( | java.util.List< GObject > | list | ) | [private] |
Convert exponents into a form that can be read directly.
Exponents get converted early in the process of evaluating an expression.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertNumerals().
| void jscicalc.Parser.convertIs | ( | java.util.List< GObject > | list | ) | [private] |
Deal with all instances of I (square root of negative one).
Implicitly calls convertContainers.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertContainers(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertVariables().
| void jscicalc.Parser.convertLFunctions | ( | java.util.List< GObject > | list | ) | [private] |
Deal with Square and like left functions.
Calls convertARFunction (through convertExponents) first.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertARFunctions(), jscicalc.pobject.LFunction.function(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertDFunctions().
| void jscicalc.Parser.convertMFunctions | ( | java.util.List< GObject > | list | ) | [private] |
Deal with multiplication and division.
Calls convertRFunctions first.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertRFunctions(), jscicalc.pobject.MFunction.function(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertAFunctions().
| void jscicalc.Parser.convertNumerals | ( | java.util.List< GObject > | list | ) | [private] |
Convert anything that is a Numeral into a Complex Exponents are also converted implicitly.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.base(), jscicalc.Parser.convertExponentsToNumerals(), jscicalc.Parser.list, and jscicalc.pobject.Numeral.name().
Referenced by jscicalc.Parser.convertContainers().
| void jscicalc.Parser.convertRFunctions | ( | java.util.List< GObject > | list | ) | [private] |
Deal with functions like SquareRoot that have right argument only.
Calls convertDFunctions first.
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertDFunctions(), jscicalc.pobject.RFunction.function(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertMFunctions().
| void jscicalc.Parser.convertToProduct | ( | java.util.List< GObject > | list | ) | [private] |
Takes final expression and multiplies all Complex numbers together.
Calls convertBoolFunctions first (and cascades)
| list | A list of tokens to be evaluated. |
References jscicalc.Parser.convertBoolFunctions(), jscicalc.Parser.list, and jscicalc.OObject.multiply().
Referenced by jscicalc.Parser.evaluate(), and jscicalc.Parser.stripParenthesis().
| void jscicalc.Parser.convertVariables | ( | java.util.List< GObject > | list | ) | [private] |
Convert variables into output form.
| list | A list of tokens to be evaluated. Nothing else is done implicitly by this method. |
References jscicalc.Parser.convertIs(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.convertARFunctions().
| PObject jscicalc.Parser.del | ( | int | index | ) |
Delete GObject at position index.
Used for editing expressions.
| index | The position at which to delete |
References jscicalc.Parser.list.
| boolean jscicalc.Parser.del | ( | ) |
Delete GObject at position index.
Used for editing expressions. This version is probably obsolete.
References jscicalc.Parser.list.
This is the main evaluation function.
It works by a finding suitable subexpressions and calling a cascade of methods to evaluate the expression in the correct sequence. Thus the parser works largely by recursion on what can be thought of as a tree of PObjects defined by the sequence of Parser methods and the PObject hierarchy.
There may be some inconsistency here—either Parser should store AngleType or it doesn’t need to store Base. It won’t cause any errors because we’ll always get evaluated what was displayed.
| angleType | Whether to use radians or degrees |
References jscicalc.OObject.auto_simplify(), jscicalc.Parser.convertToProduct(), jscicalc.Parser.list, and jscicalc.Parser.stripParenthesis().
Referenced by jscicalc.button.STOButton.actionPerformed(), jscicalc.button.SplusButton.actionPerformed(), jscicalc.button.SminusButton.actionPerformed(), jscicalc.button.MplusButton.actionPerformed(), jscicalc.button.MminusButton.actionPerformed(), jscicalc.button.EqualsButton.actionPerformed(), jscicalc.Parser.main(), jscicalc.complex.Test.parse(), and jscicalc.complex.Test.parse2().
| static String jscicalc.Parser.font_size | ( | float | fontSize | ) | [static] |
May be obsolete: used to witch font size within HTML.
If it’s not obsolete, it probably belongs in a view class
| fontSize | A font size |
| String jscicalc.Parser.getExpression | ( | int | n | ) |
Convert expression to a string.
Useful for debugging and displaying. Probably obsolete. param n The number of objects to try to convert.
References jscicalc.Parser.getStringVector(), and jscicalc.Parser.list.
| String jscicalc.Parser.getExpression | ( | ) |
Convert expression to a string.
Useful for debugging and displaying.
References jscicalc.Parser.getStringVector(), and jscicalc.Parser.list.
Referenced by jscicalc.EntryLabel.insert(), jscicalc.Parser.main(), and jscicalc.EntryLabel.setExpression().
| final PObject jscicalc.Parser.getLast | ( | ) |
Get the last object.
This is used by EqualsButton to find out whether or not it should add an Ans object before evaluating an expression.
References jscicalc.Parser.list.
Referenced by jscicalc.button.STOButton.actionPerformed(), jscicalc.button.SplusButton.actionPerformed(), jscicalc.button.SminusButton.actionPerformed(), jscicalc.button.MplusButton.actionPerformed(), jscicalc.button.MminusButton.actionPerformed(), and jscicalc.button.EqualsButton.actionPerformed().
| java.util.LinkedList<PObject> jscicalc.Parser.getList | ( | ) |
We need to can get a copy of the expression if we move up the history list.
References jscicalc.Parser.list.
Referenced by jscicalc.CalculatorApplet.downHistory(), jscicalc.CalculatorApplet.pushHistory(), jscicalc.EntryLabel.setExpression(), and jscicalc.CalculatorApplet.upHistory().
| java.util.Vector<String> jscicalc.Parser.getListAsStringVector | ( | ) | [private] |
| java.util.Vector<String> jscicalc.Parser.getStringVector | ( | int | q | ) | [private] |
Find an array of String objects representing the first q GObject objects.
The strange format is so that we could know how many characters (including HTML) were in the string. The Navigator class uses this kind of information.
| q | An integer |
References jscicalc.Parser.list, and jscicalc.pobject.PObject.name_array().
Referenced by jscicalc.Parser.getExpression().
| boolean jscicalc.Parser.isEmpty | ( | ) |
Are there any objects in the current expression.
References jscicalc.Parser.list.
| static void jscicalc.Parser.main | ( | String | args[] | ) | [static] |
Test code.
References jscicalc.Parser.add(), jscicalc.Parser.evaluate(), jscicalc.Parser.getExpression(), and jscicalc.Parser.Parser().
| void jscicalc.Parser.setList | ( | java.util.LinkedList< PObject > | newlist | ) |
If we move up or down the history list we change the current expression using this function.
| newlist | A list of PObject objects to be copied into this Parser |
References jscicalc.Parser.list.
Referenced by jscicalc.CalculatorApplet.downHistory(), and jscicalc.CalculatorApplet.upHistory().
| int jscicalc.Parser.size | ( | ) |
How many objects (usually PObject objects) in the expression?
References jscicalc.Parser.list.
| boolean jscicalc.Parser.stripParenthesis | ( | java.util.List< GObject > | list | ) | [private] |
Tries to strip a pair of parentheses.
If it succeeds, the expression in parentheses is passed to convertBoolFunctions and so on up the chain so that it gets converted to a Complex. Used convertToProduct() to do detailed conversion.
References jscicalc.Parser.convertToProduct(), and jscicalc.Parser.list.
Referenced by jscicalc.Parser.evaluate().
Base jscicalc.Parser.base [private] |
The base.
Base.DECIMAL, Base.Octal, Base.HEXADECIMAL or Base.Binary.
Referenced by jscicalc.Parser.base(), jscicalc.CalculatorApplet.getBase(), jscicalc.complex.Test.parse(), jscicalc.complex.Test.parse2(), and jscicalc.CalculatorApplet.setBase().
java.util.LinkedList<GObject> jscicalc.Parser.list [private] |
The expression: a list of objects.
Actually PObject objects most of the time, but it’s convenient to keep a list of objects because during expression evaluation, Complex objects get put in the list.
Referenced by jscicalc.Parser.add(), jscicalc.Parser.clearExpression(), jscicalc.Parser.convertAFunctions(), jscicalc.Parser.convertARFunctions(), jscicalc.Parser.convertBoolFunctions(), jscicalc.Parser.convertContainers(), jscicalc.Parser.convertDFunctions(), jscicalc.Parser.convertExponentsToNumerals(), jscicalc.Parser.convertIs(), jscicalc.Parser.convertLFunctions(), jscicalc.Parser.convertMFunctions(), jscicalc.Parser.convertNumerals(), jscicalc.Parser.convertRFunctions(), jscicalc.Parser.convertToProduct(), jscicalc.Parser.convertVariables(), jscicalc.Parser.del(), jscicalc.Parser.evaluate(), jscicalc.Parser.getExpression(), jscicalc.Parser.getLast(), jscicalc.Parser.getList(), jscicalc.Parser.getListAsStringVector(), jscicalc.Parser.getStringVector(), jscicalc.Parser.isEmpty(), jscicalc.Parser.Parser(), jscicalc.Parser.setList(), jscicalc.Parser.size(), and jscicalc.Parser.stripParenthesis().
1.7.1