Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes

jscicalc.Parser Class Reference

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...

List of all members.

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< PObjectgetList ()
 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< GObjectlist
 The expression: a list of objects.
Base base
 The base.

Detailed Description

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.

See also:
CalculatorApplet
PObject
Author:
J. D. Lamb
Version:
Revision:
1.9

Constructor & Destructor Documentation

jscicalc.Parser.Parser (  ) 

Member Function Documentation

void jscicalc.Parser.add ( GObject  o  ) 

Put a PObject object on the end of the expression.

Parameters:
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.

Parameters:
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.

Returns:
base

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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

See also:
Container
Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

See also:
Numeral
Parameters:
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.

Parameters:
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)

Parameters:
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.

Parameters:
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.

Parameters:
index The position at which to delete
Returns:
The deleted object, which would normally just be discarded

References jscicalc.Parser.list.

boolean jscicalc.Parser.del (  ) 

Delete GObject at position index.

Used for editing expressions. This version is probably obsolete.

Returns:
The deleted object, which would normally just be discarded

References jscicalc.Parser.list.

OObject jscicalc.Parser.evaluate ( AngleType  angleType  ) 

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.

Parameters:
angleType Whether to use radians or degrees
Returns:
A Complex or an error if the expression was nonsensical.

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

Parameters:
fontSize A font size
Returns:
The CSS to change 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.

Returns:
Expresion as a string.

References jscicalc.Parser.getStringVector(), and jscicalc.Parser.list.

String jscicalc.Parser.getExpression (  ) 

Convert expression to a string.

Useful for debugging and displaying.

Returns:
Expresion as a string.

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.

Returns:
last object in the 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.

Returns:
A copy of the current expression

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.

Parameters:
q An integer
Returns:
GObject instances that can be placed in a string.

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.

Returns:
true if current expression is empty.

References jscicalc.Parser.list.

static void jscicalc.Parser.main ( String  args[]  )  [static]
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.

See also:
getList()
Parameters:
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?

Returns:
size of expression stored.

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.

Returns:
true if we managed to strip a pair of parentheses.

References jscicalc.Parser.convertToProduct(), and jscicalc.Parser.list.

Referenced by jscicalc.Parser.evaluate().


Member Data Documentation

The base.

Base.DECIMAL, Base.Octal, Base.HEXADECIMAL or Base.Binary.

See also:
base()
Base

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 documentation for this class was generated from the following file: