Class Console

All Implemented Interfaces:
KeyListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible
Direct Known Subclasses:
BlockStatments, GroupConsole, RpeConsole, VectorConsole, XJepConsole

public class Console extends Applet implements KeyListener
This class implements a simple command line utility for evaluating mathematical expressions.
   Usage: java org.lsmp.djepExamples.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 and its subclasses can also be run as a java applet which displays a textarea for interactive input.

This class has been designed to be sub classed to allow different consol applications. The methods

 public void initialise()
 public void 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());
 }
See Also:
  • Field Details

    • j

      protected JEP j
      Main JEP object
    • ta

      protected TextArea ta
      Text area for user input in applets.
  • Constructor Details

    • Console

      public Console()
      Constructor
  • Method Details

    • init

      public void init()
      Applet initialization
      Overrides:
      init in class Applet
    • initialise

      public void initialise()
      sets up all the needed objects.
    • main

      public static void main(String[] args)
      Creates a new Console object and calls run()
    • run

      public void run(String[] args)
      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

      public boolean processCommand(String command)
      Process a single command.
      1. Tests for exit, quit, and help.
      2. Tests for any special commands used by sub classes. testSpecialCommands(String)
      3. Parses the command.
      4. Processes the node. processEquation(Node)
      5. Checks for errors. handleError(Exception)
      Parameters:
      command - The line to be processed
      Returns:
      false if un-recoverable error or 'quit' or 'exit'
    • processEquation

      public void processEquation(Node node) throws ParseException
      Performs the required operation on a node. Typically evaluates the node and prints the value.
      Parameters:
      node - Node representing expression
      Throws:
      ParseException - if a Parse or evaluation error
    • getCommand

      protected String getCommand()
      Get a command from the input.
      Returns:
      null if an IO error or EOF occurs.
    • getPrompt

      public String 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

      public boolean testSpecialCommands(String command)
      Checks for special commands used by subclasses. For example a subclass may have a verbose mode switched on of off using the command
      verbose on
      This method can be used detected this input, perform required actions and skip normal processing by returning true.
      Parameters:
      command -
      Returns:
      true indicates normal processing should continue (default) false if the command is special and no further processing should be performed (parsing and evaluating)
      See Also:
    • handleError

      public boolean handleError(Exception e)
      Handle an error in the parse and evaluate routines.
      Parameters:
      e -
      Returns:
      false if the error cannot be recovered and the program should exit
    • split

      public String[] split(String s)
      Splits a string on spaces.
      Parameters:
      s - the input string
      Returns:
      an array of the tokens in the string
    • print

      public void print(Object o)
      Prints a line of text no newline. Subclasses should call this method rather than System.out.print to allow for output to different places.
    • println

      public void println(Object o)
      Prints a line of text no newline. Subclasses should call this method rather than System.out.print to allow for output to different places.
    • keyReleased

      public void keyReleased(KeyEvent event)
      Handles keyRelease events
      Specified by:
      keyReleased in interface KeyListener
    • keyPressed

      public void keyPressed(KeyEvent arg0)
      Specified by:
      keyPressed in interface KeyListener
    • keyTyped

      public void keyTyped(KeyEvent arg0)
      Specified by:
      keyTyped in interface KeyListener
    • getAppletInfo

      public String getAppletInfo()
      Overrides:
      getAppletInfo in class Applet