Class Console
java.lang.Object
com.singularsys.jepexamples.consoles.Console
- Direct Known Subclasses:
BigDecimalConsole,CPConsole,PostfixEvaluationConsole,PrefixDumperConsole,PrintConsole,VectorConsole
This class implements a simple command line utility for evaluating
mathematical expressions.
Usage: java com.singularsys.jepexamples.consoles.Console [expression]If an argument is passed, it is interpreted as an expression and evaluated. Otherwise, a prompt is printed, and the user can enter expressions to be evaluated.
This class has been designed to be subclassed to allow different console applications. The methods
public void initialise() public Object processEquation(Node node) throws Exception public boolean testSpecialCommands(String command) public void printPrompt() public void printIntroText() public void printHelp()can all be overwritten.
Furthermore, main should be overwritten. For example
public static void main(String args[]) {
Console c = new DJepConsole();
c.run(args);
}
The main input loop is approximately
initialise();
printIntroText();
print(getPrompt());
String command;
while((command = getCommand()) != null)
{
if(command.equals("quit") || command.equals("exit"))
break;
if(!testSpecialCommands(command)) continue;
try {
Node n = j.parse(command);
processEquation(n);
} catch(Exception e) {}
print(getPrompt());
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classstatic enumValues returned by @link{testSpecialCommands(String command)}. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringGet a command from the input.Prints the prompt string.booleanHandle an error in the parse and evaluate routines.voidsets up all the needed objects.voidThe main input loop for interactive operation.static voidCreates a new Console object and calls run()voidPrints a line of text no newline.voidPrints a list of defined functions.voidPrint help message.voidPrints introductory text.voidPrints a line of text followed by a newline.voidprintOps()Prints a list of defined operators.final voidPrints a standard help message.voidPrints a list of variable.booleanprocessCommand(String command) Process a single command.processEquation(Node node) Performs the required operation on a node.voidThe main entry point with command line argumentsvoidsetAlteredCommand(String alt) Set the command used if @link{SPEC_ACTION.ALTERED} returned.voidString[]Splits a string on spaces.testSpecialCommands(String command) Checks for special commands.Return string representation of object.
-
Field Details
-
jep
Main Jep object -
doubleFormat
Format for double output -
history
History -
showHistory
protected boolean showHistory
-
-
Constructor Details
-
Console
public Console()Constructor
-
-
Method Details
-
initialise
public void initialise()sets up all the needed objects. -
run
The main entry point with command line arguments -
inputLoop
public void inputLoop()The main input loop for interactive operation. Repeatedly calls getCommand() and processCommand(). -
processCommand
Process a single command.- Calls
testSpecialCommands(String) - Tests for exit, break, and altered results.
- Adds the command to the history.
- Parses the command.
- Calls
processEquation(Node) - Checks for errors, calling
handleError(Exception)in necessary
- Parameters:
command- The line to be processed- Returns:
- false if un-recoverable error or 'quit' or 'exit'
- Calls
-
processEquation
Performs the required operation on a node. Typically, evaluates the node and prints the value.- Parameters:
node- Node representing expression- Returns:
- The result of the calculation
- Throws:
JepException- if a Parse or evaluation error
-
getCommand
Get a command from the input.- Returns:
- null if an IO error or EOF occurs.
-
getPrompt
Prints the prompt string. -
printStdHelp
public final void printStdHelp()Prints a standard help message. Type 'quit' or 'exit' to quit, 'help' for help. -
printHelp
public void printHelp()Print help message. -
printIntroText
public void printIntroText()Prints introductory text. -
printFuns
public void printFuns()Prints a list of defined functions. -
printOps
public void printOps()Prints a list of defined operators. -
printVars
public void printVars()Prints a list of variable. -
testSpecialCommands
Checks for special commands. For example a subclass may have a verbose mode switched on of off using the commandverbose on
This method can be used detected this input, perform required actions and skip normal processing by returning true.
In general subclasses should call the superclass methods to test for special commands that class implements
- Parameters:
command-- Returns:
- SPEC_ACTION.CONTINUE - continue processing this equation, SPEC_ACTION.BREAK - stop processing this equation and get the next line of input, SPEC_ACTION.ALTERED - the input text has been altered, SPEC_ACTION.EXIT stop the program
- See Also:
-
setAlteredCommand
Set the command used if @link{SPEC_ACTION.ALTERED} returned.- Parameters:
alt-
-
setFormat
-
handleError
Handle an error in the parse and evaluate routines. Default is to print the error message for JepExceptions and a stack trace for other exceptions- Parameters:
e-- Returns:
- false if the error cannot be recovered and the program should exit
-
split
Splits a string on spaces.- Parameters:
s- the input string- Returns:
- an array of the tokens in the string
-
print
Prints a line of text no newline. Subclasses should call this method rather than System.out.print to allow for output to different places. -
toString
Return string representation of object. Used the doubleFormat if specified.- Parameters:
o-- Returns:
-
println
Prints a line of text followed by a newline. Subclasses should call this method rather than System.out.print to allow for output to different places. -
main
Creates a new Console object and calls run()
-